Wijmo 5 (Angular) - Reverting Cell CSS

by Joel Parks

HTML

<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="http://cdn.wijmo.com/5.20143.24/controls/wijmo.min.js"></script>
<link rel="stylesheet" href="http://cdn.wijmo.com/5.20143.24/styles/wijmo.min.css">
<script src="http://cdn.wijmo.com/5.20143.24/controls/wijmo.grid.min.js"></script>
<script src="http://cdn.wijmo.com/5.20143.24/controls/wijmo.chart.min.js"></script>
<script src="http://cdn.wijmo.com/5.20143.24/interop/angular/wijmo.angular.min.js"></script>
<!-- mark this as an Angular application and give it a controller -->
<div ng-app="app" ng-controller="appCtrl">
    
<h1>Highlight Edited Cells</h1>

    <p>This is a <b>FlexGrid</b> control. Edit some cells to see their background color change.</p>
    <wj-flex-grid items-source="data" initialized="initialized(s,e)">
    </wj-flex-grid>
    <button onclick="update()">
      Revert CSS
    </button>
</div>

JavaScript

// define app, include Wijmo 5 directives
var app = angular.module('app', ['wj']);

// controller
app.controller('appCtrl', function ($scope) {

    // create some random data
    var countries = 'US,Germany,UK,Japan,Italy,Greece'.split(','),
        data = [];
    for (var i = 0; i < countries.length; i++) {
        data.push({
            country: countries[i],
            downloads: Math.round(Math.random() * 20000),
            sales: Math.random() * 10000,
            expenses: Math.random() * 5000
        });
    }
    
    var oCell;
    
    update = function(){
    	oCell.style.backgroundColor = '';
    }

    // expose data as a CollectionView to get events
    $scope.data = new wijmo.collections.CollectionView(data);
    
    // highlight cells that have been edited
    $scope.initialized = function (s, e) {
        
        // 1: keep track of edited cells
        s.cellEditEnded.addHandler(function (s, e) {
            setChanged(e.row, e.col);
        });
        
        // 2: change the background of edited cells
        s.itemFormatter = function(panel, r, c, cell) {
            if (panel == s.cells) {
            		if(getChanged(r, c, cell)){
                		console.log("Old backgroundColor: " + cell.style.backgroundColor);
                		cell.style.backgroundColor = '#00d8ff'
                    console.log("New backgroundColor: " + cell.style.backgroundColor);
                }else{
                		cell.style.backgroundColor = ''
                }
            }
        }
        
        // utility to keep track of changed items
        var changes = [];
        function setChanged(r, c) {
            if (!getChanged(r, c)) {
                changes.push({
                    item: s.rows[r].dataItem, 
                    prop: s.columns[c].binding });
            }
        }
        function getChanged(r, c, cell) {
            for (var i = 0; i < changes.length; i++) {
                var item = s.rows[r].dataItem;
                var...