Is it possible have this code equivalent code in kendo external template

十年热恋 提交于 2019-12-12 06:45:44

问题


I have this dojo with its inline template

  var actionName = 'read';
    $("#grid").kendoGrid({
      columns: [
        { field: "name" },
        { field: "age" }
      ],
      dataSource: [
        { name: "Jane Doe", age: 30, read: true, actionName: actionName }
      ],
      detailTemplate: "<div> #:" + actionName  +"# </div>"
    });

and this dojo with its external template:

<script id="detailTemplate" type="text/x-kendo-template">
        #: actionName #
</script> 


var actionName = 'read';
$("#grid").kendoGrid({
  columns: [
    { field: "name" },
    { field: "age" }
  ],
  dataSource: [
{ name: "Jane Doe", age: 30, read: true, actionName: actionName }
  ],
  detailTemplate: function(dataItem){ 
    return kendo.template($("#detailTemplate").html())(dataItem);
  }
});

In first one detailTemplate: "<div> #:" + actionName +"# </div>" I can render value: true however in second one wiht #: actionName # I can render read.

So my question how can I render to value true


回答1:


The parametr (which you named) dataItem takes over data from current row. Then you need to write property which you want to render. dataItem is valid for whole template.

<script id="detailTemplate" type="text/x-kendo-template">
    #: read #
</script>

and

detailTemplate: function(dataItem){ 
    return kendo.template($("#detailTemplate").html())(dataItem);
}

should be enough.

Dojo example

Parameter of function in detailTemplate will contains all properties from object in row (even not visible as column). So if you will do some special calculations or something you can do it in detailtemplate function and create new property which will be accesible by name in template.

Dojo example 2

Edit: If you want run (let's say) some command, which is in string you have to use eval function. This function will execute the text. It means if you will use some functuion name as eval parameter, this function will be executed.

Dojo example 3

Yet, I dare to quote one important paragrapg from this site: eval - mozila developer

Don't use eval needlessly!

eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension. More importantly, third party code can see the scope in which eval() was invoked, which can lead to possible attacks in ways to which the similar Function is not susceptible.

eval() is also generally slower than the alternatives, since it has to invoke the JS interpreter, while many other constructs are optimized by modern JS engines.

There are safer (and faster!) alternatives to eval() for common use-cases.



来源:https://stackoverflow.com/questions/34382724/is-it-possible-have-this-code-equivalent-code-in-kendo-external-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!