Angular-ui-grid enable filtering from grid menu

In Angular ui-grid it is possible to configure the grid with filtering using the enableFiltering option.

vm.gridOptions = {enableFiltering: true};

I wanted to allow the user to hide/show the filter clicking a button.
There is an example of that in http://ui-grid.info/docs/#/tutorial/103_filtering, but I wanted to add that action to the grid menu itself.

angular-ui-grid-enable-filtering-grid-menu-01

angular-ui-grid-enable-filtering-grid-menu-02

Here is the code of my solution:

            vm.gridOptions.gridMenuCustomItems = [
                {
                    title: 'Hide filter',
                    icon: 'glyphicons filter failure',
                    leaveOpen: true,
                    order: 0,
                    action: function ($event) {
                        this.grid.options.enableFiltering = !this.grid.options.enableFiltering;
                        this.grid.api.core.notifyDataChange(uiGridConstants.dataChange.COLUMN);
                    },
                    shown: function () {
                        return this.grid.options.enableFiltering;
                    }
                },
                {
                    title: 'Show filter',
                    icon: 'glyphicons filter success',
                    leaveOpen: true,
                    order: 0,
                    action: function ($event) {
                        this.grid.options.enableFiltering = !this.grid.options.enableFiltering;
                        this.grid.api.core.notifyDataChange(uiGridConstants.dataChange.COLUMN);
                    },
                    shown: function () {
                        return !this.grid.options.enableFiltering;
                    }
                }
            ];

You can see that doesn’t need to expose the grid to scope in a onRegisterApi block.

Documentation:
http://ui-grid.info/docs/#/tutorial/103_filtering
http://ui-grid.info/docs/#/tutorial/121_grid_menu

Advertisements

2 thoughts on “Angular-ui-grid enable filtering from grid menu

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s