Edit this page

Hide Columns on Clicking the Headers and Restore Them on Clicking a Button

Your AngularJS project might require you to hide Grid columns and sub-columns by clicking the column headers and, then, restore the hidden columns by clicking a button.

The following example demonstrates how to achieve this behavior.

Example
<div id="example" ng-app="KendoDemos">
      <div ng-controller="MyCtrl">
        <div kendo-grid="grid" k-options="gridOptions"></div>
        <br />
        <button kendo-button k-click="showColumns">Show hidden columns</button>
      </div>
    </div>
    <script>
      function getColumnByTitle(columns, title){
        return columns.reduce(function(acc, curr) {          
          if (acc) {
            return acc;
          }

          if (curr.title === title) {
            return curr;
          }

          if (curr.columns && curr.columns.length) {
            return getColumnByTitle(curr.columns, title);
          }          

          return null;          
        }, null);      
      }

      function showHiddenColumns(columns, grid){
        columns.forEach(function(col){
          if(!col.columns){
            if(col.hidden){
              grid.showColumn(col);
            }
          } else {
            showHiddenColumns(col.columns, grid);
          }  
        });

      }

      angular.module("KendoDemos", [ "kendo.directives" ])
        .controller("MyCtrl", function($scope){
        $scope.showColumns = function(){
          var columns = $scope.grid.columns;
          showHiddenColumns(columns, $scope.grid);
        };

        $scope.gridOptions = {
          dataSource: {
            type: "odata",
            transport: {
              read: "//demos.telerik.com/kendo-ui/service/Northwind.svc/Customers"
            },
            pageSize: 20
          },
          height: 550,
          sortable: true,
          pageable: true,
          columns: [{
            field: "CompanyName",
            title: "Company Name",
            width: 420
          },
                    {
                      title: "Contact Info",
                      columns: [{
                        field: "ContactTitle",
                        title: "Contact Title",
                        width: 200
                      },{
                        field: "ContactName",
                        title: "Contact Name",
                        width: 200
                      },{
                        title: "Location",
                        columns: [ {
                          field: "Country",
                          width: 200
                        },{
                          field: "City",
                          width: 200
                        }]
                      },{
                        field: "Phone",
                        title: "Phone"
                      }]
                    }]
        };

        $scope.$on("kendoRendered", function(e) {
          $scope.grid.thead.on('click', 'th', function(ev){
            var columnOrFieldTitle = $(ev.target).closest('th').attr('data-field') || $(ev.target).closest('th').attr('data-title');

            if($(ev.target).closest('th').attr('data-field')){
              $scope.grid.hideColumn(columnOrFieldTitle);
            } else {
              var columnByColumnTitle = getColumnByTitle($scope.grid.columns, columnOrFieldTitle);
              $scope.grid.hideColumn(columnByColumnTitle);
            }
          });
        });
      });
    </script>

See Also

For more runnable examples on the Kendo UI Grid, browse its How To documentation folder.

Is this article helpful? Yes / No
Thank you for your feedback!

Give article feedback

Tell us how we can improve this article

close
Dummy