是否有可能在 kendo 外部模板中包含此代码等效代码
Is it possible have this code equivalent code in kendo external template
我有 this dojo 及其内联模板
var actionName = 'read';
$("#grid").kendoGrid({
columns: [
{ field: "name" },
{ field: "age" }
],
dataSource: [
{ name: "Jane Doe", age: 30, read: true, actionName: actionName }
],
detailTemplate: "<div> #:" + actionName +"# </div>"
});
和this dojo及其外部模板:
<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);
}
});
在第一个 detailTemplate: "<div> #:" + actionName +"# </div>"
中,我可以渲染值:true
但是在第二个 #: actionName #
中,我可以渲染 read
。
所以我的问题是如何呈现值 true
参数(您命名的)dataItem 接管当前行的数据。然后你需要写 属性 你想要渲染的。 dataItem 对整个模板有效。
<script id="detailTemplate" type="text/x-kendo-template">
#: read #
</script>
和
detailTemplate: function(dataItem){
return kendo.template($("#detailTemplate").html())(dataItem);
}
应该够了。
detailTemplate 中的函数参数将包含行中对象的所有属性(甚至作为列不可见)。因此,如果您要进行一些特殊的计算或其他操作,您可以在 detailtemplate 函数中进行计算并创建新的 属性 ,它将在模板中按名称访问。
编辑:
如果你想要 运行 (比方说)一些命令,它在字符串中,你必须使用 eval 函数。此函数将执行文本。这意味着如果你使用一些函数名作为eval参数,这个函数将被执行。
然而,我敢于引用本网站的一个重要段落: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.
我有 this dojo 及其内联模板
var actionName = 'read';
$("#grid").kendoGrid({
columns: [
{ field: "name" },
{ field: "age" }
],
dataSource: [
{ name: "Jane Doe", age: 30, read: true, actionName: actionName }
],
detailTemplate: "<div> #:" + actionName +"# </div>"
});
和this dojo及其外部模板:
<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);
}
});
在第一个 detailTemplate: "<div> #:" + actionName +"# </div>"
中,我可以渲染值:true
但是在第二个 #: actionName #
中,我可以渲染 read
。
所以我的问题是如何呈现值 true
参数(您命名的)dataItem 接管当前行的数据。然后你需要写 属性 你想要渲染的。 dataItem 对整个模板有效。
<script id="detailTemplate" type="text/x-kendo-template">
#: read #
</script>
和
detailTemplate: function(dataItem){
return kendo.template($("#detailTemplate").html())(dataItem);
}
应该够了。
detailTemplate 中的函数参数将包含行中对象的所有属性(甚至作为列不可见)。因此,如果您要进行一些特殊的计算或其他操作,您可以在 detailtemplate 函数中进行计算并创建新的 属性 ,它将在模板中按名称访问。
编辑: 如果你想要 运行 (比方说)一些命令,它在字符串中,你必须使用 eval 函数。此函数将执行文本。这意味着如果你使用一些函数名作为eval参数,这个函数将被执行。
然而,我敢于引用本网站的一个重要段落: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.