Kendo UI:使用MVVM(数据属性)使用分层数据网格时如何绑定网格/详细信息模板

时间:2017-07-12 06:14:41

标签: data-binding kendo-ui kendo-grid kendo-mvvm kendo-template

我正在使用Kendo UI构建分层数据网格,我正在使用MVVM方法进行小部件绑定。

Here is the DEMO 我要制作的分层网格。但这里的例子使用的是jQuery,而不是MVVM。

如何使用MVVM使用数据属性绑定detail template详细信息行?

我尝试使用以下代码绑定detailTemplate,但它无效:

JS:

var viewModel = kendo.observable({
    ......
    ..........
    gridDetailTemplate: "<div>Name: #: name #</div><div>Age: #: age #</div>",
    ..........
    ......
});

HTML(剑道模板):

<!-- Datagrid -->
<div data-role="grid" 
    data-columns="[
        {'field':'FullName', 'title':'Full Name'},
        {'field':'Gender', 'title':'Gender'},
        {'field':'Email', 'title':'Email'},
        {'field':'HomeTel', 'title':'HomeTel'},
        {'field':'Mobile', 'title':'MobileTel'},
    ]" 
    data-bind ="source: viewModel.datasource" 
    data-detail-init="viewModel.getGridRowDetailData"
    data-pageable='{
                    refresh: false,
                    pageSizes: true,
                    buttonCount: 5,
                }'
    data-navigatable = "true"
    data-resizable = "true"
    data-no-records= "true"
    data-messages = '{
        noRecords: "There is no data to be displayed"
    }'
    data-detail-template="viewModel.gridDetailTemplate"
    >
</div>

1 个答案:

答案 0 :(得分:0)

最后,这就是我完成它的方式:

我从viewModel中删除了detailTemplate,并在视图文件中创建了一个模板部分,并使用data-detail-template="data_grid_row_detail_template"将其与模板ID绑定

这是我的最终代码:

<强> JS:

var viewModel = kendo.observable({
    ......
    ..........
    //no need of the below line here
    //gridDetailTemplate: "<div>Name: #: name #</div><div>Age: #: age #</div>",
    ..........
    ......
});

HTML(剑道模板):

<!-- Datagrid -->
<div data-role="grid" 
    data-columns="[
        {'field':'FullName', 'title':'Full Name'},
        {'field':'Gender', 'title':'Gender'},
        {'field':'Email', 'title':'Email'},
        {'field':'HomeTel', 'title':'HomeTel'},
        {'field':'Mobile', 'title':'MobileTel'},
    ]" 
    data-bind ="source: viewModel.datasource" 
    data-detail-init="viewModel.getGridRowDetailData"
    data-pageable='{
                    refresh: false,
                    pageSizes: true,
                    buttonCount: 5,
                }'
    data-navigatable = "true"
    data-resizable = "true"
    data-no-records= "true"
    data-messages = '{
        noRecords: "There is no data to be displayed"
    }'
    data-detail-template="data_grid_row_detail_template"
    >
</div>

<!-- Detail template -->
<script type="text/x-kendo-template" id="data_grid_row_detail_template">
    <div>Name: #: name #</div><div>Age: #: age #</div>
</script>