Edit this page

Sort Displayed Columns Menu Items

Environment

Product Progress Kendo UI Grid
Created with version 2018.1.117

Description

My Grid has a column menu, which contains displayed columns submenu.

How can I sort the items of the displayed columns submenu?

Solution

A possible solution is to sort the li elements of the column menu within the columnMenuInit event handler.

    <div id="example">
      <div id="grid"></div>

      <script>
        $(document).ready(function() {
          $("#grid").kendoGrid({
            dataSource: {
              type: "odata",
              transport: {
                read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Orders"
              },
              schema: {
                model: {
                  fields: {
                    OrderID: { type: "number" },
                    ShipCountry: { type: "string" },
                    ShipName: { type: "string" },
                    ShipAddress: { type: "string" }                                        
                  }
                }
              },
              pageSize: 30,
              serverPaging: true,
              serverFiltering: true,
              serverSorting: true
            },
            height: 550,
            sortable: true,
            filterable: true,
            columnMenu: true,
            pageable: true,
            columns: [ {
              field: "OrderID",
              title: "Order ID",
              width: 120
            }, {
              field: "ShipCountry",
              title: "Ship Country"
            }, {
              field: "ShipName",
              title: "Ship Name"
            },  {
              field: "ShipAddress",
              filterable: false
            }
                     ],
            columnMenuInit:function(e){
              var list= e.container.find('.k-columns-item ul')
              var items = list.find('li');

              //list.html('');

              items.each(function(x,y){
                $(y).removeClass('k-first k-last')
              }) 

              items.sort(function(a,b){
                a = $(a);
                b = $(b);

                var firstText = a.find('input[data-field]').attr('data-field');
                var secondText = b.find('input[data-field]').attr('data-field');


                return ((firstText < secondText) ? -1 : ((firstText > secondText) ? 1 : 0));

              })

              items.first().addClass('k-first');
              items.last().addClass('k-last');


              items.each(function(y,x){
                list.append($(x));
              })
            }
          });
        });
      </script>
    </div>
Is this article helpful? Yes / No
Thank you for your feedback!

Give article feedback

Tell us how we can improve this article

close
Dummy