Dynamic DataMaps

In some situations, you may want to change the data map used with a column depending on the value of another column. For example, you may want the drop-down in the 'City' column to show only cities in the country that is selected in the current row.

This is not as simple as it may seem, because the maps are used by all cells, not just by the ones being edited. So changing the map values will change the whole grid.

There are two ways to handle this:

  • Customized DataMaps: You can override the DataMap's getDisplayValues method to get only values that apply to the current context.

    For example:

    // create data map
    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;
    };
  • String-Only DataMaps: If your data maps contain only array strings, then it is not used as a real map. The cells actually contain the strings stored in the grid, and in this case it is safe to switch data maps before editing cells.

    For example:

    // 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;
          // ... other countries ...
          default:
            col.dataMap = ['Unknown Country!'];
            break;
        }
      }
    });
1
2
3
4
5
6
7
8
9
10
11
12
13
using System.Web.Mvc;
 
namespace LearnMvcClient.Controllers
{
    public partial class C1FlexGridController : Controller
    {
        // GET: DynamicDataMaps
        public ActionResult DynamicDataMaps()
        {
            return View();
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<h1>
    @Html.Raw(Resources.C1FlexGrid.DynamicDataMaps_Title)
</h1>
<p>
    @Html.Raw(Resources.C1FlexGrid.DynamicDataMaps_Text1)
</p>
<p>
    @Html.Raw(Resources.C1FlexGrid.DynamicDataMaps_Text2)
</p>
<p>
    @Html.Raw(Resources.C1FlexGrid.DynamicDataMaps_Text3)
</p>
<ul>
    <li>
        <p>
            @Html.Raw(Resources.C1FlexGrid.DynamicDataMaps_Text4)
        </p>
        <p>
            @Html.Raw(Resources.C1FlexGrid.DynamicDataMaps_Text5)
        </p>
        <pre>
// create data map
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 &lt; cities.length; i++) {
    var city = cities[i];
    if (city.country == country) {
      arr.push(city.name);
    }
  }
  return arr;
};</pre>
    </li>
    <li>
        <p>
            @Html.Raw(Resources.C1FlexGrid.DynamicDataMaps_Text6)
        </p>
        <p>
            @Html.Raw(Resources.C1FlexGrid.DynamicDataMaps_Text5)
        </p>
<pre>
// 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;
      // ... other countries ...
      default:
        col.dataMap = ['Unknown Country!'];
        break;
    }
  }
});</pre>
    </li>
</ul>