【问题标题】:Check if MUIDataTable onRowSelect is selected?检查是否选择了 MUIDataTable onRowSelect?
【发布时间】:2021-07-10 21:17:31
【问题描述】:

如何知道在 onRowSelect 方法中该行是否被选中?

import MUIDataTable from "mui-datatables";

...

const [ total, setTotal ] = useState(0);

...

const handleRowSelect = (curRowSelected) => {
    console.log(curRowSelected);
    const val = credits[curRowSelected[0].index].amount;
    // I want to add a condition: IF selected, I'll add value to my total variable; otherwise, I'll subtract
}

const options = {
  filterType: 'dropdown',
  pagination: false,
  onRowsSelect: handleRowSelect
};

...

<MUIDataTable
    title={"Payments"}
    data={credits}
    columns={columns}
    options={options}
/>

【问题讨论】:

    标签: reactjs mui-datatable


    【解决方案1】:

    您可以在选项中传递isRowSelectable,这将为您提供所选行的索引。

    const columns = [....]
    const data = [...]
    const options = {
    ...whateverOptionsYouHave,
    onRowSelect: (selectedRows) => {
    
    // selectedRows would be an array of objects containing index and dataIndex
    /*
      selectRows = [
        { dataIndex: 0, // will be index of selected row
          index: 0
        }
      ]
    */
       console.log(selectedRows)
       // write code to whatever you want to with selected row
     }
    }
    
    <MUIDataTable options={options} columns={columns} data={data} />
    
    
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-07
      • 1970-01-01
      • 2014-03-28
      • 2012-12-28
      • 1970-01-01
      • 1970-01-01
      • 2012-09-13
      • 1970-01-01
      相关资源
      最近更新 更多