【问题标题】:Kendo UI MVVM - How to iterate over and render a collection within a view?Kendo UI MVVM - 如何在视图中迭代和渲染集合?
【发布时间】:2015-07-14 11:30:57
【问题描述】:

我只是想在我的 Kendo.View 中循环一个数组并尝试从元素中呈现一个属性。这在 MVC Razor 中非常简单,例如

@foreach( var displayLink in Model ) {
 <h1>displayLink.Text</h1>
}

我没有选择摘录,而是分享了整个文件。

这一切都运行,没有例外等。视图呈现静态内容,但不呈现循环的内容。我打开了evalTemplate = true,但仍然没有骰子。我一直无法找到任何方法来做到这一点,这让我发疯。我能找到的只是连接 Kendo UI ListView 等的方法。我不想要那个权重,我只想直接遍历数组。

Index.htm(视图):

<div class="jumbotron">
    <div class="container">
        <h1>Web</h1>
        <p>The future is <i>now</i>.
        </p>
    </div>
</div>


# for(var i = 0; i < DashboardLinks.length; i++) { #
    <h1>#= DashboardLinks[i].TitleText #</h1>
# } #

控制器:

define(
    // == INTERFACE NAME ==
    "Controllers.IHome", 

     // == DEPENDENCIES ==
    [
        "Util.IGetViewSource", 
        "Util.ILayout",
        "ViewModels.Home.IHomeVM"
    ],

    function ( /* Dependency injections: */ getViewSource, layout, iHomeVM)
    {

        // Define the module.
        var module =
           {
               index: function () {

                   getViewSource("~/App/Views/Home/Index.htm", function (viewSource) {
                       // get the model
                       var viewModel = new iHomeVM();
                       viewModel.AddDashboardLink("#timecard", "Time Cards", "Manage time cards and get it done.", "time");

                       // render the view
                       var view = new kendo.View(viewSource, { model: viewModel, evalTemplate: true });

                       // render the view
                       layout.renderBodyView(view);
                   });
               }
           };

        // Return the module.
        return module;
    }
);

家庭虚拟机:

define(
    // == INTERFACE NAME ==
    "ViewModels.Home.IHomeVM",

    // == DEPENDENCIES ==
    [
        "ViewModels.Shared.ILinkVM"
    ],
    function(
        // == DEPENDENCY INJECTIONS ==
        iLinkVM
    ) {
        // == CONSTRUCTOR ==
        function HomeVM() {
            console.log("HomeVM constructor executing.");


            // == PROPERTIES & METHODS ==
            this.DashboardLinks = [];


            // Return a copy of this wrapped in Kendo's observable.
            return kendo.observable(this);
        }

        HomeVM.prototype.AddDashboardLink = function(
            href,
            titleText,
            descriptionText,
            iconName) {
            this.DashboardLinks.push(new iLinkVM(
                href,
                titleText,
                descriptionText,
                iconName
            ));
        } 

        // Return the view model module.
        return HomeVM;
    }
);

链接虚拟机:

define(
    // == INTERFACE NAME ==
    "ViewModels.Shared.ILinkVM",  

    // == DEPENDENCIES ==
    [

    ],

    function (
        // == DEPENDENCY INJECTIONS ==

    )
    {
        // == CONSTRUCTOR ==
        function LinkVM(href, titleText, descriptionText, iconName) {
            console.log("LinkVM constructor executing.");


            // == PROPERTIES & METHODS ==
            this.Href = href;
            this.TitleText = titleText;
            this.DescriptionText = descriptionText;
            this.IconName = iconName;


            // Return a copy of this wrapped in Kendo's observable.
            return kendo.observable(this);
        }


        // Return the view model module.
        return LinkVM;
    }
);

【问题讨论】:

    标签: javascript html kendo-ui kendo-mvvm client-side-templating


    【解决方案1】:

    乍一看,您的剑道模板似乎缺少一些步骤。具体来说,您需要在&lt;script type="text/x-kendo-template"&gt; 标记中定义一个模板并将数据传递给它。对于我正在制作的示例,这将表示为这样

    <script id="myTemplate" type="text/x-kendo-template">
        # for (var i = 0; i < data.DashboardLinks.length; i++) { #
            <h1>#= data.DashboardLinks[i].TitleText #</h1>
        # } #
    </script>
    

    然后为了使用它,我们可以利用这些 Kendo 函数来动态渲染我们的模板 - 我们可以将其注入到我们的 DOM 中

    var template = kendo.template($("#myTemplate").html()); // notice id on <script>
    
    var data = { DashboardLinks: [{TitleText : 'LinkA'}, {TitleText: 'LinkB'}, {TitleText: 'LinkC'}] };
    
    var result = template(data);
    

    注意我们如何将data 传递给我们的template(data)。这个函数渲染我们的模板,事实上如果我们console.log(result)此时我们看到我们渲染的模板

    链接A


    链接B


    链接C

    然后可以调用 .html(result) 将我们渲染的标记注入到我们的 DOM 中

    来源:Kendo UI Templates Overview

    工作示例:Kendo UI Dojo

    【讨论】:

    • 如果是模板就好了,但这是视图。 (kendo.View) 我想在我的视图中使用这个模板功能。在我看来真的没有办法循环数据吗?
    • @AndrewLundgren 您是否找到了另一种方法来完成此任务,而不会增加利用模板的复杂性?
    【解决方案2】:

    我找到了:您可以通过设置“evalTemplate”属性来做到这一点: http://docs.telerik.com/kendo-ui/api/javascript/view#configuration-evalTemplate

      // create the view
      var view = new kendo.View(viewSource, { model: viewModel, evalTemplate: true });
    

    然后您可以使用 MVVM 声明式绑定以及 Kendo 模板绑定,例如 for 循环。

    确保正确转义所有哈希 ('#'),否则模板会崩溃。

    【讨论】:

    • 干得好!如果模板方法有帮助,我会留下我的答案
    猜你喜欢
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    相关资源
    最近更新 更多