Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I hide certain rows in a ui-grid based on its values?

I have a simple UI grid with these options:

$scope.transactionGrid = {
    enableSorting : true,
    enableColumnResize : true,
    enableScrollbars : true,
    enablePaginationControls : false,
    minRowsToShow : 6,
    onRegisterApi : function(gridApi) {
        $scope.gridEventsApi = gridApi;
    }
};

I want to hide rows which have a specific value, deleted: "y".

$scope.transactionGrid.data = [
    { Name: "First", deleted: "y" },
    { Name: "Second", deleted: "y" },
    { Name: "Third", deleted: "n" },
    { Name: "Fourth", deleted: "n" }
];

Without actually changing the data, can it be filtered out from rows?

like image 323
josh_boaz Avatar asked Mar 03 '16 21:03

josh_boaz


2 Answers

One way is to adjust the row-repeater-template to check for some row-specific value and make the row show/hide that way. I created a Plunkr showcasing a possible solution.

First you need to create your row-value-checker-function:

appScopeProvider: {
  showRow: function(row) {
    return row.deleted !== 'y';
  }
},

Then you adjust their template by adding that check to their row-repeater

$templateCache.put('ui-grid/uiGridViewport',  
  ...
  ng-if=\"grid.appScope.showRow(row.entity)\"
  ...
}
like image 171
CMR Avatar answered Sep 28 '22 19:09

CMR


I know you specifically said "without actually changing the data", but assigning a filtered dataset to the grid would not change the dataset, just the data for the grid. Also it might be a relevant and valid solution for other cases like this.

I forked CMR's Plunk to demonstrate this: http://plnkr.co/edit/NntwWb?p=preview

The key part is just adding a filter when assigning the dataset:

$scope.gridOptions = {
    data: $scope.myData.filter(function(row) {
        return row.deleted !== "y";
    })
};
like image 29
Flygenring Avatar answered Sep 28 '22 17:09

Flygenring