Currently I have external filters for my ui-grid but have enableFiltering: true
so I can still utilize the filter
property on my columns (maybe this is wrong, I don't know, but the ui-grid filtering API works well so I don't want to screw with it).
The problem with this is that the enableFiltering
property also controls the visibility of the Clear All Filters
menu option for the grid.
I've tried using uiGridGridMenuService.removeFromGridMenu(grid, id)
to unregister the menu option, but this won't work when the grid is first rendered because the grid.gridMenuScope.menuItems
property is not even defined until the user clicks on the grid menu button for the first time. Additionally, this menu item is assigned an id of menuitem-0
instead of some static/unique property so even if it the removal did work - it's not a safe way to remove this. Finally, based on the code below and running the debugger, the getMenuItems
call is executed each time the grid menu button is clicked so the clear filter option is added every single time.
It doesn't look like there's any way to 1) prevent this menu option from appearing, short of not using the grid's filtering API, or 2) overriding the menu action.
Here's the directive code for the grid menu button (note the $broadcast
event, maybe I can use that? But that's also fragile/hacky):
.directive('uiGridMenuButton', ['gridUtil', 'uiGridConstants', 'uiGridGridMenuService', 'i18nService',
function (gridUtil, uiGridConstants, uiGridGridMenuService, i18nService) {
return {
priority: 0,
scope: true,
require: ['^uiGrid'],
templateUrl: 'ui-grid/ui-grid-menu-button',
replace: true,
link: function ($scope, $elm, $attrs, controllers) {
var uiGridCtrl = controllers[0];
// For the aria label
$scope.i18n = {
aria: i18nService.getSafeText('gridMenu.aria')
};
uiGridGridMenuService.initialize($scope, uiGridCtrl.grid);
$scope.shown = false;
$scope.toggleMenu = function () {
if ( $scope.shown ){
$scope.$broadcast('hide-menu');
$scope.shown = false;
} else {
$scope.menuItems = uiGridGridMenuService.getMenuItems( $scope );
$scope.$broadcast('show-menu');
$scope.shown = true;
}
};
$scope.$on('menu-hidden', function() {
$scope.shown = false;
gridUtil.focus.bySelector($elm, '.ui-grid-icon-container');
});
}
};
}]);
And here's the getMenuItems
service method:
getMenuItems: function( $scope ) {
var menuItems = [
// this is where we add any menu items we want to always include
];
if ( $scope.grid.options.gridMenuCustomItems ){
if ( !angular.isArray( $scope.grid.options.gridMenuCustomItems ) ){
gridUtil.logError( 'gridOptions.gridMenuCustomItems must be an array, and is not');
} else {
menuItems = menuItems.concat( $scope.grid.options.gridMenuCustomItems );
}
}
var clearFilters = [{
title: i18nService.getSafeText('gridMenu.clearAllFilters'),
action: function ($event) {
$scope.grid.clearAllFilters(undefined, true, undefined);
},
shown: function() {
return $scope.grid.options.enableFiltering;
},
order: 100
}];
menuItems = menuItems.concat( clearFilters );
menuItems = menuItems.concat( $scope.registeredMenuItems );
if ( $scope.grid.options.gridMenuShowHideColumns !== false ){
menuItems = menuItems.concat( service.showHideColumns( $scope ) );
}
menuItems.sort(function(a, b){
return a.order - b.order;
});
return menuItems;
}