How to enable Column Hiding in Ignite UI for Angular Grid

Dhananjay Kumar / Monday, January 14, 2019

Ignite UI for Angular Grid is fastest Angular Grid out there.  It does not only run fast, it is also very easy to use igxGrid in your application. Ignite UI for Angular Grid component class is named as igxGrid and on the template it can be used as <igx-grid></igx-grid>

Learn more about Ignite UI for Angular Grid here

In this blog post, let us learn that how Column Hiding can be enabled in IgniteUI for Angular Grid.  

Step 1:  Add Ignite UI for Angular in  Angular Project

There are three ways to add an igx-grid to an Angular project:

  1. If starting a new project, use the Ignite UI CLI to scaffold the project. You can use command line options to add the igx-grid, including dependency installation.
  2. In an existing project, you can use the Ignite UI for Angular Toolbox Extension to add an igx-grid in the project. Learn how in this blog post.
  3. You can use npm to install Ignite UI for Angular dependencies in your project. You can learn that in details here :  Step-by-Step with Images to Add Ignite UI for Angular in an Existing Angular Project

Step 2:  Add igx-grid to an Angular Project

To work with igxGrid, you need to add

  1. igxGridModule
  2. BrowserAnimationModule
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { IgxGridModule } from 'igniteui-angular';

After importing pass these two modules to imports array of Module. Next, to bind with igxGrid, let us create a local data source in the component.

getData() {
    return [
        { model: 'BMW', color: 'Black', price: '20000' },
        { model: 'Audi', color: 'Blue', price: '10000' },
        { model: 'Merc', color: 'Red', price: '25000' },
        { model: 'Toyta', color: 'Green', price: '18000' },
        { model: 'GM', color: 'Blye', price: '10000' },
    ];
}

If you want to learn how to work with REST based API and igxGrid, follow four simple steps to working with Ignite UI for Angular Grid and REST Service

 In the ngOnInit life cycle hook of the component, read data from getData() function as shown in the code listed below. We will  set data source of igxGrid to localData using property binding on the template.

ngOnInit() {
    this.localData = this.getData();
}

Add igxGrid on the template as shown in the code listing below.  We are explicitly configuring columns such that, we can work with column hiding feature.

<igx-grid #grid1 id="grid1" [data]="localData" [autoGenerate]="false">
    <igx-column field="model" header="Maker"></igx-column>
    <igx-column field="color" header="Color"></igx-column>
    <igx-column field="price" header="Price"></igx-column>
</igx-grid>

In above igxGrid;

  1. Columns are configured manually
  2. Datasource is set using [data] property binding to localData
  3. Since columns are configured manually, autoGenerate is set to false.

At this point on running application, you will get  igxGrid in Angular application as shown in image below:

Step 3:  Enable Column Hiding

Ignite UI for Angular Grid, place column hiding UI in the grid’s toolbar. You can use grid’s tool bar dropdown to show or hide columns.  So first step you need to is set showToolbar of grid to true.

<igx-grid .... [showToolbar]="true" toolbarTitle="Cars" ...>
 
</igx-grid>

After setting showToolbar to true, you need to set columnHiding to true.

<igx-grid .... [columnHiding]="true" ...>
 
</igx-grid>

By setting combination of showToolbar and columnHiding, you can work with column hiding in igxGrid. Putting everything together,  with column hiding and manual column configuration igx-grid will look like as shown in the code listing below:

<igx-grid #grid1 id="grid1" [data]="localData" [autoGenerate]="false"
            [showToolbar]="true" toolbarTitle="Cars" [columnHiding]="true">
      <igx-column field="model" header="Maker"></igx-column>
      <igx-column field="color" header="Color"></igx-column>
      <igx-column field="price" header="Price"></igx-column>
  </igx-grid>

 

At this point on running application, you will find igxGrid rendered as shown in the image below:

You can also disable a column from hiding by setting [disabledHiding] property to true. So  you can disable hiding of a columns model as shown in the code listing below:

<igx-grid #grid1 id="grid1" [data]="localData" [autoGenerate]="false"
          [showToolbar]="true" toolbarTitle="Cars" [columnHiding]="true">
    <igx-column field="model" [disableHiding]="true" header="Maker"></igx-column>
    <igx-column field="color" header="Color"></igx-column>
    <igx-column field="price" header="Price"></igx-column>
</igx-grid>

At this point on running application, you will find igxGrid rendered  with model column disabled for hiding shown in the image below:

Besides column, hiding there many IgniteUI for Angular Grid has many features, which make it, best grid for enterprise applications. Check out all features here

Now you have seen that hiding columns is as simple as setting property binding. I hope you find this post useful.

Infragistics Ignite UI for Angular