【问题标题】:Kendo template conditional formatting剑道模板条件格式
【发布时间】:2012-06-17 19:32:08
【问题描述】:

免责声明:最初是 posted 发给 KendoUI 论坛的,但没有收到任何答复。

我正在尝试在我的 ListView 模板中使用条件格式的元素。这个局部视图使用一个共享的 DataSource 来允许通过 Pager、一个两卡 ListView 和前面提到的模板进行导航。下面是相关的模板代码:

<script id="contact-template" type="text/x-kendo-template">
<div id="ContactCard" class="IsActive${IsActive}">
    #if (Salutation === null || Salutation === '') {#<h4>#}else{#<h4>#=Salutation# #}##=FirstName# #=LastName#</h4>
    #if (Title === null || Title === '') {##}else{#<p>#=Title#</p>#}#
    <br />
    #if (Email == 0 || Email === '') {##}else{#<p><a href='mailto:#=LastName#,%20#=FirstName#%20<#=Email#>'>#=Email#</a></p>#}#
    #if (Phone === null  || Phone === '') {##}else{#<p>#=Phone##if (Extension === null || Extension === '') {#</p>#}else{# ext. #=Extension#</p>#}##}#
</div>

我尝试了几种不同的方法来生成这段代码,包括一个简单的 if 和 if (Salutation != null &amp;&amp; Salutation != '') 这样的反向检查,但无济于事。我想我错过了一些关于如何从#if 部分引用数据源数据的内容?我尝试了if (#=Salutation# != null &amp;&amp; #=Salutation# != '') 之类的方法,但这引发了错误的模板错误。

这是输出:

注意:忽略可怕的格式。这是预造型。

这是整个文件,供参考:

@model int   @* accountId  *@

<article id="contactArticle">
    <div id="contactList"></div>
    <footer><span id="pagerTotal"></span><a href="#" class="k-link" id="pageLeft" onclick="pageLeftOne()"><</a><div id="pager"></div><a href="#" class="k-link" id="pageRight" onclick="pageRightOne()">></a></footer>
</article>
<script id="contact-template" type="text/x-kendo-template">
    <div id="ContactCard" class="IsActive${IsActive}">
        #if (Salutation === null || Salutation === '') {#<h4>#}else{#<h4>#=Salutation# #}##=FirstName# #=LastName#</h4>
        #if (Title === null || Title === '') {##}else{#<p>#=Title#</p>#}#
        <br />
        #if (Email == 0 || Email === '') {##}else{#<p><a href='mailto:#=LastName#,%20#=FirstName#%20<#=Email#>'>#=Email#</a></p>#}#
        #if (Phone === null  || Phone === '') {##}else{#<p>#=Phone##if (Extension === null || Extension === '') {#</p>#}else{# ext. #=Extension#</p>#}##}#
    </div>
</script>
<script type="text/javascript">
    var currentPage = 1;
    var pages;
    var contactDataSource;

    //SNIP//   

    $(document).ready(function() {
        var init = 1;
        contactDataSource = new kendo.data.DataSource({
            transport: {
                read: {
                    url: '@Url.Action("ContactPager", "Contact")',
                    dataType: "json",
                    type: "POST",
                    timeout: 2000,
                    data: {
                        accountId: @Model
                    }
                }
            },
            schema: {
                data: "data",
                total: "total",
                type: "json",
                model: {
                    fields: {
                        Id: { type: "string"},
                        FirstName: { type: "string" },
                        LastName: { type: "string"},
                        Title: { type: "string", defaultValue: ''},
                        Salutation: { type: "string", defaultValue: ''},
                        Extension: { type: "string", defaultValue: ''},
                        Phone: { type: "string", defaultValue: ''},
                        Email: { type: "string", defaultValue: ''},
                        IsActive: {type: "boolean"} //,
                        //ReceivesDistributionEmails: {type: "boolean"}
                    }
                }
            },
            pageSize: 2
        });

        contactDataSource.read();

        contactDataSource.bind("change", function(e) {
            if (init) {
                init = 0;
                if (contactDataSource.total() < 1) {
                    //SNIP

                } else {
                    $("#pager").kendoPager({
                        dataSource: contactDataSource,
                        buttonCount: 5
                    });
                    //SNIP//     
                    pages = $("#pager").data("kendoPager").dataSource.totalPages();

                    $("#contactList").kendoListView({
                        dataSource: contactDataSource,
                        pageable: true,
                        template: kendo.template($("#contact-template").html())
                    });
                    kendo.init($("#contactList"));
                }
            }
        });
    });

</script>

TL;DR:我如何获得一个 Kendo 模板来根据数据源成员的值构建它的内容?

【问题讨论】:

  • 模板的输出有什么问题?这是编写模板表达式的有用链接:demos.kendoui.com/web/templates/expressions.html
  • 我在编写代码时仔细研究了该操作指南,尤其是“在模板定义中使用 JavaScript 代码”部分。我以为我用对了……但是,我的输出仍然显示空值,而不是跳过这些字段。
  • 看起来#=Title# 的评估是文字字符串'null',因此您需要将其评估为字符串值。看看我刚刚发布的答案。

标签: asp.net-mvc-4 kendo-ui


【解决方案1】:

尝试将 null 用单引号括起来:

...
#if (Title != 'null' && Title != '')  { #
     <p>#=Title# </p> 
# } #
...

尽管标记被留下,但这种表示法可以用作替代方法。它可能会根据您尝试实现的格式而起作用。

<p>${ Title != 'null' && Title != '' ? Title : ''} </p>

【讨论】:

  • 非常感谢!多么简单的问题可以忽略,但我从来没有想过!你摇滚。
  • 奇怪,我在网格中使用了行模板。上面的解决方案不起作用,但这个: ${ Title ?标题:'-'}
【解决方案2】:

我知道这是旧的,但我使用的另一个解决方案如下:

@(Html.Kendo().Grid<Object>()
    .Name("dataGrid")
    .DataSource(ds =>
        ds.Ajax()
            .Read(r => r.Action("Action", "Controller", new { area = "area" }))
            .ServerOperation(true)
            .PageSize(50)
            )
    .Columns(cols =>
    {
        cols.Bound(t => t.Property);
    })
    .Resizable(resizeable => resizeable.Columns(true))
    .Scrollable(t => t.Virtual(true))
    .Sortable()
    .Filterable()
    .ColumnMenu()
    .HtmlAttributes(new { style = "height:98%;width:100%;", @class="cssClass" })
    .Events(e => e.DataBound("onDataBound"))
    .Deferred()
    .ClientRowTemplate("<tr>" +
            "#=checkNull(Property)#" +
            "</tr>")
 )

然后,您可以添加一个 JavaScript 函数来检查属性。

   function checkNull(item) {
        return item === null ? "" : item;
    }

这很令人沮丧,所以它可能会帮助其他人。显然,您可以更改函数以检查您喜欢的任何内容。

【讨论】:

  • 接受的答案对我不起作用。这个做了
  • @tjeuten,它对我也不起作用 - 所以我在我的情况下为其他人添加了这个。
【解决方案3】:

对于一个快捷方式,您可以使用:

# if(property){ #
#: property #
# } #

如果要根据值显示/隐藏(不是空字符串或null)

【讨论】:

    【解决方案4】:

    我意识到这是一个旧线程,但是我的回答可能对某人有用。

    你可以像这样内联你的条件:

    groupHeaderTemplate: "${ value == 'D' ? 'Declined' : value == 'P' ? 'Pending' : value == 'A' ? 'Approved' : value }"
    

    【讨论】:

      猜你喜欢
      • 2015-02-26
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      相关资源
      最近更新 更多