【问题标题】:React Material - MUIDataTable - Is there a way to have the row count in the header?React Material - MUIDataTable - 有没有办法在标题中计算行数?
【发布时间】:2020-09-01 09:02:47
【问题描述】:

有人知道在标题中包含行数的方法吗? 我似乎无法使用 optionscolumns 属性来做到这一点。

我想要实现的是:

第 1 列标题(行数)|第 2 列标题 | ...... |第 N 列标题

提前感谢您的任何建议。

更多信息:

  1. What I am trying to achieve in a picture

  2. Trying to get a working copy of my app in SandBox and will post back. Current sandbox shows the options and columns options used

【问题讨论】:

  • 到目前为止你有什么尝试?你可以发布一些代码吗?还是我们可以用来启动的代码框?或者你想要的图片
  • @Incepter 为“不完整”的问题道歉。
  • 已发现此工作所需的信息是“tableState”状态/属性的一部分,可作为 onTableChange 表属性中的道具使用,但似乎不是标题/列选项。我想要实现的是具有以下格式的标题:标题名称(过滤器记录数/记录总数)
  • 我认为你应该创建自己的标题,因为你有所需的道具
  • @incepter 如前所述,我已经考虑过这个选项,但是我对如何实现它有点困惑:当尝试使用“customHeadRender”创建自定义标题时,我似乎没有可以访问 tableState(请参阅原始问题中的 CodeSandBox)。抱歉,如果上述内容看起来微不足道,但我在 React 中还很陌生,并试图了解这个库是如何工作的。任何帮助/见解将不胜感激。

标签: reactjs mui-datatable


【解决方案1】:

请检查这个例子:

import React from "react";
import MUIDataTable from "mui-datatables";

export default class MuiDatatableHeader extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            filteredRowCount: 0
        }
    }

    render() {
        const columns = [
            {
                label: "Name",
                name: "Name",
                options: {
                    customHeadRender: (columnMeta, updateDirection) => (
                        `${columnMeta.name} (Total Count ${data.length})`
                    )
                }
            },
            {
                label: "Title", name: "Title",
                options: {
                    customHeadRender: (columnMeta, updateDirection) => (
                        `${columnMeta.name} (Total Filter Row Count ${this.state.filteredRowCount})`
                    )
                }
            },
            {name: "Location"},
            {name: "Age"},
            {name: "Salary"}
        ];
        const data = [
            ["Gabby George", "Business Analyst", "Minneapolis", 30, "$100,000"],
            ["Aiden Lloyd", "Business Consultant", "Dallas", 55, "$200,000"]
        ];

        const options = {
            selectableRows: "multiple",
            selectableRowsHeader: data.length > 0,
        };

        return (
            <div>
                <button onClick={() => {
                    this.setState({filteredRowCount: 10})
                }}>Filter data
                </button>
                <MUIDataTable
                    title={"ACME Employee list"}
                    data={data}
                    columns={columns}
                    options={options}
                />
            </div>
        );
    }
}

【讨论】:

  • 感谢您的回答。我已经厌倦了您提出的解决方案,问题是它只显示记录总数。我所追求的是(过滤记录数)/(记录总数)。此信息是 tableState 属性/状态的一部分,更具体地说是 tableState.displayData 的一部分,但状态不是可从 customHeadRender 等属性访问。
  • @Nikolaos,我已经更新了我的答案,我在状态下放置了一个状态变量 filteredRowCount ,并使用按钮单击更新了 filteredRowCount 的数据。然后filteredRowCount 显示在Title 列标题中。希望对你有帮助
  • 我不确定你是否理解我想要达到的目标,也许我没有正确解释。我正在寻找的是能够在过滤时过滤的行数以 (过滤记录数)/(记录总数) 的格式显示在标题中乙>。您的方法只是将状态设置为预定义的数字。
  • 是的,我完全理解你之前所说的,这就是我给你这个例子的原因,因为我无法给出你正在寻找的确切例子。在这里,您将过滤结果设置为状态,然后您可以从过滤后已设置的状态显示(过滤记录数)/(记录总数)
猜你喜欢
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 2021-07-26
  • 2012-10-16
相关资源
最近更新 更多