更改Angular模型以更新Kendo

时间:2014-01-09 21:39:02

标签: javascript angularjs kendo-ui angular-kendo

我一直在使用Angular进行项目,最近刚刚在http://kendo-labs.github.io/angular-kendo/#/发现了Kendo-Angular项目。我成功地将Angular-Kendo添加到了我的项目中,它的工作方式与我认为的一样,除了像我以前那样更新模型之外。

这个项目正是我正在寻找的,但是,文档中的示例实际上并没有显示您能够更新Angular模型,因此它更新了Kendo数据源。

以下是一段代码:

$scope.data = new kendo.data.DataSource({
    data: [{
        name: "India",
        data: [10, 7.943, 7.848, 9.284, 9.263, 9.801, 3.890, 8.238, 9.552, 6.855]
    }, {
        name: "World",
        data: [1.988, 2.733, 3.994, 3.464, 4.001, 3.939, 1.333, 2.245, 4.339, 2.727]
    }, {
        name: "Russian Federation",
        data: [4.743, 7.295, 7.175, 6.376, 8.153, 8.535, 5.247, 7.832, 4.3, 4.3]
    }, {
        name: "Haiti",
        data: [0.253, 0.362, 3.519, 1.799, 2.252, 3.343, 0.843, 2.877, 5.416, 5.590]
    }]
});

使用Angular,人们会期望做出类似这样的事情:

<input ng-model="data[0].data[0]" />

输入字段中的输出为10。但是,将此数据传递给图表并尝试更改输入框中的值时,图表不会更新。

任何有这些特定图书馆经验的人都知道如何实现这样的东西吗?这种支持是否存在?这只是一个让Kendo与Angular一起工作的库吗?

3 个答案:

答案 0 :(得分:2)

我解决了这个问题,但现在正如我期待的那样。

我只是将一个更改事件绑定到输入并调用了Kendo redraw()方法,并且每次我的模型更新时它都会重绘。考虑到剑道有一个完整的努力,有点烦人。您可能认为可以使用双向绑定。

如果存在,还在寻找更好的答案。

答案 1 :(得分:2)

请注意,angular-kendo的作者和/或对AngularJS有更深入了解的人可能会让我“做错了”,但这里有:

angular-kendo已经在数据源上使用了$watch,所以如果你最初在那里添加一些代码,比如这样:

scope.$watch(attrs.kDataSource, function (mew, old) {
    if (mew !== old) {
        element.data('$kendoDataSource', toDataSource(mew, type));

        var role = element.data("role");
        var widgetType = role.charAt(0).toUpperCase() + role.slice(1);
        var w = element.data("kendo" + widgetType);;

        if (!w) {
            w = kendo.widgetInstance(element, kendo.ui);
        }

        if (w && typeof w.setDataSource === "function") {
            w.setDataSource(element.data('$kendoDataSource'));
        }
    }
}, true);

那么你寻找的行为是有效的。我不确定为什么这个(好吧,类似的东西,但更好)没有实现;对我来说,它似乎是一个核心功能,但可能有一些我不理解的原因(我还没有真正阅读过这里的所有源代码)。在任何情况下,必须手动将更改事件处理程序附加到输入以更新UI对我来说似乎并非“有角度”。

Demo嘻嘻哈哈。免责声明:我不负任何责任。不要使用它。

编辑:看了一下angular-kendo问题跟踪器之后,看起来他们打算做类似的事情(从comment by @BurkeHolland here来判断),所以这可能不是完全错误的;我将演示更新为更具可读性

答案 2 :(得分:1)

我并不为这个解决方案而疯狂,但我认为这是目前进行绑定的最佳方式。解决方案是使用kendo.data.ObservableArray或kendo.data.DataSource来支持datagrid,然后更新控制器中的ObservableArray或DataSource:

angular.module('MyApp').controller('MyController', function($scope, $http) {
    $scope.products = new kendo.data.DataSource({
        data: [],            // kendo watches this array
        pageSize: 5
    });

    $http.get('data/products.json').then(function(result) {
        // update the Kendo DataSource here.
        $scope.products.data(result.data);
    });
});

HTML看起来像这样:

<div kendo-grid
     k-data-source="products"
     k-selectable="'row'"
     k-columns='[{ "field": "ProductName",           "title": "Name" },
                 { "field": "Supplier.SupplierName", "title": "Supplier" },
                 { "field": "Category.CategoryName", "title": "Category" }]'
     k-sortable="true"
     k-groupable="true"
     k-filterable="true">
</div>