如何在KendoUI上动态绑定列定义

How to bind columns definition dynamically on KendoUI

本文关键字:动态绑定 定义 KendoUI      更新时间:2023-09-26

我需要动态绑定到kendou上的一个网格的列字段。

<table    id="checkout-grid"
                class="k-grid"
                data-role="grid"
                data-bind="source: items"
                data-row-template="checkout-form-item-template"
                data-scrollable="false"
                data-columns="[
                        { title: 'Name', width: '300px'},
                        'Description',
                        { title: 'Price', width: '50px' },
                        { title: 'Quantity', width: '50px' },
                        { title: 'Total', width: '100px' }
                    ]">
        </table>

问题是,我需要设置不同语言的列的标题。

如何在不使用javascript的kendoGrid方法的情况下绑定该定义。

我尝试从视图模型绑定数据列值,但它抛出一个不支持绑定异常,甚至使用attr值。

首先,我看到你的Grid定义有一个问题,你说的是title,而不是field

回答您的问题并知道您不想动态生成columns.definition,我建议您将其定义为:

<table id="checkout-grid"
    class="k-grid"
    data-role="grid"
    data-bind="source: items"
    data-scrollable="false"
    data-columns="[
        { field: 'Name', width: '300px'},
        'Description',
        { field: 'Price', width: '50px' },
        { field: 'Quantity', width: '50px' },
        { field: 'Total', width: '100px' }
        ]">
    <thead>
        <td>Nombre</td>
        <td>Descripción</td>
        <td>Precio</td>
        <td>Cantidad</td>
        <td>Total</td>
    </thead>
</table>

。,在本地化中添加thead定义。但显然,如果你的服务器可以这样做,也可以生成如下内容:

<table id="checkout-grid"
    class="k-grid"
    data-role="grid"
    data-bind="source: items"
    data-scrollable="false"
    data-columns="[
        { field: 'Name', title: 'Nombre', width: '300px'},
        { field: 'Description', title: 'Descripción' },
        { field: 'Price', width: '50px', title: 'Precio' },
        { field: 'Quantity', width: '50px', title: 'Cantidad' },
        { field: 'Total', width: '100px', title: 'Total' }
    ]"
>