reactjs - 检查是否选择了 MUIDataTable onRowSelect?
问题描述
我如何知道是否在 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}
/>
解决方案
在您可以传递的选项中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} />
推荐阅读
- android - Android 错误膨胀 ImageView - ResourcesNotFoundException
- python - 如何在熊猫中使用 .assign 方法有条件地分配计算列?
- postgresql - 使用 SECURITY DEFINER 调用函数时权限被拒绝,即使在 GRANT ALL to role 已经运行之后
- python-3.x - 如何在没有任何模型的情况下使用带有 Django Rest Framework 的视图集通过 post 方法在 Swagger UI 中传递参数?
- php - 将值从 json 键/值对重新分配给下一个键
- swift - 如何在 Swift 4 中获取 AVPlayer 项目的总持续时间
- python - 如何比较两个有变化的文件的 csv 文件和输出行?
- php - SQL Query 在 phpmyadmin 中运行良好,但 php 代码存在 T_Variable 问题
- reactjs - 键盘在 Android 上打开并立即消失
- c# - 为什么使用仅公开公钥的证书时 TLS 握手会失败?