5

默认情况下 Ant 设计的 Tree select 按值搜索,有没有按标题搜索的方法?我尝试过使用onSearch函数,但它不会改变 Tree Select 的任何行为

4

3 回答 3

7

您可以按标题或 treeData 项的任何其他字段进行搜索,为此目的有一个回调道具 filterTreeNode

例子:

<TreeSelect
 treeData={data}
 filterTreeNode={(search, item) => {
        return item.title.toLowerCase().indexOf(search.toLowerCase()) >= 0;
      }}
/>
于 2020-10-14T07:53:53.380 回答
6

Just to elaborate on the answer provided by @Oliver, you can add the following props:

<TreeSelect

  showSearch
  treeNodeFilterProp='title'
  treeData={treeData}
  ...

/>

You can use the filterTreeNode prop to provide a custom filter function, but for standard functionality it should not be needed.

于 2020-11-27T14:11:31.190 回答
3

There's actually a better and simpler way to do it by providing treeNodeFilterProp, which is directly used for filtering. Its default value is 'value' and you can simply change it to 'title' to achieve the desired behaviour.

于 2020-10-28T09:40:46.033 回答