我有一个问题:
我有一个自定义的onClick
和onChange
函数,希望将它们包含在选择复选框中
我该如何实现?这样,当有人选择从MT生成的复选框时,他们会运行自定义的onClick
和onChange
吗?
还想知道是否可以为搜索栏添加自动填充功能吗?
即,您开始输入文字,它会为您提供与数据中的内容相匹配的值 或前5个值或数据中的某些内容,并允许用户通过单击下拉选项来填写自动填充。
state = {
items: [],
chosen: [],
selected: []
}
toggle = (id) => {
const selected = this.state.selected;
if (selected.includes(id)) selected.splice(selected.indexOf(id), 1); else selected.push(id);
this.setState({ selected });
localStorage.setItem('chosen', JSON.stringify(selected));
console.log(this.state)
}
async componentDidMount() {
this.setState({
items: await (await fetch(`${process.env['REACT_APP_API']}/items`)).json(),
selected: JSON.parse(localStorage.getItem('chosen')) || []
});
}
render() {
return (
<Fragment>
<MaterialTable
columns={[
{
title: 'myChecbox',
render: rowData => {
return (
{/* THIS WORKS AS IT SHOULD */}
<input className="toggle_checkbox toggle" type="checkbox" checked={this.state.selected.includes(rowData.id)} onChange={this.toggle.bind(this, rowData.id)} />
)
},
cellStyle: {
width:20,
maxWidth:20,
padding:20
},
headerStyle: {
width:20,
maxWidth:20,
padding:20
}
},
{
title: 'Logo', field: 'name',
render: rowData => {
return (
<a href={rowData.link} key={rowData.id}>
<img className="image" src={rowData.logo} alt={rowData.name} />
</a>
)
}
},
{
title: 'Name', field: 'name',
render: rowData => {
return (
<a href={rowData.link} key={rowData.id}>
<div className="NameContainer">
<h5>{rowData.name}</h5>
<span>{rowData.type}</span>
</div>
</a>
)
}
},
{
title: 'score', field: 'score',
cellStyle: data => {
if (data === "N/A") {
return {
color: "#aaa",
}
}
}
},
{ title: 'Size', field: 'size' },
]}
data=
{
this.state.items.length ? this.state.items.map((item) => (
{
id: item.id,
logo: item.Logo.url,
name: item.Name,
type: item.Type.replace(/_/g, ' '),
score: item.Score,
size: item.Size
}
)) : [{ name: 'LOADING' }]
}
options={{
maxBodyHeight: 1000,
grouping: true,
selection: true,
pageSize: 10,
doubleHorizontalScroll: true,
}}
{/* I CANT GET THIS TO WORK =/ */}
onRowClick={(event, rowData) => { this.state.selected.includes(rowData.id); }}
onSelectionChange={data => {this.toggle.bind(this, data.id)}}
/>
</Fragment>
);
}
});
答案 0 :(得分:0)
onSelectionChange
传递选定行的数组。因此data参数没有id。
要自动完成搜索字段,您可以在github上打开功能请求问题。
答案 1 :(得分:0)
您不应直接更改状态,而应使用setState。
import React, { Fragment, Component } from "react";
import MaterialTable from "material-table";
import ReactDOM from "react-dom";
import "./styles.css";
class App extends Component {
state = {
items: [],
chosen: [],
selected: []
};
toggle = id => {
const selected = this.state.selected;
if (selected.includes(id)) selected.splice(selected.indexOf(id), 1);
else selected.push(id);
this.setState({ selected });
localStorage.setItem("chosen", JSON.stringify(selected));
console.log(this.state);
};
async componentDidMount() {
this.setState({
items: await (await fetch(
`${process.env["REACT_APP_API"]}/items`
)).json(),
selected: JSON.parse(localStorage.getItem("chosen")) || []
});
}
render() {
return (
<Fragment>
<MaterialTable
columns={[
{
title: "myChecbox",
render: rowData => {
return (
<input
className="toggle_checkbox toggle"
type="checkbox"
checked={this.state.selected.includes(rowData.id)}
onChange={this.toggle.bind(this, rowData.id)}
/>
);
},
cellStyle: {
width: 20,
maxWidth: 20,
padding: 20
},
headerStyle: {
width: 20,
maxWidth: 20,
padding: 20
}
},
{
title: "Logo",
field: "name",
render: rowData => {
return (
<a href={rowData.link} key={rowData.id}>
<img
className="image"
src={rowData.logo}
alt={rowData.name}
/>
</a>
);
}
},
{
title: "Name",
field: "name",
render: rowData => {
return (
<a href={rowData.link} key={rowData.id}>
<div className="NameContainer">
<h5>{rowData.name}</h5>
<span>{rowData.type}</span>
</div>
</a>
);
}
},
{
title: "score",
field: "score",
cellStyle: data => {
if (data === "N/A") {
return {
color: "#aaa"
};
}
}
},
{ title: "Size", field: "size" }
]}
data={
this.state.items.length
? this.state.items.map(item => ({
id: item.id,
logo: item.Logo.url,
name: item.Name,
type: item.Type.replace(/_/g, " "),
score: item.Score,
size: item.Size
}))
: [{ name: "LOADING" }]
}
options={{
maxBodyHeight: 1000,
grouping: true,
selection: true,
pageSize: 10,
doubleHorizontalScroll: true
}}
onRowClick={(event, rowData) => {
this.setState({
selected: [...this.state.selected, rowData.tableData.id]
});
}}
onSelectionChange={data => {
this.toggle.bind(this, data.id);
}}
/>
</Fragment>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
答案 2 :(得分:0)
您打错了,在“选项”中激活选择时,必须像数组一样使用dataRow。 所以这是一个如何使用它的例子 https://codesandbox.io/s/7zz0v3m91x
关于自动填充,此表现在还不可用,但是如果您像在远程模式下那样使用该表,则几乎可以达到相同的效果,因为它将在表中显示要完成的元素
希望我能帮上忙 问候