javascript - 如何在 react Bootstrap 表中添加按钮?

标签 javascript node.js reactjs ecmascript-6

我正在尝试将按钮添加到 react Bootstrap 表中的单元格,以下是我的代码:

    import React, { Component } from "react";
    import BootstrapTable from "react-bootstrap-table-next";
    import { Button } from 'reactstrap';

    class ActionsCard extends Component {

    constructor(props) {
        super(props);
        this.state = {
          actions: [{action: "Upgrade device", details: "Upgrade device to version 0.1.1", _id: "1"}],
          valid: true
        };
        this.columns = [
          {
            text: "Action",
            dataField: "action",
            sort: true,
            editable: false,
            headerStyle: (colum, colIndex) => {
              return { width: "5%", textAlign: "left" };
            }
          },
          {
            text: "Details",
            dataField: "details",
            sort: true,
            editable: false,
            headerStyle: (colum, colIndex) => {
              return { width: "12%", textAlign: "left" };
            }
          },
          {
            sort: true,
            headerStyle: (colum, colIndex) => {
              return { width: "16%", textAlign: "left" };
            },
            Header: 'Test',
            Cell: cell => (
               <Button onClick={() => console.log(cell.original)}>Upgrade</Button>
            ),
          }
        ];
      }

      render() {
        return (
          <React.Fragment>
            <BootstrapTable
              keyField="_id"
              data={this.state.actions}
              columns={this.columns}
              noDataIndication="No Interfaces available"
              defaultSorted={[{ dataField: "action", order: "asc" }]}
            />
          </React.Fragment>
        );
  }
}

export default ActionsCard;

但是,当我运行代码时,表格的前两列按预期出现,但第三列只是空的。

最佳答案

您可以使用格式化程序添加按钮,如 this讨论提及

    {
        dataField: "databasePkey",
        text: "Remove",
        formatter: (cellContent: string, row: IMyColumnDefinition) => {
            if (row.canRemove)
                return <button className="btn btn-danger btn-xs" onClick={() => this.handleDelete(row.databasePkey)}>Delete</button>
            return null
        },
    },

关于javascript - 如何在 react Bootstrap 表中添加按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57746404/

相关文章:

javascript - 根据对象的值过滤对象数组

node.js - 在 Node.js 中记录用户浏览器详细信息

javascript - Emacs Flymake-jslint 显示所有错误

node.js - 在 mongoose 中进行预更新时,user.isModified 不是函数

node.js - 将字符串替换为第一个字符串的子字符串

reactjs - 我的应用程序代码如何知道应用程序是否正在使用 testcafe 进行测试?

javascript - 添加依赖项时使用 useEffect() Hook 时如何避免无限循环?

reactjs - `updater` 无法与 Relay Modern 配合使用,因为 `ConnectionHandler.getConnection()` 返回 `undefined`

javascript - 使用 jQuery 和 CSS 重新创建动画

javascript - Node.js - 如何以正确的方式链接 Promise