Search code examples
javascriptslickgrid

SlickGrid -- viewing and grouping by multiple values field


I'm using SlickGrid and very happy with it, one of the best grids I've ever encountered. I use DataView for the data and groupItemMetadataProvider for grouping.

I have a following problem and looking for a solution. My data contains some fields with multiple values. E.g. one data entry can be associated with multiple countries: US, IR, NZ or multiple categories: cats, dogs, birds.

Two questions:

  • What is the best practice to implement the view for several entries in one cell. ? I suppose, custom template or formatter can be used here, but will be glad to hear your suggestions.
  • How to implement grouping by such field? E.g. I would like to group by country, so the entry should be shown under each country group, same row under US, AU and NZ.

Real Life Example

Without grouping

Name    | Animals
-----------------
Paker   | cats, dogs, birds
Michael | cats, snakes

Group by animals

Name    | Animals
-----------------
- cats
Paker   | cats, dogs, birds
Michael | cats, snakes

- dogs
Paker   | cats, dogs, birds

- birds
Paker   | cats, dogs, birds

- snakes
Michael | cats, snakes

Cross-posted to SlickGrid Google Group: https://groups.google.com/forum/#!topic/slickgrid/BxS_4Lny3KE

Thanks a lot!


Solution

  • When displaying custom or collection based data within a single cell, if attempting to render that data vertically, you'll need to consider the lack of native support for dynamic row height.

    Inspecting the grouping function (extractGroups ~line 472)

    for (var i = 0, l = rows.length; i < l; i++) {
     r = rows[i];
     val = gi.getterIsAFn ? gi.getter(r) : r[gi.getter];
     group = groupsByVal[val];
     if (!group) {
       group = new Slick.Group();
       group.value = val;
       group.level = level;
       group.groupingKey = (parentGroup ? parentGroup.groupingKey + groupingDelimiter : '') + val;
       groups[groups.length] = group;
       groupsByVal[val] = group;
     }
     group.rows[group.count++] = r;
    }
    

    reveals that a custom getter is possible, but that a single value is used/expected in the grouping of each row. Thus, for a row having a multi-value cell to appear in multiple groups (without modyfying source), the data would have to be "flattened".

    A less than ideal solution then becomes to maintain datasets respective to the view state being rendered. As data interaction needs and/or data size increase, this workaround becomes less feasible and tends toward the need to implement a customization in the source.