Wijmo 5 - Filtered DataMap

HTML

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

  <h1>Dynamic DataMap</h1>

  <p>
    This grid overrides the <b>getDisplayValues</b> method of the
    "Cities" <b>DataMap</b> to show only cities in the row's country:</p>

  <div id="theGrid"></div>
</div>

CSS

.wj-flexgrid {
  max-height: 250px;
}

JavaScript

onload = function() {

  // create data maps
  var countryMap = new wijmo.grid.DataMap(getCountries(), 'id', 'name');
  var cityMap = new wijmo.grid.DataMap(getCities(), 'id', 'name');

	// override cityMap's getDisplayValues method to get cities that
  // correspond to the current item
  cityMap.getDisplayValues = function(dataItem) {
  	var arr = [],
  		cities = getCities(),
    	country = theGrid.collectionView.currentItem.country;
    for (var i = 0; i < cities.length; i++) {
    	var city = cities[i];
      if (city.country == country) {
      	arr.push(city.name);
      }
    }
    return arr;
  };

	// create and bind the grid
  var theGrid = new wijmo.grid.FlexGrid('#theGrid', {
  	autoGenerateColumns: false,
    columns: [
	    { binding: 'country', header: 'Country', dataMap: countryMap },
	    { binding: 'city', header: 'City', dataMap: cityMap },
	    { binding: 'downloads', header: 'Downloads' },
	    { binding: 'sales', header: 'Sales' }
		],
    itemsSource: getData()
	});

  // create some random data
  function getData() {
	  var countries = getCountries(),
    	cities = getCities(),
      data = [];
	  for (var i = 0; i < cities.length; i++) {
	    data.push({
	      country: cities[i].country,
	      city: cities[i].id,
	      downloads: Math.round(Math.random() * 20000),
	      sales: Math.random() * 10000,
	      expenses: Math.random() * 5000
	    });
	  }
    return data;
  }
	function getCountries() {
  	return [
	  	{ id: 0, name: 'US' },
	    { id: 1, name: 'Germany' },
	    { id: 2, name: 'UK' },
	    { id: 3, name: 'Japan' },
	    { id: 4, name: 'Italy' },
	    { id: 5, name: 'Greece' }
		];
	}
  function getCities() {
  	return [
  		{ id: 0, country: 0, name: 'Washington' },
    	{ id: 1, country: 0, name: 'Miami' },
    	{ id: 2, country: 0, name: 'Seattle' },
    	{ id: 3, country: 1, name: 'Bonn' },
    	{ id: 4, country: 1, name: 'Munich' },
    	{ id: 5, country: 1, name: 'Berlin' },
    	{ id: 6, country: 2, name: 'London' },
...