angular ui grid row template color based on condition
Asked Answered
P

4

5

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

Peavy answered 3/2, 2015 at 2:45 Comment(1)
What is some condition? Does it come from the $scope? Then look out for external scope/appScope. Described here: ui-grid.info/docs/#/tutorial/305_appScope This whole (awesome) project is in the works right now, so please tell us which version you use.Outside
S
6

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>',
Strati answered 3/2, 2015 at 3:14 Comment(1)
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).Dismuke
I
9

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'}];
Invent answered 5/2, 2015 at 18:21 Comment(0)
S
6

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>',
Strati answered 3/2, 2015 at 3:14 Comment(1)
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).Dismuke
S
6

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

cellClass: function (grid, row) {
  return row.entity.age < 18 ? 'young' : 'old';
};
Strumpet answered 1/10, 2015 at 7:21 Comment(0)
G
0

For my UI-Grid I've created conditional formatting with the following row template in the $scope.gridOptions object:

rowTemplate: '<div ng-class="{\'recruiter-row-active\':row.entity.activePositions!=0, ' +
    '\'recruiter-row-passive\':(row.entity.activePositions==0 && row.entity.passivePositions !=0),' +
    '\'recruiter-row-free\':(row.entity.activePositions==0 && row.entity.passivePositions==0)}">' +
    '<div ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.colDef.name" ' +
    'class="ui-grid-cell" ng-class="{ \'ui-grid-row-header-cell\': col.isRowHeader }" ui-grid-cell></div></div>'

The classes look like this:

.ui-grid-row .recruiter-row-active {
  background-color: #ff816b !important;
}
.ui-grid-row .recruiter-row-passive {
  background-color: #fcff9d !important;
}
.ui-grid-row .recruiter-row-free {
  background-color: #70cc79 !important;
}

The class for the html row in question is "ui-grid-row" and "ng-scope" and the parent element has class "ui-grid-canvas"

I was able to get my conditional formatting to work when I also implemented a

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

However I don't want to affect the other grids in my web app.

How would I get my conditional row formatting to override the default?

Gerius answered 18/7, 2018 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.