简体   繁体   English

React.js 中搜索输入的过滤器

[英]A filter for a search input in React.js

I am trying to make a filter for a Search Input with React.我正在尝试使用 React 为搜索输入制作过滤器。 I have a fournisseurs array with an object inside, like this:我有一个fournisseurs数组,里面有一个 object,如下所示:

fournisseurs: [
    {
      'id'              : '0',
      'codeFournisseur' : '2222222',
      'categorie'       : 'sdfgsdfg',
      'statusJuridique' : 'sdfgdfhs',
      'raisonSociale'   : 'sdfhdhs',
      'typeReglement'   : 'sdhgdfhdf',
      'delaiReglement'  : 'sdhfgdhs',
      'franco'          : 'sdfhsdh',
      'fraisDePort'     : 'sdhhsdhf',
      'adresse'         : 'sdfhsdfh',
      'codePostal'      : 'sfdgsdfgsdfg',
      'ville'           : 'sdgsd',
      'pays'            : 'France',
      'telephone'       : '+333333333333',
      'mail'            : 'dfxgdf@sdhgdfh.fr',
      'siteInternet'    : 'http://www.sdfgsdfhs.fr',
      'tvaIntracomm'    : 'sdfhsdghsdgh'
    }
]

The problem is that I can't make a search on the whole object.问题是我无法对整个 object 进行搜索。 I can only search on one element (raisonSociale here) of the object such as:我只能搜索 object 的一个元素(此处为 raisonSociale),例如:

useEffect(() => {
        setData(searchText.length === 0 ? fournisseurs : _.filter(fournisseurs, fournisseur => fournisseur.raisonSociale.toLowerCase().includes(searchText.toLowerCase())))
    }, [fournisseurs, searchText]);

Would someone guide me if it would be possible to use a spread operator in the filter method or something similar to retrieve all elements of the object from the search?如果可以在过滤方法中使用扩展运算符或类似的方法从搜索中检索 object 的所有元素,有人会指导我吗?

Try this, I have added two new keys which you can also search试试这个,我添加了两个新键,你也可以搜索

useEffect(() => {
    setData(searchText.length === 0 ? fournisseurs : {
       _.filter(fournisseurs, fournisseur => 
         return fournisseur.raisonSociale.toLowerCase().includes(searchText.toLowerCase()) ||
                fournisseur.typeReglement.toLowerCase().includes(searchText.toLowerCase()) ||
                fournisseur.delaiReglement.toLowerCase().includes(searchText.toLowerCase())
      )
    })
}, [fournisseurs, searchText]);

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

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