Anthony Don
Anthony Don

Reputation: 157

How to switch off filtering for a certain column in Kendo grid after it's loaded?

I have a kendo grid created with the usual MVC Razor syntax.

For example,

    @(Html.Kendo().Grid<exampleViewModel>()
    .Name("ExampleGridName")
    .DataSource(ds => ds
        .Ajax()
        .Read(read => read.Action("ExampleMethod", "ExampleClass"))
    .Columns(c =>
    {
        c.Bound(m => m.Id);
        c.Bound(m => m.FirstName);
        c.Bound(m => m.LastName);
    });
    .Filterable())

My question: Is there a way to switch off filtering for a column like "LastName" after the grid is loaded using jquery?

I was successful iterating through the columns and identify the one I need filtering switched off. I tried setting the filterable property to false but it didn't work. Maybe because it looks like an object type.

I came across this issue as I was trying to reuse a grid but had to apply different filters during load for the pages. The filters worked but when the filter was cleared, the grid was totally reset. When I have the filter switched off at run time, the reset would reset but keep the filters applied during load.

Upvotes: 0

Views: 1465

Answers (1)

David Shorthose
David Shorthose

Reputation: 4497

check out this dojo I created based of Telerik's own filter grid demo and let me know if this is along the right track for you filter demo

code that I added to the demo.

  $(document).ready(function () {
  var preventFilter = 'City'

  console.log($('th[role="columnheader"]').length);
  $('th[role="columnheader"]').filter('[data-role="filtermenu"]').each(function (item) {


      if ($(this).attr("data-field") == preventFilter) {
          console.log('matched')
          $(this).attr("data-role", "");
          $(this).find("a").remove();
          $(this).removeClass('k-filterable');
      }

  });

}); just to be clear it removes the filter link from the City column. which was added when the grid was initialized.

This works based on the filter item being in the header if you have either the menu or the row features enabled then you will need to change the code appropriately.

Glad this helped.

Upvotes: 1

Related Questions