[英]How to stop my search function from checking cases (Uppercase/lowercase) - Javascript/ReactJS
I am working on a search function in ReactJS. 我正在使用ReactJS中的搜索功能。 My search function is working fine , but it is checking cases(Uppercase/lowercase). 我的搜索功能运行正常,但是正在检查大小写(大写/小写)。 This is my Demo Fiddle . 这是我的演示小提琴 。 You can check the search functionality. 您可以检查搜索功能。 I want to get rid of the case checking(Uppercase/lowercase). 我想摆脱大小写检查(大写/小写)。 How should I change the code in the easiest manner? 我应该如何以最简单的方式更改代码?
This is my Search function 这是我的搜索功能
getMatchedList(searchText) {
console.log('inside getMatchedList');
if (searchText === ''){
this.setState({ data: this.state.dataDefault });
}
if (!TypeChecker.isEmpty(searchText)) {//Typechecker is a dependency
console.log('inside if block');
const res = this.state.dataDefault.filter(item => {
return item.firstName.includes(searchText) || item.lastName.includes(searchText);
});
console.log('res' + JSON.stringify(res));
this.setState({ data: res });
}
}
If I remove Typechecker dependency, how should I change the code? 如果删除Typechecker依赖项,应如何更改代码? I basically want my search function to be case case insensitive 我基本上希望我的搜索功能不区分大小写
You can use toLowerCase 您可以使用toLowerCase
if (!TypeChecker.isEmpty(searchText)) {
console.log("inside if block");
const res = this.state.dataDefault.filter(item => {
if (
item.firstName.toLowerCase().includes(searchText.toLowerCase()) ||
item.lastName.toLowerCase().includes(searchText.toLowerCase())
)
return item;
});
console.log(res);
this.setState({ data: res });
}
https://codesandbox.io/s/5yj23zmp34 https://codesandbox.io/s/5yj23zmp34
With ReactTable
you can filter out directly through the table props. 使用ReactTable
您可以直接通过表格道具过滤掉。
When you define your table accessors, you can define them in the following way: 定义表访问器时,可以通过以下方式定义它们:
{
Header: "Name",
columns: [
{
Header: "First Name",
accessor: "firstName",
filterMethod: (filter, row) =>
row[filter..toUpperCase().includes(filter.value.toUpperCase())
},
{
Header: "Last Name",
id: "lastName",
accessor: "lastName",
filterMethod: (filter, row) => row[filter.id].toUpperCase().includes(filter.value.toUpperCase())
}
]
},
As you can see the filterMethod
will do the filter for you. 如您所见, filterMethod
将为您进行过滤。
And then, you can completely remove your getMatchedList
function (so you get rid also of TypeChecker
as you want to). 然后,您可以完全删除getMatchedList
函数(这样就可以随意删除TypeChecker
了)。
The case insensitive is made checking the values using toUpperCase()
(you can even use toLowerCase()
) which will give you the case insensitive. 不区分大小写是通过使用toUpperCase()
(甚至可以使用toLowerCase()
)检查值来实现的,这将使您不区分大小写。
Here your fork: 这是你的叉子:
https://codesandbox.io/s/w0x6wwpo15 https://codesandbox.io/s/w0x6wwpo15
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.