简体   繁体   English

如何使用 react-bootstrap-table 为列放置事件

[英]How to put event for column using react-bootstrap-table

I am working with table in my react application, I am usingReact bootstrap table2 for this, I am successfully rendering the table.我正在我的反应应用程序中使用表格,为此我使用了React bootstrap table2 ,我成功地呈现了表格。

Please do check the edited part请检查编辑的部分

  • I am facing issue in rendering the table data conditionally我在有条件地呈现表数据时遇到问题
  • I have one column named as Availability so it is true and false, what I am trying to do is when it is true show tick mark (right) if false show cross one, I am able to insert new column but I am not able to update the existing one.我有一个名为Availability列,所以它是真的和假的,我想要做的是当它是真的时显示刻度线(右)如果假显示交叉一,我可以插入新列,但我不能更新现有的。

What I was doing to insert new column我在做什么来插入新列

let c = {
  dataField: "Class",
  text: "Class",
  editable: false,
  formatter: () => {
    return 12;
  },
};
d.push(c);
setColumnData(d);

Above d is columnData d上方是 columnData

But I am stuck here to update the existing one但我被困在这里更新现有的

My data我的资料

{
  column_data: [
    {
      dataField: "first_name",
      text: "Name",
      sort: true,
    },
    {
      dataField: "last_name",
      text: "Last Name",
      sort: false,
    },
    {
      dataField: "availability",
      text: "Available",
      sort: true,
    },
  ],
  tableData: [
    {
      first_name: "simon",
      last_name: "chaz",
      availability: true,
    },
    {
      first_name: "steve",
      last_name: "smith",
      availability: false,
    },
    {
      first_name: "michel",
      last_name: "gread",
      availability: true,
    },
    {
      first_name: "rimon",
      last_name: "class",
      availability: false,
    },
  ],
}

This is how I am rendering table:这就是我渲染表格的方式:

<BootstrapTable
  bootstrap4
  keyField="certificate_Name"
  data={data.tableData}
  columns={data.column_data}
  wrapperClasses="table-responsive"
  classes="table-hover table-bordered"
  headerClasses="header-class"
/>;
  • If Availability is true then I want to show <i className="ri-checkbox-circle-line"></i> .如果Availability为真,那么我想显示<i className="ri-checkbox-circle-line"></i>
  • if Availability is false then <i class="ri-close-line"></i>如果Availability为 false,则<i class="ri-close-line"></i>

I am trying to show my cell data on specific condition我正在尝试在特定条件下显示我的单元格数据

Here is my code sandbox 这是我的代码沙箱

Edit编辑

I have updated my code sandbox withe the above solution,我已经使用上述解决方案更新了我的代码沙箱,

If it is true I am showing whatever I want to else What else I want to.如果是真的,我会展示我想要的任何东西 else 我想要的东西。

{
    dataField: "available",
    text: "Available",
    sort: true,
    formatter: columnFormatter  // this I am putting 
  }

And here is my function这是我的功能

 const columnFormatter = (cell, row, rowIndex, formatExtraData) => {
  if (row && row.availability) {
    return <a href="#">check this out</a>;
  } else {
    return row.availability;
  }
};

now in if condition I am trying to put an event onClick but I am not able to achieve that.现在在 if 条件下,我试图将event放在onClick但我无法实现。

I was going through this link but this is for on each row click, I want to click on specific column to specific cell. 我正在浏览此链接,但这是针对每一行点击,我想点击特定列到特定单元格。

updated code sandbox 更新的代码沙箱

Adding an onClick handler is as simple as:添加一个onClick处理程序非常简单:

<a
    href="#"
    onClick={() => {
      console.log("clicked row", rowIndex);
    }}>
    check this out
</a>

编辑沉思-driscoll-eobpe

I suppose you want to conditionally render the icons based on availability based on this我想您想根据此条件根据可用性有条件地呈现图标

  • If Availability is true then I want to show <i className="ri-checkbox-circle-line"></i> .如果可用性为真,那么我想显示<i className="ri-checkbox-circle-line"></i>
  • if Availability is false then <i class="ri-close-line"></i>如果 Availability 为 false,则<i class="ri-close-line"></i>

so try doing this所以尝试这样做

const columnFormatter = (cell, row, rowIndex, formatExtraData) => {
  if (row && row.availability) {
    return <svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-check-circle-fill" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
  <path fill-rule="evenodd" d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zm-3.97-3.03a.75.75 0 0 0-1.08.022L7.477 9.417 5.384 7.323a.75.75 0 0 0-1.06 1.06L6.97 11.03a.75.75 0 0 0 1.079-.02l3.992-4.99a.75.75 0 0 0-.01-1.05z"/>
</svg>
  } else {
    return <svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-x-circle-fill" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
  <path fill-rule="evenodd" d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zM5.354 4.646a.5.5 0 1 0-.708.708L7.293 8l-2.647 2.646a.5.5 0 0 0 .708.708L8 8.707l2.646 2.647a.5.5 0 0 0 .708-.708L8.707 8l2.647-2.646a.5.5 0 0 0-.708-.708L8 7.293 5.354 4.646z"/>
</svg>
  }
};

https://codesandbox.io/s/charming-bird-e389g?file=/src/App.js:228-1118 https://codesandbox.io/s/charming-bird-e389g?file=/src/App.js:228-1118

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM