我正在尝试实现一个全局搜索过滤器,用于在整个表中搜索键。我附加了一个更改处理程序,并在每个输入上触发一个回调,该回调在数据中搜索该键并设置值。当我输入字符时,它会被过滤,但是当我逐个删除字符时,过滤器似乎不起作用。我要去哪里错了? 帮助将不胜感激
代码沙箱:https://codesandbox.io/s/jolly-bhabha-iqcx1
import React from "react";
import ReactDOM from "react-dom";
import { Input } from "semantic-ui-react";
import ReactTable from "react-table";
import "react-table/react-table.css";
import "./styles.css";
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
data: [
{ firstName: "aaaaa", status: "Pending", visits: 155 },
{ firstName: "aabFaa", status: "Pending", visits: 155 },
{ firstName: "adaAAaaa", status: "Approved", visits: 1785 },
{ firstName: "aAaaaa", status: "Approved", visits: 175 },
{ firstName: "adaSaaa", status: "Cancelled", visits: 165 },
{ firstName: "aasaaa", status: "Cancelled", visits: 157 },
{ firstName: "aweaaaaaewea", status: "Approved", visits: 153 },
{ firstName: "aaaaaa", status: "Submitted", visits: 155 }
],
columns: [],
searchInput: ""
};
}
componentDidMount() {
let columns = [
{
Header: "First Name",
accessor: "firstName",
sortable: false,
show: true,
displayValue: " First Name"
},
{
Header: "Status",
accessor: "status",
sortable: false,
show: true,
displayValue: "Status "
},
{
Header: "Visits",
accessor: "visits",
sortable: false,
show: true,
displayValue: " Visits "
}
];
this.setState({ columns });
}
handleChange = event => {
this.setState({ searchInput: event.target.value }, () => {
this.globalSearch();
});
};
globalSearch = () => {
let { data, searchInput } = this.state;
if (searchInput) {
let filteredData = data.filter(value => {
return (
value.firstName.toLowerCase().includes(searchInput.toLowerCase()) ||
value.status.toLowerCase().includes(searchInput.toLowerCase()) ||
value.visits
.toString()
.toLowerCase()
.includes(searchInput.toLowerCase())
);
});
this.setState({ data: filteredData });
}
};
render() {
let { data, columns, searchInput } = this.state;
return (
<div>
<br />
<Input
size="large"
name="searchInput"
value={searchInput || ""}
onChange={this.handleChange}
label="Search"
/>
<br />
<br />
<ReactTable
data={data}
columns={columns}
defaultPageSize={10}
className="-striped -highlight"
/>
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
答案 0 :(得分:3)
这不是react-table的问题。在当前的实现中,
这是您第一次从原始数据中进行过滤。
第二次尝试过滤时,您尝试从先前过滤的数据中过滤结果(状态对象中的当前数据是先前尝试的过滤结果)。
我建议尝试(这是从您的分支出来的):https://codesandbox.io/s/eloquent-clarke-w1ehv
将原始数据保持为不可变源,并在每次搜索输入更改时从原始数据中进行过滤,而不是从处于状态的当前数据对象中进行过滤。看一看我连接的叉子。
如果您的数据来自ajax调用,则可能应该维护两个状态变量:
this.state = {
data: [],
filteredData: [],
columns: [],
searchInput: ""
};
componentDidMount() {
yourapicall.then(data => {
this.setState({
data // whatever data you get.
});
});
}
// And in your global search
globalSearch = () => {
let { searchInput, data } = this.state;
let filteredData = data.filter(value => {
return (
value.firstName.toLowerCase().includes(searchInput.toLowerCase()) ||
value.status.toLowerCase().includes(searchInput.toLowerCase()) ||
value.visits
.toString()
.toLowerCase()
.includes(searchInput.toLowerCase())
);
});
this.setState({ filteredData });
};
并有条件地呈现您的反应表
<ReactTable
data={filteredData && filteredData.length ? filteredData : data}
columns={columns}
defaultPageSize={10}
className="-striped -highlight"
/>
希望这会有所帮助!