Kendo UI在ListView的模板中循环遍历Json中的集合

mse*_*ais 13 templates json listview razor kendo-ui

我试图在Kendo UI模板中找出列表视图,如何遍历每个对象中的集合以在页面上呈现信息.这是我正在玩的json的一个例子:

{"Data":[{"Title":"Malicious Acts","KeyPairs":{"Key1":"12","Key2":"24"}},            {"Title":"Enrollments","KeyPairs":{"Key1":"12","Key2":"24"}},{"Title":"Customer Feedback","KeyPairs":{"Key1":"12","Key2":"24"}},{"Title":"Questionable Accounts","KeyPairs":{"Key1":"12","Key2":"24"}}],"Total":4,"AggregateResults":null,"Errors":null}
Run Code Online (Sandbox Code Playgroud)

我想在模板中渲染KeyPairs项目,只是在理解如何操作时遇到一些麻烦.

这是来源:

<div id="subscriberFunctions">

    <script type="text/x-kendo-tmpl" id="template">
        <div class="subscriberFunction">
            <h3>${Title}</h3>
           <!-- Display KeyPairs -->
        </div>
    </script>

    @(Html.Kendo().ListView<SubscriberMenuFunction>()
        .Name("listView")
        .TagName("div")
        .ClientTemplateId("template")
        .DataSource(dataSource =>
        {
            dataSource.Read(read => read.Action("SubscriberMenu", "ListView"));
        })
        .Selectable(selectable => selectable.Mode(ListViewSelectionMode.Single))

    )
</div>
Run Code Online (Sandbox Code Playgroud)

我敢肯定我只是在思考这个问题,而且它是一种简单化的东西,但不确定如何在模板中实现foreach循环以便Kendo UI识别它.

提前致谢!

小智 26

您可以使用for循环中的计数器执行此操作.这应该可以解决您的问题:

<script type="text/x-kendo-tmpl" id="template">
    <div class="subscriberFunction">
        <h3>${Title}</h3>
        <!-- Display KeyPairs -->
        <ul>
            #for (var i=0,len=KeyPairs.length; i<len; i++){#
                <li>${ KeyPairs[i] }</li>
            # } #
        </ul>
    </div>
</script>
Run Code Online (Sandbox Code Playgroud)


Ata*_*hev 8

您可以在模板中包含任意JavaScript代码:

<script type="text/x-kendo-tmpl" id="template">
    <div class="subscriberFunction">
        <h3>${Title}</h3>
       <!-- Display KeyPairs -->
         <ul>
         # for (var key in KeyPairs) { #
              <li>${ KeyPairs[key] }</li>
         # } #
         </ul>
    </div>
</script>
Run Code Online (Sandbox Code Playgroud)

  • 这得到了我的价值,但带来了看起来像javascript代码:•52b1e4cc-5fef-4f5c-a0b5-f054b2cf6655•function(){return b}•function(a){var b = this,c,d,e = function( ){return b},f; o.fn.init.call(this); for(d in a)c = a [d],d.charAt(0)!="_"&&(f = M.call (c),c = b.wrap(c,d,e)),b [d] = c; b.uid = n.guid()}•function(a){return this.hasOwnProperty(a)&& a! =="_ events"&& typeof this [a]!== r && a!=="uid"}•function(){var a = {},b,c; for(c in this)if(this.shouldSerialize(c) ){b = this [c]; if(b instanceof R || b instanceof Q)b = b.toJSON(); a [c] = b}返回}} ... (2认同)