Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use function in Kendo Grid Column Template with AngularJS

I have a column in a Kendo grid that I want to perform some specific logic for when rendering, and am using Angular. I have the grid columns set up using the k-columns directive.

After looking at the documentation, it seemed simple: I could add the template option to my column, define the function to perform my logic, and pass the dataItem value in. What I have looks something like this:

k-columns='[{ field: "Name", title: "Name", 
    template: function (dataItem){
        // Perform logic on value with dataItem.Name
        // Return a string
    }
}]'

However, running this causes a syntax error complaining about the character '{' that forms the opening of the block in my function.

I have seen several examples of defining a template function in this format. Is there something else that needs to be done for this to work? Am I doing something incorrectly? Is there another way of defining the template as a function and passing the column data to it? (I tried making a function on my $scope, which worked, except I couldn't figure out how to get data passed into the function.)

Thank you for your help.

like image 321
MWinstead Avatar asked Jun 10 '14 18:06

MWinstead


People also ask

How do you call a function in kendo template?

We will start by creating a sample grid, applying a simple template, and then move the generation of the content to render to a function that we will call from the template. Before we start we will create a simple Kendo UI grid sample using AngularJS, and then we will proceed to add templates to certain columns.

What is Pageable in kendo grid?

kendo:grid-pageable-messagesThe text messages displayed in pager. Use this option to customize or localize the pager messages.

What is Dataitem in kendo grid?

Returns the data item to which the specified table row is bound. The data item is a Kendo UI Model instance.

What is Template in kendo grid?

template String|Function. The template which renders the column content. The grid renders table rows ( <tr> ) which represent the data source items. Each table row consists of table cells ( <td> ) which represent the grid columns. By default the HTML-encoded value of the field is displayed in the column.


1 Answers

This can be done via the columns.template parameter by supplying a callback function whose parameter is an object representing the row. If you give the row a field named name, this will be the property of the object you reference:

$("#grid").kendoGrid({
  columns: [ {
    field: "name",
    title: "Name",
    template: function(data) {
        return data.name + "has my respect."
    }
  }],
  dataSource: [ { name: "Jane Doe" }, { name: "John Doe" } ]
});

More information is available on Kendo's columns.template reference page.

like image 191
Bucket Avatar answered Oct 13 '22 00:10

Bucket