How to access column name from column template in KendoUI Grid?

noir picture noir · Aug 2, 2012 · Viewed 7.8k times · Source

I have the following issue when using KendoUI Grid with a column template:

My data for a row looks like this (simplified):

{ Name: 'name', Statuses: [ {Name: 'StatusA', Value: 'ValueA'} , {Name:'StatusB', Value: 'ValueB'}] }

So on each row data I have a list of name-value collection for statuses. I have a number of columns (one for each status) named with the status name: StatusA, StatusB, etc.

In the column template I want to put the value of the status depending on the column name. I can access data.Statuses in the template and get the status values, but the template is not aware for which column is being rendered.

Is there any way to access the column name in the template or send it as a parameter?

Any help is appreciated!

Answer

dougw picture dougw · Oct 19, 2012

You can include a javascript function in the template like this:

{title:"StatusA", field:"Status1", template: "#= formatStatus(data.Statuses[0]) #" },
{title:"StatusB", field:"Status2", template: "#= formatStatus(data.Statuses[1]) #"}

Here is the article that helped me discover this:

http://www.kendoui.com/forums/ui/grid/grid-cell-template-for-image-with-javascript-selector.aspx