Search code examples
kendo-uikendo-grid

How to order a Kendo Grid checkbox column filter when multi and columnMenu are both true?


Using a Kendo UI grid, you can set the column as "filterable: { multi: true}", which generates a nice checkbox list in the filter instead of the default text box and equality operator template. The problem, is that by default the items in the checkbox list are ordered with the dataset, which itself is probably ordered by some other field.

Kendo docs explains how to filter a column when the "filterable: { multi: true}", but it only works when columnMenu is false. Column menu is another option that adds a nice menu to all the columns.

So, how do you order a Kendo Grid checkbox column filter when multi and columnMenu are both true?


Solution

  • I came here looking for an answer to this question also, and I tried Bill's solution and it works great...as long as you don't have any locked columns.

    Bill's solution, based on the Kendo UI docs:

    columnMenuInit: function (e) {
        var multiCheck = this.thead.find("[data-field=" + e.field + "]").data("kendoColumnMenu").filterMenu.checkBoxAll;
        if (multiCheck) {
            var filterMultiCheck = this.thead.find("[data-field=" + e.field + "]").data("kendoColumnMenu").filterMenu
            filterMultiCheck.container.empty();
            filterMultiCheck.checkSource.sort({ field: e.field, dir: "asc" });
            filterMultiCheck.checkSource.data(filterMultiCheck.checkSource.view().toJSON());
            filterMultiCheck.createCheckBoxes();
    }
    

    },

    If you initialize your grid with locked columns, this solution will work for you:

    columnMenuInit: function (e) {
        let lockedColumn = false;
        let columnMenu = this.thead.find("[data-field=" + e.field + "]").data("kendoColumnMenu");
    
        // if columnMenu is falsy this may be a locked column, in which case we need to target the column menu from the lockedHeader property
        if (!columnMenu) {
            columnMenu = this.lockedHeader.find("[data-field=" + e.field + "]").data("kendoColumnMenu");
            lockedColumn = true;
        }
    
        if (columnMenu) {
            const multiCheck = columnMenu.filterMenu.checkBoxAll;
    
            // if this column uses multi-check filtering, sort the filter options in ascending alphabetical order
            if (multiCheck) {
                const filterMultiCheck = lockedColumn ? this.lockedHeader.find("[data-field=" + e.field + "]").data("kendoColumnMenu").filterMenu : this.thead.find("[data-field=" + e.field + "]").data("kendoColumnMenu").filterMenu;
                filterMultiCheck.container.empty();
                filterMultiCheck.checkSource.sort({ field: e.field, dir: "asc" });
                filterMultiCheck.checkSource.data(filterMultiCheck.checkSource.view().toJSON());
                filterMultiCheck.createCheckBoxes();
            }
        }
    }