javascriptkendo-uikendo-datasourcekendo-templatekendo-listview

How to call a function from kendoListView template


Anyone can teach me how to pass the username which come from "myData" to myFunction? For example "myData" got a username call "jack", how to pass the "jack" to myfunction?

myData = new kendo.data.DataSource({
   transport: {
   read: function (options) {
      options.success(arrData);}
   }
});

$($element).find('.slakeNotice').kendoListView({
   dataSource: myData,
   template: '<tr><td>' + myFunction(username) +' </td></tr>'
}).data("kendoListView");

function myFunction(name){
   return xxx;
}

I tried several method, but no luck.

template: '<tr><td>' + myFunction(username) +' </td></tr>'

get username not defined.

template: '<tr><td>' + myFunction("username") +' </td></tr>'

or

 template: '<tr><td>' + myFunction("#=username#") +' </td></tr>'

just get a string in my function.

I also tried with:

template: '<tr><td>#=myFunction(username)#</td></tr>'

But get myFunction not defined.

Anyone can help? Thanks.


Solution

  • You could try something like,

        var getTemplate = function (e) {
            // put a breakpoint here and inspect 'e' (data item)
            return '<tr><td>' + e.username + ' </td></tr>'
        }
    
        $($element).find('.slakeNotice').kendoListView({
            dataSource: [{ id: 1, username: 'asd' }, { id: 2, username: 'asd' }],
            template: getTemplate
        }).data("kendoListView");