我在我的项目中添加了一个可编辑的反应表(https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/examples/kitchen-sink),一切正常。但是当我添加一个带有复选框的列,勾选复选框并转到不同的页面(或排序或搜索)并返回时,勾选消失了。这就是我将复选框添加到“列”字段的方式,
{
Header: 'On Leave',
accessor: 'onLeave',
Filter: SelectColumnFilter,
filter: 'includes',
disableGroupBy: true,
Cell: row => { return(
<div style={{'text-align':'center'}}>
<input type="checkbox"
value={row.value == "Yes" ? "on" : "off"}
onBlur={(event) => updateMyData(parseInt(row.row.id), row.column.id, event.target.checked ? "Yes" : "No")} />
</div>)},
}
updateMyData() 在复选框丢失焦点并且 console.log 打印正确数据时触发,
0 : 0 : onLeave : Yes
1 : 1 : onLeave : Yes
2 : 2 : onLeave : Yes
3 : 3 : onLeave : Yes
4 : 4 : onLeave : Yes
updateMyData() 如下,
// When our cell renderer calls updateMyData, we'll use
// the rowIndex, columnId and new value to update the
// original data
const updateMyData = (rowIndex, columnId, value) => {
// We also turn on the flag to not reset the page
skipResetRef.current = true
setData(old =>
old.map((row, index) => {
if (index === rowIndex) { console.log(index + " : " + rowIndex + " : " + columnId + " : " + value););
return {
...row,
[columnId]: value,
}
}
return row
})
)
}
为什么复选框值未保存在“数据”字段中?谢谢