Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default filter for Kendo UI Grid

I have a Kendo UI grid that is rendered with javaScript. I want the string columns to have a single option ("Contains") and without the second filter. So far so good, I wrote

        $("#MyGrid").kendoGrid({
            // other bits of configuration here
            filterable: {
                extra:false, 
                operators: {
                    string:{ contains: "Contains"}
                }
            },
            // more bits of configuration here
        });

As part of the definition of the grid. And the result looks good-ish (I only have one option, so the drop down is redundant).

Filter as I defined

However, regardless of this, the filter still performs the equals operation rather than the contains operation (which is the only one available to it).

I've spent a while trying to figure this out and I keep going around in circles because the code I found either does not work, or doesn't make sense, or both.

Can anyone tell me how to default the filter to "Contains" and not "Is Equal To"?

like image 411
Colin Mackay Avatar asked Jan 14 '13 15:01

Colin Mackay


3 Answers

I had the same problem and I got it, that it needs the .Clear() option!

I am building my Grid with the MVC Wrapper in Razor:

@(Html.Kendo().Grid<LocationViewModel>()
    .Name("locationGrid")
    // other bits of configuration here
    .ColumnMenu()
    .Filterable(f => f.Operators(o => o.ForString(s => s
        .Clear()
        .Contains("Contains")
        .DoesNotContain("Does not contain")
        .EndsWith("Ends with")
    )))
    // other bits of configuration here
)

Summary:

  1. .Clear() is needed!
  2. Sorting is necessary! Put .Contains() first after .Clear() then the default is Contains!

Additional Info: I am using Kendo UI 2013.1.514

like image 148
florian.isopp Avatar answered Oct 20 '22 07:10

florian.isopp


Try to update to latest internal build. Version later than 2012.3.1304 should contain the fix.

like image 24
Petur Subev Avatar answered Oct 20 '22 06:10

Petur Subev


The best way to change the default operator for all of the instances:

kendo.ui.FilterMenu.prototype.options.operators =           
  $.extend(kendo.ui.FilterMenu.prototype.options.operators, {
  string: {
      contains: "Contains",
      startswith: "Starts with",
      eq: "Is equal to",
      neq: "Is not equal to",
      doesnotcontain: "Does not contain",
      endswith: "Ends with"
  }
});

and the complete script:

kendo.ui.FilterMenu.prototype.options.operators =           
  $.extend(kendo.ui.FilterMenu.prototype.options.operators, {

/* FILTER MENU OPERATORS (for each supported data type) 
 ****************************************************************************/   
  string: {
      contains: "Contains",
      startswith: "Starts with",
      eq: "Is equal to",
      neq: "Is not equal to",
      doesnotcontain: "Does not contain",
      endswith: "Ends with"
  },
  number: {
      eq: "Is equal to",
      neq: "Is not equal to",
      gte: "Is greater than or equal to",
      gt: "Is greater than",
      lte: "Is less than or equal to",
      lt: "Is less than"
  },
  date: {
      eq: "Is equal to",
      neq: "Is not equal to",
      gte: "Is after or equal to",
      gt: "Is after",
      lte: "Is before or equal to",
      lt: "Is before"
  },
  enums: {
      eq: "Is equal to",
      neq: "Is not equal to"
  }
 /***************************************************************************/   
});
like image 31
Salar Avatar answered Oct 20 '22 08:10

Salar