简体   繁体   English

React Datatables的自定义搜索框

[英]Custom Search box for React Datatables

I have a Datatable set up like the default shown here . 我有一个Datatable设置,就像这里显示的默认设置一样。 I want to have the search bar to filter the data detached from where it is and have it sitting somewhere completely different. 我想让搜索栏过滤从它所在位置分离出来的数据,让它坐在完全不同的地方。 So I want to have my own input field used to search the data in the exact way the current default one does. 所以我想让我自己的输入字段用于以当前默认方式的确切方式搜索数据。

Here is my code and my current progress... 这是我的代码和我目前的进展......

state = {
    searchValue: ''
}

const data = [
    ["Gabby", "Data 1", "Pending", "15/01/19", "$100,000"],
    ..........];

let options = {
    .....
    customSearch: (searchQuery, currentRow, columns) => {
        let isFound = false;
        currentRow.forEach(col => {
            if (col.toString().indexOf(this.state.searchValue) >= 0) {
                isFound = true;
            }
        });
    return isFound;
    }
};
.....
<input
    type="text"
    value={this.state.searchValue}
    onChange={(e, value) => this.handleSearchChange(e, value)}
/>
......
<MUIDataTable
    title={"My Title"}
    data={data}
    columns={columns}
    options={options}
/>

What this currently does is you can type into my custom input field but you have to open up the default search field and when you type in there it uses my custom search text. 目前这样做是你可以输入我的自定义输入字段,但你必须打开默认搜索字段,当你输入它时,它使用我的自定义搜索文本。 So in a way I have it searching using the custom search. 所以在某种程度上我使用自定义搜索进行搜索。 I would like it to work just as it does in the gif . 我希望它能像在gif中一样工作

Sorry if this is noob but I haven't been working with React long. 对不起,如果这是菜鸟,但我没有使用React长。

Thanks 谢谢

I was so close. 我太近了。 I just needed to add searchText: this.state.searchValue, above customSearch and change if (col.toString().indexOf(this.state.searchValue) >= 0) { to if (col.toString().indexOf(searchQuery) >= 0) { . 我只需要添加searchText: this.state.searchValue,上述customSearch和更改if (col.toString().indexOf(this.state.searchValue) >= 0) {if (col.toString().indexOf(searchQuery) >= 0) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM