Search function of AgSetColumnFilter customization

旧时模样 提交于 2021-01-12 06:10:28

问题


I have this project in Plunker https://plnkr.co/edit/3yuS4UKvkrK75Zy2

which shows a table with its column. What I need is building a search lookup function instead of the default filter function, so that ON The List of Values in Mini Filter window and NOT the whole table:

If typing the full name Michael then the table will be filtered by Michael, OR if I type the phone number then the name of Michael will be filtered by Michael. in other words.

There is a mapping array:

valueFilter = [{
    key: 'michael',
    value: 'michael',
    tokens: ['michael', 'mich', 'ael', '+0912312321'],
  },
  {
    key: 'natalie',
    value: 'natalie',
    tokens: ['natalie', 'lie', '+091212'],
  }];

so instead of the default search function, I want that the search will be looked up in tokens of mapping object.

I dont what to change the values of the filter to filter based tokens, and i also dont want to change the columns values to column based token, but i only need that if the input of the user in the search field is inside the tokens then returns the key of the token and show it to the user.


回答1:


Got a working example here. Note that I'm not very familiar with Angular, so the code might seem weird.

I defined a custom filter, called CustomSetFilter, in which the text input functions as a search field for the tokens of each checkbox as defined in filterData. If the input is empty, all checkboxes appear. If not, only checkboxes that have a token equal to the input's value (ignoring case) will show up. This can be seen in the onChange() function.




回答2:


I don't know if AgSetFilter is required, but we can do it with TextFilter

1)First of all, we should create a column based on tokens

2)then we need to format the value for the good name in cell because we had a array

3)and write a custom filter options

this.columnDefs = [
  {
    field: 'tokens',
    filter: 'agTextColumnFilter',
    valueFormatter: p => p.value[0],
    filterParams: {
      filterOptions: [{
      displayKey: 'ByToken',
      displayName: 'ByToken',
      test: function (filterValue, cellValue) {
        const array = cellValue.split(',')
        return array.includes(filterValue);
      },
      hideFilterInput: false
      }]
    },
  }
];

result:

WORKING EXAMPLE:

https://plnkr.co/edit/7mtR5Kq3Hyqup0Ta



来源:https://stackoverflow.com/questions/65450872/search-function-of-agsetcolumnfilter-customization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!