Reputation: 1251
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
Upvotes: 5
Views: 32706
Reputation: 43
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?
Upvotes: 0
Reputation: 3195
Instead of cellTemplate
mentioned in other anwser, you can also use cellClass
:
cellClass: function (grid, row) {
return row.entity.age < 18 ? 'young' : 'old';
};
Upvotes: 6
Reputation: 8623
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>',
Upvotes: 6
Reputation: 258
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'}];
Upvotes: 9