【发布时间】:2018-03-15 01:50:39
【问题描述】:
我在使用这个 react-table 包https://react-table.js.org/#/story/readme向行添加复选框时遇到了麻烦
我正在尝试为表格中的每一行添加一个复选框。我尝试将“复选框”添加到列区域中看到的“单元格”值,但是,它似乎不适用于分页。一旦我单击下一页然后返回,它就会忘记所有以前检查过的产品。我如何保持它们的状态?
我添加了一个键,它可以防止在所有页面上检查该元素,但是当我在页面上来回切换时它不会记住它。所以我现在只需要存储它的“开启状态”。
Cell: rowInfo => (<Checkbox key={rowInfo.index} onChange={this.handleChange} />)
这是完整的代码:
import React from 'react'
import ReactDOM from 'react-dom'
import ReactTable from 'react-table'
import PropTypes from 'prop-types'
import { Checkbox } from '@shopify/polaris';
export default class ProductIndexTable extends React.Component {
constructor(props) {
super(props);
this.state = {
rowInfo: ''
}
this.handleChange = this.handleChange.bind(this)
}
handleChange(event) {
}
render() {
function CreateItem(product) {
return {
title: <a href={'/products/' + product.id} >{product.title}</a>,
price_test_status: product.has_active_price_test,
price_test_completion_percentage: product.price_test_completion_percentage
}
}
return (<ReactTable
data={this.props.products.map(CreateItem)}
getTdProps={(state, rowInfo, column, instance) => {
return {
onClick: (e, handleOriginal) => {
// console.log('A Td Element was clicked!')
// console.log('it produced this event:', e)
// console.log('It was in this column:', column)
// console.log('It was in this row:', rowInfo)
// console.log('It was in this table instance:', instance)
this.setState({
rowInfo: rowInfo.index
})
// IMPORTANT! React-Table uses onClick internally to trigger
// events like expanding SubComponents and pivots.
// By default a custom 'onClick' handler will override this functionality.
// If you want to fire the original onClick handler, call the
// 'handleOriginal' function.
if (handleOriginal) {
handleOriginal()
}
}
}
}}
columns={[
{
Header: "Base",
columns: [
{
Header: <Checkbox />,
maxWidth: 50,
Cell: (<Checkbox onChange={this.handleChange} />)
}, {
Header: "Product Title",
accessor: "title",
maxWidth: 400
}, {
Header: "Price Test Status",
accessor: "price_test_status",
maxWidth: 200
}, {
Header: "Price Test Completion Percentage",
accessor: "price_test_completion_percentage",
Cell: row => (
<div
style={{
width: '100%',
height: '100%',
backgroundColor: '#dadada',
borderRadius: '2px'
}}
>
<div
style={{
width: `${row.value}%`,
height: '100%',
backgroundColor: row.value > 66 ? '#85cc00'
: row.value > 33 ? '#ffbf00'
: '#ff2e00',
borderRadius: '2px',
transition: 'all .2s ease-out'
}}
/>
</div>
)
}
]
}
]}
defaultPageSize={10}
className="-striped -highlight"
/>
);}
}
【问题讨论】:
-
你试过在复选框上使用
key属性吗? -
您可以尝试在这里提出问题:github.com/react-tools/react-table/issues,因为这听起来像是包中的错误
-
好主意。刚刚提出这个问题。我仍然想知道是否有解决方法?我还没有尝试使用密钥道具。我是新手,所以我必须对此进行一些研究。
-
@ByteMe 只需添加
key属性并放置一个唯一的 id,它可以是地图函数中的计数器。当它们都在虚拟 DOM 中时,React 使用它来区分相同的组件 -
感谢 Carlos,我添加了 key 道具,它阻止了它检查其他页面上的所有元素。我现在只需要弄清楚如何存储它们。查看我的更新
标签: javascript reactjs