KB - Wijmo 5 - Cell Color On Update

This sample shows you how to change the color of a cell when its value changes.

by Joel Parks

HTML

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.grapecity.com/wijmo/5.latest/styles/wijmo.min.css">
<script src="https://cdn.grapecity.com/wijmo/5.latest/controls/wijmo.min.js"></script>
<script src="https://cdn.grapecity.com/wijmo/5.latest/controls/wijmo.grid.min.js"></script>
<script src="https://cdn.grapecity.com/wijmo/5.latest/controls/wijmo.chart.min.js"></script>
<div class="container">

  <h1>Hello Wijmo 5!</h1>
  <div id="theGrid"></div>
</div>

CSS

.wj-flexgrid {
  margin: 12px;
}
.wj-flexchart {
  height: 300px;
  margin: 12px;
}
body {
  margin-bottom: 24pt;
}

JavaScript

onload = function() {

    // create some random data
    var countries = 'US,Germany,UK,Japan,Italy,Greece'.split(',');
    var data = [], listOfEditedCells = [];
    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
        });
    }

    // create CollectionView on the data (so grid and chart stay in sync)
    var view = new wijmo.collections.CollectionView(data);

    // initialize the grid
    var grid = new wijmo.grid.FlexGrid('#theGrid', {
        columns: [
        	{ binding: 'country', header: 'Country' },
          { binding: 'downloads', header: 'Downloads' },
          { binding: 'sales', header: 'Sales' }
				],
        autoGenerateColumns: false,
        itemsSource: view,
        selectionMode: wijmo.grid.SelectionMode.Row,
        itemFormatter: function(panel, r, c, cell) {
        		if(panel == grid.cells) {
            		for(var i = 0; i < listOfEditedCells.length; i++) {
                		if(listOfEditedCells[i].row == r && listOfEditedCells[i].column == c) {
                    		cell.style.backgroundColor = '#e0e0e0';
                    }
                }
            }
        },
        cellEditEnding: function(s, e) {
        		console.log(s.getCellData(e.row, e.col))
            console.log(s.activeEditor.value);
        		if(s.getCellData(e.row, e.col) != s.activeEditor.value) {
            		listOfEditedCells.push({ row: e.row, column: e.col });
            }
        }
    });
}