Add a class to ng-grid row

robert.bo.roth picture robert.bo.roth · Oct 29, 2013 · Viewed 11.3k times · Source

I'm using ng-grid to display a collection of files that are being uploaded (each file has its own row).

If one/any of the files fails to upload, I'd like to modify that row and put a class on it to show that it failed to upload.

How would I go about adding a class to an entire row?

Answer

pmoule picture pmoule · Oct 29, 2013

You have to use a row template. In this template you can use ng-class and dynamically assign a CSS class by databinding.

A simple example:

HTML

<body ng-controller="MyCtrl">
    <div class="grid" ng-grid="gridOptions"></div>
</body>

JavaScript

var app = angular.module('myApp', ['ngGrid']);

app.controller('MyCtrl', function($scope) {
  $scope.fileOneUploaded = true;
  $scope.fileTwoUploaded = false;

  $scope.gridData = [{fileName: 'File 1', size: 1000, isUploaded: $scope.fileOneUploaded },
                {fileName: 'File 2', size: 2000, isUploaded: $scope.fileTwoUploaded }];
  $scope.gridOptions = { 
    data: 'gridData',
    rowTemplate: '<div style="height: 100%" ng-class="{red: !row.getProperty(\'isUploaded\')}">' + 
                    '<div ng-repeat="col in renderedColumns" ng-class="col.colIndex()" class="ngCell ">' +
                      '<div ng-cell></div>' +
                    '</div>' +
                 '</div>'

  }
});

CSS

.grid {
  width: 300px; 
  height: 100px;
  border: solid 1px rgb(90,90,90);
}

.red {
  background-color: red;
  color: white;
}