Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular UI Grid filter only applies to beginning of cell value

I'm using an Angular UI Grid in my app. I set enableFiltering to true in the options which made some filter boxes show up above my columns. This is great, but the filters don't work exactly as desired.

If a cell contains the text "I like pizza" and I type "I like", that cell's row is shown as a match. I would also think that if I type "pizza", the "I like pizza" cell/row should show up, but that's not the case.

How can I get the filters to allow searching anywhere in the text, not just from the beginning?

like image 380
Jason Swett Avatar asked Jun 03 '15 19:06

Jason Swett


2 Answers

You can use filter: {condition: uiGridConstants.filter.CONTAINS} in the column definition to allow that column to search anywhere in the text.

Here's a sample column definition with this in place:

columnDefs: [
  // default
  { field: 'name',
    filter: {condition: uiGridConstants.filter.CONTAINS} },
  ...
like image 167
Brad Barber Avatar answered Nov 15 '22 09:11

Brad Barber


You can pass a custom filter object that takes a condition property, which can be a function that takes searchTerm and cellValue. Will display if the function returns true. Plunkr

  { field: 'name', filter: {
    condition: function(searchTerm, cellValue) {
      return cellValue.indexOf(searchTerm) > -1
    }
  }}
like image 33
azium Avatar answered Nov 15 '22 11:11

azium