JadedEric
JadedEric

Reputation: 2073

KendoUI Grid: Array as a field

I have a data source, which gets built from a JSON data string, containing a field called Fruit:

[{
... /other entries
fruit: [{
  name: 1
}, {
  name: 2
}, {
  name: 3
}]
}]

I'm using this field in a KGrid, and would like to do a comma seperated list of links, from the names:

<a href="#">1</a>, <a href="#">2</a>, <a href="#">3</a>

Currently, I'm hooking into the dataBound function, and build this up individually for the fruit field, is there an easier way to do this with, let's say, a template? I tried to look up information about something similar in the docs, but couldn't find anything pertaining to splitting arrays?

Upvotes: 1

Views: 7593

Answers (1)

Brett
Brett

Reputation: 4269

I wouldn't transform the data at the data source. That job is the responsibility of the UI component. Instead move your logic to the column template function of your grid. [ API reference ]

$('#grid').kendoGrid({
  columns: [ {
    field: 'fruit',
    template: function(dataItem) {
      var html = [];

      for (var i = 0; i < dataItem.length; i++) {
        html.push('<a href="#">' + dataItem[i].name + '</a>');
      }

      return html.join(', ');
    }
  }],
  dataSource: data
});

Upvotes: 7

Related Questions