Add CheckBox to Column Header in AngularJS Grid

Created: 23 October 2015

Using templates, we can add any kind of custom content to column header in AngularJS Grid. In case of a checkbox, we will use the IntegralUI CheckBox directive, which comes with built-in functionality and customizable appearance.

Grid directive is part of IntegralUI Studio for Weba suite of UI Components for development of web apps

As above demonstration shows, the header of first column displays a checkbox which when clicked, it will change the value of all checkboxes in column cells. In addition, the footer of second column displays how many rows are checked.

Create a Template for CheckBox Column

In our case, the column header only will display one checkbox. Therefore, we need only single instance of CheckBox directive:

As it can be seen from code, the syntax is very simple: we only have a box without text and a handler for an event that is fired whenever a checkbox changes its value. See more below on how this event is handled.

Next we need to link this template with the grid column. We will use the template identifier as a value of headerTemplate field of column object:

Adding CheckBox to Column Cells

The above template will only show a checkbox in column header, but cells will remain the same, showing only text value. In order to add a checkbox to column cells, we need to set the editorType field of column object to checkbox':

Handling CheckBox Events

In order to handle the checkedChanged event from CheckBox directive inside a template, we need to add an event handler function inside the column object. This will make sure the event and the function are linked, during compilation of the template.

Whenever the checkbox in column header is clicked, checkbox in all column cells will also change their value to match the value of the header checkbox. This enables to mark all rows as checked or unchecked, as quickly as possible.

Template for Column Footer

Finally, template for the column footer contains a button and a label which will displays the total number of all and checked rows. The body of this template looks like this:

Conclusion

Templates allow us to add custom content to column header. In case of adding a checkbox is very simple, we can either use the CheckBox directive or even standard input element, the result is the same.

Tab content switching

Newsletter

Sign-up to our newsletter and you will receive news on upcoming events, latest articles, samples and special offers.

Name:

Email: *

*By checking this box, I agree to receive a newsletter from Lidor Systems in accordance with the Privacy Policy. I understand that I can unsubscribe from these communications at any time by clicking on the unsubscribe link in all emails.