Kendo Grid - Change default filter to Contains

I wanted the default string search to be the Contains filter rather than the EQ filter. It can be changed in two ways.
The order of the operators in the options determines the default as it is the first in the list that is used.

So to change the order and hence default you can specify it in the grid declaration or in code.

When declaring the grid modify the filter to specify the list of the string filter like...

.Filterable(f => f.Extra(true)
 .Operators(o => o.ForString(s =>s.Clear()
  .Contains("Contains")
  .DoesNotContain("Does not contain")
  .IsEqualTo("Is equal to")
  .IsNotEqualTo("Is not equal to")
  .StartsWith("Starts with")
  .EndsWith("Ends with ")
)))
        

or you can in code just place the contains to the front of the list. I've got in my library

SetContainsAsDefaultStringFilter: function (){  
  var stringOps = kendo.ui.FilterMenu.prototype.options.operators.string;
  var containsOp = { contains: "Contains" };
  kendo.ui.FilterMenu.prototype.options.operators.string = $.extend({}, containsOp, stringOps);
},

I just call it when required.
Note: Needs to be called before the grid is rendered.

The Newer GridFilterMode Row mode

The new filter row mode on the Kendo grids is cool but it has broken the default settings above.
If you have the mode enabled in your MVC Kendo grid like

  .Filterable(filtering => filtering.Mode(GridFilterMode.Row).Enabled(true))

After a bit of digging in their source I found the defaults are stored in the FilterCell rather than FilterMenu so you need to adjust as follows

var cellmenu = kendo.ui.FilterCell.prototype.options;  
stringOps = cellmenu.operators.string;  
cellmenu.operators.string = $.extend({}, containsOp, stringOps);  
cellmenu.messages.isFalse = "F";  
cellmenu.messages.isTrue = "T";  

The last two isFalse/isTrue is just a shorting the bool radio box otherwise I find it takes up far to much space.

References

http://www.telerik.com/forums/grid-column-filter-default-method
http://stackoverflow.com/questions/14321414/set-default-filter-for-kendo-ui-grid


Above referring to version
Telerik Kendo UI Professional Q2 2016
Telerik UI for ASP.NET MVC Q2 2016


comments powered by Disqus