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

2024-02-01

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

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

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

在我的 Angular 技能控制器中,我将这些获取的技能放入范围变量中。

$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网格的初始化慢。然后它将显示一个空表,因为该表的数据未绑定到 Angular $scope.skills 变量。

我到处搜索,但我不知道如何在初始化中为数据属性使用自定义函数,或者如何将范围变量绑定到表。

任何帮助,将不胜感激!


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

    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 $resource 进行回调。 :

    $scope.doSearch = function() {
        $scope.regs = AppService.query({"regId": $scope.searchAppId}, function(result) {
            $scope.registrations.read();
        });
    };
    

有用。 该解决方案的另一个优点是,您不必将网格创建转移到 Java 脚本代码中,它可以保留在 HTML 中。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为kendo数据源提供一个角度范围变量 的相关文章