Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...