Kendo网格客户端模板条件语句

14
 columns.Bound(p => p.Active).ClientTemplate("\\#if('#=Active#'=='Y') {\\<input type='button' value='OK' />\\}\\#").Width(150).Title("Status");

但是条件被视为字符串??

#if('Y'=='Y')`enter code here` {
<input type="button" value="OK">
}#  
5个回答

34

在Kendo模板中,有三种使用哈希语法的方法:

  1. 渲染字面值:#= #
  2. 渲染HTML编码的值:#: #
  3. 执行任意JavaScript代码:# if(...){# ... #}#

因此,在您的代码中,您需要编写:

columns.Bound(p => p.Active).ClientTemplate(
     "#if(Active=='Y') {#
        <input type="button" value="OK">
      #}#").Width(150).Title("Status");

在示例中,请注意#符号如何分隔内部代码和外部代码。当您处于代码内部时,您不必再次使用#来访问变量,这就是为什么Active之前可以没有#的原因。


你知道如何使用外部模板(在脚本文件中)来完成吗?我的模板太复杂了,无法放在客户端模板中... - Hemadeus
我认为这应该可以运行... columns.Bound(p => p.Active).ClientTemplate("# myfunc(data) #"); - Stuart Hallows
这对我在模板中解决空白值很有用:谢谢。 - callisto
如果我想编写else条件语句怎么办? - Metaphor

8
尝试这个:
 columns.Bound(p => p.Active).ClientTemplate(
                "# if (IsServiceExist) { #" +
                    "<input type='button' value='OK' />"+
                "# }#").Width(150).Title("Status");

如果我想要多个条件,应该怎么办呢?比如:if (IsServiceExist && !IsEmail)。这是正确的方式吗? - Metaphor
@Metaphor 是的,你可以在 ClientTemplate 中实现这个。 - Jaimin
它真的帮了我。谢谢Jaimin。来我的网站http://translateyar.ir。 - mahdi moghimi

7

我希望您能找到解决方案。以下是一些有关IT技术的内容:

columns.Bound(p => p.IsActive)
    .ClientTemplate(
        "\\# if (IsActive != false) { \\#" +
            "\\<input type=\"checkbox\" id=\"checkBox\" class=\"parentCheckBox\" window-call=\"template\" checked/>\\" +
        "\\# } else { \\#" + 
            "\\<input type=\"checkbox\" id=\"checkBox\" class=\"parentCheckBox\" window-call=\"template\" />\\" + 
        "#\\ } \\#")
    .Width(10);

4
为了在您的Kendo模板中呈现数据值,您可以使用以下内容作为指南:
columns.Template(@<text></text>)
    .ClientTemplate("#if (Field3 == true) {#"
    + "<a onclick='jsFoo(#=Id#)' href='\\#'></a> "
    + "#} #").Width(70).Title("ColA");

这个程序相关的内容从英语翻译成中文。只返回翻译后的文本:这个比原来的答案更好。 - JohnMathew

-1
columns.Bound(searchModel => searchModel.Value).ClientTemplate(
    "#if(Name=='DevboardTask'){# " + 
        "<a href='\\#UpdateStatusWindow' onclick=\"javascript:openflexpmtask('#=Value#');\">#=Value#</a> " +
    "#} else {# " +
        "<a\">#=Value#</a> " +
    "#}#");

这可能会对你有所帮助。这只是一个例子...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接