为kendo数据源提供角度范围变量

时间:2013-10-10 07:43:33

标签: javascript angularjs kendo-ui kendo-grid angularjs-resource

我目前正在尝试使用远程数据填充kendo网格。 Kendo有自己的功能来获取数据,但我想使用我创建的角度工厂。

所以我有一个工厂,它有一个“getSkills”功能。此函数从我的api获取所有技能对象。

angular.module('MyApp').factory('Factory', function ($resource) {
    return $resource('/api/v1/skills/', {  },
        {
            getSkills: { method: 'GET', isArray: true }
        });
});    

在我的角色中的SkillController中,我将这些获取的技能放在范围变量中。

$scope.skills = SkillFactory.getSkills();

我在这里初始化Kendo网格:

$scope.gridOptions = {
                dataSource: {
                    data: $scope.skills,
                    schema: {
                        model: {
                            fields: {
                                ID: { type: "number" },
                                Name: { type: "string" },
                                CreatedBy: { type: "number" },
                                CreatedDate: { type: "string" },
                                EditedBy: { type: "number" },
                                EditedDate: { type: "string" },
                                InUse: { type: "boolean" }
                            }
                        }
                    },
                    pageSize: 20
                },
                scrollable: true,
                sortable: true,
                filterable: true,
                pageable: {
                    input: true,
                    numeric: false
                },
                selectable: true,
                columns: [
                    { field: "Name", title: "skillname", width: "130px" }
                ]
            };

大多数情况下,ajax回调比kendo网格的初始化要慢。然后它将显示一个空表,因为该表的数据未绑定到角度$ scope.skills变量。

我到处搜索过,但我无法弄清楚如何在初始化中使用自定义函数作为数据属性,或者如何将范围变量绑定到表中。

任何帮助将不胜感激!

4 个答案:

答案 0 :(得分:7)

我找到了一个更简单的解决方案: 在我的例子中,$ scope.regs定义了使用Angular从服务器REST服务更新的数据 使用“AppService”公开的$ resource。此服务定义为:

    var registrationServices = angular.module('registrationServices', ['ngResource']);

    registrationServices.factory('AppService', ['$resource',
        function($resource) {
            return $resource('rest/registrations');
    }]);
  1. 我将k-auto-bind =“false”设置为HTML中的网格定义:

    <div id="form-item">
     <label for="appId" class="info">AppId:</label>
     <input id="appId" ng-model="searchAppId"> 
     <button id="search" class="k-button" ng-click="doSearch()" >Search</button>
    </div>  
    
    <div kendo-grid  k-data-source="registrations" k-selectable="'row'"
      k-pageable='{ "refresh": true, "pageSizes": true }'
      k-columns="registrationsColumns"
      k-sortable="true" k-groupable="true" k-filterable="true"
      k-on-change="selectedItem = data"
      k-auto-bind="false" >
    </div>
    
  2. 我没有使用“data”属性绑定Kendo网格数据源,而是使用“transport”并将“read”定义为函数,类似于:

      $scope.regs;
    
     $scope.registrations = new kendo.data.DataSource({
        transport: {
            read: function(options) {
                options.success($scope.regs);
            }
        },
        schema: {
            model: {
                fields: {
                    registrationId: {type: "number"},
                    clientFullName: {type: "string"},
                    registrationDate2: {type: "number"},
                    registrationDate: {type: "date"}
                }
            }
        },
        pageSize: 5,
        serverPaging: true,
        serverFiltering: true,
        serverSorting: true
    });
    
    
    $scope.registrationsColumns = [{"field": "registrationId", "title": "Id"},
        {"field": "clientFullName", "title": "Name"},
        {"field": "registrationDate",
            "title": "Registration Date",
            format: "{0:dd/MM/yyyy}",
            filterable: {ui: dateFilter, extra: "false"}
        }
    ];
        ....
    
  3. 然后,当我想刷新网格中的数据时,我使用Angular $资源进行回调。 :

    $scope.doSearch = function() {
        $scope.regs = AppService.query({"regId": $scope.searchAppId}, function(result) {
            $scope.registrations.read();
        });
    };
    
  4. 有效。 此解决方案的另一个优点是,您不必将网格创建移动到Java Script代码,它可以保留在HTML中。

答案 1 :(得分:6)

这样的事情会让你走上正确的道路。您可以使用传输读取方法简单地调用工厂。您无法混合和匹配它们要么所有人都阅读,创建和销毁方法必须使用工厂,要么他们都必须明确地调用终点即url:&#39; / api / myservice /& #39;而不是使用$ http只需消耗你的工厂就像在你的应用程序中的任何其他地方一样:

$scope.Source = new kendo.data.DataSource({

    transport: {

        read: function (options) {

            return $http.post('/api/getReportData/', {sDate: '', eDate: ''})
                .success(function (data) {

                    options.success(data);
                    Toaster.Info("Refreshed");

                })
                .error(function () {
                    return;
                });

            console.log("mmm");
        }

    }

});

答案 2 :(得分:0)

我修复了以下内容:

我为我的资源函数提供了一个这样的回调:

SkillFactory.getSkills({},
    function success(data) {
        createGrid(data);
    });

在函数createGrid(data)中;我初始化这样的数据:

 function createGrid(gridData) {
$("#skillGrid").kendoGrid({
   dataSource: {
   data: gridData,
   schema: {
      model: {
        fields: {
              ID: { type: "number" },
              Name: { type: "string" },
              CreatedBy: { type: "number" },
              CreatedDate: { type: "string" },
              EditedBy: { type: "number" },
              EditedDate: { type: "string" },
              InUse: { type: "boolean" }
                }
             }
          },
       pageSize: 20
       },

因此,在初始化的data属性中,我在成功获取数据时设置了数据。 我希望这有帮助!

答案 3 :(得分:-2)

您是否在 Angular 中查看了$q 承诺?见$q promises in Angular