【问题标题】:Kendo knockout: re-bind column template剑道淘汰赛:重新绑定列模板
【发布时间】:2015-08-19 21:23:12
【问题描述】:

我正在使用 kendo-knockout 库。我创建了一个动态表,以便可以切换数据源。我做了一个自定义绑定,在更新事件期间我破坏了网格,然后设置新选项(数据源、列、模式等)并“重新创建”网格。但是我无法重新绑定我在列中设置的模板。

  var grid = $(element).data('kendoGrid');
  grid.thead.remove();
  grid.destroy();

--- 新选项

column.template = "<a data-bind=\"text: status,  css: {'errorMessage': true}, click: $root.openPopup \">${status}</a>";


                $scope.dataSource({
                    data: items,
                    columns: columns,
                    schemaModelFields: schema
                });

更新期间在活页夹中

 function (dataSource, element, vm) {
    [...]
     var gridOptions = {
                    columns: columns,
                    dataSource: {
                        data: data,
                        schema: {
                            model: {
                                fields: schemaModelFields
                            }
                        }
                    }, 
                    [...] 



 $(element).kendoGrid(gridOptions);

我希望能够从根 VM 调用 openPopup 方法。它确实正确渲染了模板,但绑定被破坏了。

<a data-bind="text: status,  css: {'errorMessage': true}, click: $root.openPopup">Not Set</a>

【问题讨论】:

    标签: javascript knockout.js binding kendo-grid


    【解决方案1】:

    我得到了答案...使用 dataBound 事件清理节点并重新应用绑定

            var gridOptions = {
                    columns: columns,
                    dataSource: {
                        type: 'knockout',
                        data: data,
                        schema: {
                            model: {
                                fields: schemaModelFields
                            }
                        }
                    },
                    useKOTemplates: true,
                    dataBound: dataBound,
                    preventBinding: true,                    
                };
    
                var grid = $(element).kendoGrid(gridOptions);
    
               var dataBound = function () {
                var body = this.element.find("tbody")[0];
    
                if (body) {
                    ko.cleanNode(body);
                    ko.applyBindings(ko.dataFor(body), body);
                }
            }
    

    这解决了我的问题,但适用于投标的上下文是来自根的 VM,而不是数据源项,因此在 $root.openPopup 中,我得到的参数是完整的 VM,而不是行项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-27
      • 1970-01-01
      • 2019-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多