Wijmo (PureJS) - Dynamic DataMap

Dynamic datamap that changes the city dropdown based on the value of the country dropdown.

by Joel Parks

HTML

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

JavaScript

onload = function() {
  // create the grid
  var theGrid = new wijmo.grid.FlexGrid('#theGrid', {
    selectionMode: 'Row',
    autoGenerateColumns: false,
    columns: [
    	{ binding: 'country', header: 'Country', dataMap: getCountries() },
      { binding: 'city', header: 'City', dataMap: getCities() },
      //{ binding: 'downloads', header: 'Downloads', visible: false },
      //{ binding: 'sales', header: 'Sales', visible: false },
      //{ binding: 'counter', header: 'Counter', visible: false },
		],
    itemsSource: getData(),
    
    // update cities data map depending on country
    beginningEdit: function(s, e) {
      var col = s.columns[e.col];
      if (col.binding == 'city') {
        switch (s.rows[e.row].dataItem.country) {
          case 'US':
            col.dataMap = ['Washington', 'Miami', 'Seattle'];
            break;
          case 'UK':
            col.dataMap = ['London', 'Oxford', 'Bath'];
            break;
          case 'Japan':
            col.dataMap = ['Tokyio', 'Sendai', 'Kobe'];
            break;
          case 'Greece':
            col.dataMap = ['Athens', 'Santorini', 'Thebes'];
            break;
          case 'Germany':
            col.dataMap = ['Bonn', 'Munich', 'Kiel'];
            break;
          case 'Italy':
            col.dataMap = ['Rome', 'Florence', 'Milan'];
            break;
          default:
            col.dataMap = ['Unknown Country!'];
            break;
        }
      }
    }
	});

  // some random data
  function getCountries() {
    return 'US,Germany,UK,Japan,Italy,Greece'.split(',');
  }
  function getCities() {
    return 'Washington,Bonn,London,Tokyo,Rome,Athens'.split(',');
  }
  function getData() {
    var countries = getCountries(),
      cities = getCities(),
      data = [];
    for (var i = 0; i < 1000000; i++) {
      data.push({
        country: countries[i % countries.length],
        city: cities[i % cities.length],
        downloads: Math.round(Math.random() * 20000),
       ...