【问题标题】:Adding Edit button with icon in a react-bootstrap-table2在 react-bootstrap-table2 中添加带有图标的编辑按钮
【发布时间】:2018-09-28 10:45:40
【问题描述】:

我想要表格中这样的一列来编辑整个对应的行 对应于react-Bootstrap_中的每个编辑按钮table2

我想做什么的示例图片:

import React from "react";
import "../../node_modules/bootstrap/dist/css/bootstrap.min.css";
import BootstrapTable from "react-bootstrap-table-next";
import paginationFactory from "react-bootstrap-table2-paginator";
import cellEditFactory from "react-bootstrap-table2-editor";

export class Table extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      data: [
        {
          id: 1,
          dn: "Gob",
          f: "wah",
          ct: "2",
          cr: "acha",
          dsf: 12,
          dsp: 30
        },
        {
          id: 2,
          dn: "Buster",
          f: "wah",
          ct: "5",
          cr: "acha",
          dsf: 12,
          dsp: 30
        },
        {
          id: 3,
          dn: "George Michael",
          f: "wah",
          ct: "4",
          cr: "acha",
          dsf: 12,
          dsp: 30
        },
        {
          id: 3,
          dn: "George Michael",
          f: "wah",
          ct: "4",
          cr: "acha",
          dsf: 12,
          dsp: 30
        },
        {
          id: 3,
          dn: "George Michael",
          f: "wah",
          ct: "4",
          cr: "acha",
          dsf: 12,
          dsp: 30
        },
        {
          id: 3,
          dn: "George Michael",
          f: "wah",
          ct: "4",
          cr: "acha",
          dsf: 12,
          dsp: 30
        },
        {
          id: 3,
          dn: "George Michael",
          f: "wah",
          ct: "4",
          cr: "acha",
          dsf: 12,
          dsp: 30
        },
        {
          id: 3,
          dn: "George Michael",
          f: "wah",
          ct: "4",
          cr: "acha",
          dsf: 12,
          dsp: 30
        },
        {
          id: 3,
          dn: "George Michael",
          f: "wah",
          ct: "4",
          cr: "acha",
          dsf: 12,
          dsp: 30
        },
        {
          id: 3,
          dn: "George Michael",
          f: "wah",
          ct: "4",
          cr: "acha",
          dsf: 12,
          dsp: 30
        },
        {
          id: 1,
          dn: "Gob",
          f: "wah",
          ct: "2",
          cr: "acha",
          dsf: 12,
          dsp: 30
        },
        {
          id: 2,
          dn: "Buster",
          f: "wah",
          ct: "5",
          cr: "acha",
          dsf: 12,
          dsp: 30
        },
        {
          id: 3,
          dn: "George Michael",
          f: "wah",
          ct: "4",
          cr: "acha",
          dsf: 12,
          dsp: 30
        },
        {
          id: 3,
          dn: "George Michael",
          f: "wah",
          ct: "4",
          cr: "acha",
          dsf: 12,
          dsp: 30
        },
        {
          id: 3,
          dn: "George Michael",
          f: "wah",
          ct: "4",
          cr: "acha",
          dsf: 12,
          dsp: 30
        },
        {
          id: 3,
          dn: "George Michael",
          f: "wah",
          ct: "4",
          cr: "acha",
          dsf: 12,
          dsp: 30
        },
        {
          id: 3,
          dn: "George Michael",
          f: "wah",
          ct: "4",
          cr: "acha",
          dsf: 12,
          dsp: 30
        },
        {
          id: 3,
          dn: "George Michael",
          f: "wah",
          ct: "4",
          cr: "acha",
          dsf: 12,
          dsp: 30
        },
        {
          id: 3,
          dn: "George Michael",
          f: "wah",
          ct: "4",
          cr: "acha",
          dsf: 12,
          dsp: 30
        },
        {
          id: 3,
          dn: "George Michael",
          f: "wah",
          ct: "4",
          cr: "acha",
          dsf: 12,
          dsp: 30
        }
      ],
      columns: [
        {
          dataField: "dn",
          text: "Doctor Name",
          sort: true
        },
        {
          dataField: "f",
          text: "Facility",
          sort: true
        },
        {
          dataField: "ct",
          text: "Consultation Type",
          sort: true
        },
        {
          dataField: "cr",
          text: "Consultation Rate",
          sort: true
        },
        {
          dataField: "dsf",
          text: "Doctor Share (Fixed)",
          sort: true,
          style: { backgroundColor: "#e0f7fa" }
        },
        {
          dataField: "dsp",
          text: "Doctor Share(%)",
          sort: true,
          style: { backgroundColor: "#fbe9e7" }
        },
        {
          dataField: "edit",
          text: "Edit",
          editCellStyle: (cell, row, rowIndex, colIndex) => {
            const icon = { style: 'class="glyphicon glyphicon-pencil">' };
            return { icon };
          }
        }
      ]
    };
  }

  render() {
    const selectRow = {
      mode: "checkbox",
      clickToSelect: true
    };

    // const editFormatter = (cell, row, rowIndex, formatExtraData) => {
    //   <Button
    //     icon
    //     labelPosition="left"
    //     onClick={() => deleteMe(rowIndex, rowId)}
    //   >
    //     <Icon name="remove" /> Remove{" "}
    //   </Button>;
    // };
    const customTotal = (from, to, size) => (
      <span className="react-bootstrap-table-pagination-total">
        Showing {from} to {to} of {size} Results
      </span>
    );

    return (
      <div className="container" style={{ marginTop: 50 }}>
        <BootstrapTable
          striped
          hover
          keyField="dn"
          data={this.state.data}
          columns={this.state.columns}
          pagination={paginationFactory({ nextPageText: "Next" })}
          selectRow={selectRow}
          cellEdit={cellEditFactory({ mode: "click" })}
        />
      </div>
    );
  }
}

如何在 编辑 栏中添加按钮? 这样我应该能够编辑一行。 我知道必须有一些机制来自定义列并添加 图片中提到的图标。

【问题讨论】:

  • 您好,欢迎来到 SO。您需要共享代码,以便我们能够帮助您解决问题
  • @Think-Twice 重新编辑了帖子。

标签: javascript reactjs


【解决方案1】:

您可以在列定义中添加一个虚拟字段,并引用一个格式化程序,该格式化程序可以在每一行的列中呈现一个组件。在我们的例子中,该组件是一个 ActionsFormater,我们使用它来呈现多个按钮(查看、编辑、删除),这些按钮可以作用于为每一行传入的 id。

{
    dataField: 'actions',
    text: 'Actions',
    isDummyField: true,
    csvExport: false,
    formatter: this.actionsFormatter,
  },

actionsFormatter = (cell, row) => <ActionsFormatter id={row.id} />;

看起来像这样:

【讨论】:

  • 你在哪里给出了按钮的图标?按钮标签在哪里?
  • 好的,我制作了一个编辑按钮组件,然后将该组件导入为 ActionsFormatter。但是 className ="any Glyphicon" 在 ActionsFormatter 组件中的按钮标签中不起作用
  • 这是特定于您的工具的。我们正在使用 Font Awesome 和 react-strap,按钮看起来像这样:
【解决方案2】:

我做了这个函数并返回了 EditIcon 按钮组件。

function rankFormatter(cell, row, rowIndex, formatExtraData) { 
     return ( 
           < div 
               style={{ textAlign: "center",
                  cursor: "pointer",
                 lineHeight: "normal" }}>

        < EditIcon
          style={{ fontSize: 20 }}
          color="disabled"  
         /> 
      </div> 
 ); } 

这是我将 rankFormatter 函数分配给格式化程序的列。

      { dataField: "edit", 
        text: "Edit",
        sort: false,
        formatter: rankFormatter,
        headerAttrs: { width: 50 },
        attrs: { width: 50, class: "EditRow" } 
      }

这正是我想要的方式......!

【讨论】:

【解决方案3】:

其他用户的解决方案有效,但我只是注意到当前版本的 reactstrap-table-next 已内置支持此功能,因此我将为未来的用户提供此答案。

可以通过将属性expandRow.showExpandColumn 设置为true 来显示额外的列。 然后要仅在单击此列时扩展行,属性 expandRow.expandByColumnOnly 也必须设置为 true。 此列出现的一侧可以通过属性expandRow.expandByColumnOnly 设置,其值可以是leftright

最后,我们还必须通过属性 'expandRow.expandColumnRenderer' 为该列传递一个渲染器,该属性采用类似 ({ expanded, rowKey, expandable }) =&gt; () 的函数

expandRow 示例:

const expandRow = {
    renderer: row => ...,
    expandByColumnOnly: true,
    expandColumnPosition: 'right',
    showExpandColumn: true,
    expandColumnRenderer: ({expanded, rowKey, expandable}) => {
        const onclick = (event) => {
            console.log("clicked on row ", rowKey)
        }

        if (!expandable) {
            return;
        }
        return (
            <button type="button" onClick={onclick} className="btn btn-outline-primary"/>
        );
    }
};

...

<BootstrapTable ... expandRow={expandRow} />

Documentation here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-25
    • 2016-10-06
    • 1970-01-01
    • 2020-02-23
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    相关资源
    最近更新 更多