Jas*_*euw 6 javascript kendo-ui angularjs 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 }
});
});
Run Code Online (Sandbox Code Playgroud)
在角度的SkillController中,我将这些获取的技能放在范围变量中.
$scope.skills = SkillFactory.getSkills();
Run Code Online (Sandbox Code Playgroud)
我在这里初始化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" }
]
};
Run Code Online (Sandbox Code Playgroud)
大多数情况下,ajax回调比kendo网格的初始化要慢.然后它将显示一个空表,因为该表的数据未绑定到角度$ scope.skills变量.
我到处搜索,但我无法弄清楚如何在初始化中使用自定义函数作为数据属性,或者如何将范围变量绑定到表.
任何帮助,将不胜感激!
小智 7
我发现了一个更简单的解决方案:在我的例子中,$ scope.regs定义了使用通过"AppService"公开的Angular $资源从服务器REST服务更新的数据.该服务定义为:
var registrationServices = angular.module('registrationServices', ['ngResource']);
registrationServices.factory('AppService', ['$resource',
function($resource) {
return $resource('rest/registrations');
}]);
Run Code Online (Sandbox Code Playgroud)
我在HTML中将k-auto-bind ="false"设置为网格定义:
<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>
Run Code Online (Sandbox Code Playgroud)我没有使用"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"}
}
];
....
Run Code Online (Sandbox Code Playgroud)然后,当我想刷新网格中的数据时,我使用Angular $资源进行回调.:
$scope.doSearch = function() {
$scope.regs = AppService.query({"regId": $scope.searchAppId}, function(result) {
$scope.registrations.read();
});
};
Run Code Online (Sandbox Code Playgroud)有用.此解决方案的另一个优点是,您不必将网格创建移动到Java Script代码,它可以保留在HTML中.
这样的事情会让你走上正确的道路.您可以使用传输读取方法简单地调用工厂.你只是不能混合和匹配它们你们所有人都读取,创建和销毁方法必须使用工厂或者他们都必须明确地调用终点即url:'/ api/myservice /'而不是使用$ 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");
}
}
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
17110 次 |
最近记录: |