ReactJS - react-table 中的复选框列不起作用
ReactJS - Checkbox column in react-table doesn't work
我已将 editable react-table (https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/examples/kitchen-sink) 添加到我的项目中,一切正常。但是当我添加一个带有复选框的列,并勾选复选框并转到不同的页面(或排序或搜索)并返回时,勾号消失了。这就是我将复选框添加到 'columns' 字段的方式,
{
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
})
)
}
为什么复选框值没有保存在 'data' 字段中?谢谢
问题在于使用复选框 'value' 属性。相反,使用 'defaultChecked' 解决了问题,
Cell: row => {
return(
<div style={{'text-align':'center'}}>
<input type="checkbox"
defaultChecked={row.value == "Yes" ? true : false}
onBlur={(event) => updateMyData(parseInt(row.row.id), row.column.id, event.target.checked ? "Yes" : "No")} />
</div>)}
这个问题有更多的细节,
我已将 editable react-table (https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/examples/kitchen-sink) 添加到我的项目中,一切正常。但是当我添加一个带有复选框的列,并勾选复选框并转到不同的页面(或排序或搜索)并返回时,勾号消失了。这就是我将复选框添加到 'columns' 字段的方式,
{
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
})
)
}
为什么复选框值没有保存在 'data' 字段中?谢谢
问题在于使用复选框 'value' 属性。相反,使用 'defaultChecked' 解决了问题,
Cell: row => {
return(
<div style={{'text-align':'center'}}>
<input type="checkbox"
defaultChecked={row.value == "Yes" ? true : false}
onBlur={(event) => updateMyData(parseInt(row.row.id), row.column.id, event.target.checked ? "Yes" : "No")} />
</div>)}
这个问题有更多的细节,