khemt khemt - 4 months ago 128
AngularJS Question

How to set filter in columnDefs property in angular UI grid

I have angular ui grid in application and I want a field to be hide if any field value is null or empty.


$scope.gridOptions = {
paginationPageSize: 10,
columnDefs: [
{ displayName: 'First Name', field: 'firstName', headerCellClass: "GridHeader" },
{ displayName: 'Last Name', field: 'lastName', headerCellClass: "GridHeader" },
{ displayName: 'Active Status', field: 'activeStatus', headerCellClass: "GridHeader" },
{ displayName: 'RoleName', field: 'roleName', headerCellClass: "GridHeader" },
]
};


How to put a filter in field. Suppose if my rolename in above code is empty/null then hide the role field from grid.

Any help and suggestion highly appreciated. Thanks in advance:)

Answer

Yes, I fixed it.

var result = [];
var gridValue= [
      { displayName: 'First Name', field: 'firstName', headerCellClass: "GridHeader" },
      { displayName: 'Last Name', field: 'lastName', headerCellClass: "GridHeader" },
      { displayName: 'Active Status', field: 'activeStatus', headerCellClass: "GridHeader" },
      { displayName: 'RoleName', field: 'roleName', headerCellClass: "GridHeader" },
    ]; 

in the response I checked for null value.

if(response.data != null)
{
    if (response.data.roleName === null) {
        result = gridValue.slice(0, 3);
    }
    else {
        result = gridValue;
    }
    $scope.gridOptions = {
        paginationPageSize: 10,
        columnDefs: result,
     };
}

this helped me :)

Comments