angular ui grid row template color based on condition

31,730

Solution 1

I think in both ng-grid and ui-grid, you can use

cellTemplate: '<div style="height: 100%" ng-class="{red: row.getProperty(\'viewed\') < 1}"><div ng-style="{\'cursor\': row.cursor}" ng-repeat="col in renderedColumns" ng-class="col.colIndex()" class="ngCell ">' +
         '<div class="ngVerticalBar" ng-style="{height: rowHeight}" ng-class="{ngVerticalBarVisible: !$last}"> </div>' +
        '<div ng-cell></div>' +
        '</div></div>',

Solution 2

I don't see the "viewed" as being a property of row, according the ui-grid api: http://ui-grid.info/docs/#/api/ui.grid.class:GridRow. But, if you wanted to evaluate a property in your object (assuming your data is an array of objects) it would look something like this.

You'll need this in your css to override default row coloring

.ui-grid-row .ui-grid-cell {
  background-color: inherit !important;
}

Your styles:

.my-style-1 {
  background-color: #ff0000 !important;
}
.my-style-2 {
  background-color: #ffffff !important;
}
.my-style-3 {
  background-color: #0000ff !important;
}

The rowTemplate:

rowTemplate: '<div ng-class="{\'my-style-1\':row.entity.Field1===1,  \'my-style-2\':row.entity.Field1===2,  \'my-style-2\':row.entity.Field1===3}" <div ng-repeat="col in colContainer.renderedColumns track by col.colDef.name"  class="ui-grid-cell" ui-grid-cell></div></div>'

//example data
data:[{'Field1': 1, 'Field2': 'abc', 'Field3': 'def'},
        {'Field1': 2, 'Field2': 'hij', 'Field3': 'klm'},
        {'Field1': 3, 'Field2': 'nop', 'Field3': 'qrs'}];

Solution 3

Instead of cellTemplate mentioned in other anwser, you can also use cellClass:

cellClass: function (grid, row) {
  return row.entity.age < 18 ? 'young' : 'old';
};
Share:
31,730
Riyas TK
Author by

Riyas TK

A Full Stack Developer with five years of experience in Node.js, Javascript, React, Angular, React Native, MongoDB, PostgreSQL, AWS, ElasticSearch, Etc.

Updated on July 19, 2020

Comments

  • Riyas TK
    Riyas TK almost 4 years

    I need to change row color of angular ui grid based on some condition. The target is achieved in ng-grid as shown

     rowTemplate: '<div style="height: 100%" ng-class="{red: row.getProperty(\'viewed\') < 1}"><div ng-style="{ \'cursor\': row.cursor }" ng-repeat="col in renderedColumns" ng-class="col.colIndex()" class="ngCell ">' +
                 '<div class="ngVerticalBar" ng-style="{height: rowHeight}" ng-class="{ ngVerticalBarVisible: !$last }"> </div>' +
                '<div ng-cell></div>' +
                '</div></div>',
    

    how to achieve the same in angular ui grid

  • Mike
    Mike over 7 years
    Note this has now changed: In 2.x you would use row.getProperty(col.field) within a cellTemplate to get the value of a cell. In 3.0 this has changed to grid.getCellValue(row, col).