Demo - Custom Cells

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>
<div class="container">

  <h1>
    Custom Cells</h1>
  <p>
    The FlexGrid provides a powerful infrastructure for binding cells
    to data and formatting the cells using CSS.</p>
  <p>
    But in some cases that may not be enough. In those situations,
    use the <b>formatItem</b> event to customize the style or
    the content presented in each cell.</p>
  <p>
    The grid below uses <b>formatItem</b> to calculate and format
    cells that show the difference between values in the current
    and previous items:</p>
  <div id="theGrid">
  </div>
    
</div>

CSS

.wj-flexgrid {
  max-height: 250px;
  margin: 10px 0;
}
.wj-cell .v-center {
  position: relative;
  top: 50%;
	transform: translateY(-50%);
  white-space: pre-wrap;
}
.wj-cell:not(.wj-state-selected):not(.wj-state-multi-selected) .diff-none {
  color: #d8b400;
}
.wj-cell:not(.wj-state-selected):not(.wj-state-multi-selected) .diff-up {
  color: #4c8f00;
}
.wj-cell:not(.wj-state-selected):not(.wj-state-multi-selected) .diff-down {
  color: #9f0000;
}
body {
  margin-bottom: 20px;
}

JavaScript

onload = function () {

	// generate some random data
  var countries = 'US,Germany,UK,Japan,Italy,Greece'.split(','),
      data = [];
  for (var i = 0; i < 200; i++) {
    data.push({
    	id: i,
      country: countries[i % countries.length],
      sales: Math.random() * 10000,
      expenses: Math.random() * 5000,
    });
  }

	// show data in a grid
  var theGrid = new wijmo.grid.FlexGrid('#theGrid', {
    isReadOnly: true,
    allowResizing: 'None',
    allowDragging: 'None',
    allowSorting: false,
    selectionMode: 'RowRange',
  	showAlternatingRows: false,
  	autoGenerateColumns: false,
    columns: [
			{ binding: 'id', header: 'ID', width: 50 },
			{ binding: 'country', header: 'Country' },
			{ binding: 'sales', header: 'Sales', width: 80, format: 'n0' },
			{ binding: 'salesDiff', header: 'Diff', dataType: 'Number', width: 80, format: 'p0' },
			{ binding: 'expenses', header: 'Expenses', width: 80, format: 'n0' },
			{ binding: 'expensesDiff', header: 'Diff', dataType: 'Number', width: 80, format: 'p0' }
    ],
    itemsSource: data,
  });

	// insert extra column header row
  var ch = theGrid.columnHeaders,
  		hr = new wijmo.grid.Row();
  ch.rows.insert(0, hr);
  
  // fill out headings in extra header row
  for (var i = 0; i < theGrid.columns.length; i++) {
  	var hdr = ch.getCellData(1, i);
    if (hdr == 'Diff') hdr = ch.getCellData(1, i - 1)
	  ch.setCellData(0, i, hdr);
  }
  
  // allow merging across and down extra header row
	theGrid.allowMerging = 'ColumnHeaders';
	hr.allowMerging = true;      
  theGrid.columns[0].allowMerging = true;
  theGrid.columns[1].allowMerging = true;

	// custom rendering for headers and "Diff" columns
  theGrid.formatItem.addHandler(function(s, e) {
  
		// center-align column headers
		if (e.panel == s.columnHeaders) {
			e.cell.innerHTML = '<div class="v-center">' +
      	e.cell.innerHTML + '</div>';
    }
  
		// custom rendering for "Diff" columns
    if (e.panel == s.cells) {
		  	var col =...