Angular 数据表不处理数据
Angular Datatable not working with data
我正在使用 Angular-datatable 来显示数据并且它可以很好地处理静态数据,但是当动态提供数据时它不起作用。这让我很难过。
What i want to achieve is to load data from ajax
in datatable
.
我正在使用以下代码进行初始化,它工作正常Plunker:
function MyChapterController($scope, $q, DTOptionsBuilder, DTColumnBuilder, chapter) {
var vm = this;
vm.dtOptions = DTOptionsBuilder.newOptions()
.withPaginationType('full_numbers')
.withDisplayLength(2)
.withDOM('pitrfl');
vm.dtColumns = [
DTColumnBuilder.newColumn('id').withTitle('ID'),
DTColumnBuilder.newColumn('title').withTitle('First name'),
DTColumnBuilder.newColumn('lastName').withTitle('Last name').notVisible()
];
vm.myChapters = [];
}
以下代码无效检查 Plunker:
function MyChapterController($scope, $q, $resource, DTOptionsBuilder, DTColumnBuilder, chapter) {
var vm = this;
vm.dtOptions = DTOptionsBuilder.newOptions()
.withPaginationType('full_numbers')
.withDisplayLength(2)
.withDOM('pitrfl');
vm.dtColumns = [
DTColumnBuilder.newColumn('id').withTitle('ID'),
DTColumnBuilder.newColumn('title').withTitle('First name'),
DTColumnBuilder.newColumn('lastName').withTitle('Last name').notVisible()
];
vm.myChapters = [{
"id": 860,
"firstName": "Superman",
"lastName": "Yoda"
}, {
"id": 870,
"firstName": "Foo",
"lastName": "Whateveryournameis"
}, {
"id": 590,
"firstName": "Toto",
"lastName": "Titi"
}, {
"id": 803,
"firstName": "Luke",
"lastName": "Kyle"
}];
}
我也尝试过 dataresource
Plunker 但这里也很不幸。
它已经消耗了我很多时间。所以最后我决定听取大家的建议。任何帮助,将不胜感激。
你错过了两点:
- 控制器未被调用,因为它未添加到标记中。如果您以后要使用
ui-router
没关系,因为您可以在配置中添加控制器。
- 您的 table 未填充,因为您错过了添加绑定,例如
{{chapter.id}}
和 myChapters
在 $scope
中不可用,因为您使用的是 controllerAs
语法。
请查看下面或更新后的 plunkr 中的演示。
在下面的演示中,我只将 $http.get(...)
更改为 $http.jsonp(...)
以从 mocky.io.
获取 json 数据
更新
要使过滤器成为分页功能,您必须将 table 标记更改为此。
<table datatable="ng" dt-options="chapterCtrl.dtOptions" dt-column-defs="chapterCtrl.dtColumnDefs" id="cb-mychapter-table" class="row-border hover table table-bordered cb-data-table" cellspacing="0" width="100%">
// Code goes here
'use strict';
angular.module('myApp', ['datatables','ngResource']);
(function (angular) {
'use strict';
angular
.module('myApp')
.controller('myChapterController', MyChapterController)
.factory('chapter', ChapterFactory);
MyChapterController.$inject = ['$scope', '$q', '$resource', 'DTOptionsBuilder', 'DTColumnBuilder', 'chapter'];
function MyChapterController($scope, $q, $resource, DTOptionsBuilder, DTColumnBuilder, chapter) {
var vm = this;
vm.dtOptions = DTOptionsBuilder.newOptions()
.withPaginationType('full_numbers')
.withDisplayLength(2)
.withDOM('pitrfl');
vm.dtColumns = [
DTColumnBuilder.newColumn('id').withTitle('ID'),
DTColumnBuilder.newColumn('title').withTitle('First name'),
DTColumnBuilder.newColumn('lastName').withTitle('Last name').notVisible()
];
/*vm.myChapters = [{
"id": 860,
"firstName": "Superman",
"lastName": "Yoda"
}, {
"id": 870,
"firstName": "Foo",
"lastName": "Whateveryournameis"
}, {
"id": 590,
"firstName": "Toto",
"lastName": "Titi"
}, {
"id": 803,
"firstName": "Luke",
"lastName": "Kyle"
}];*/
chapter.getChapters().then(function(chapters) {
vm.myChapters = chapters;
});
}
ChapterFactory.$inject = ["$http"];
function ChapterFactory($http) {
return {
getChapters: function() {
return $http.jsonp('http://www.mocky.io/v2/55f2b4cb0938f5cd069cff10?callback=JSON_CALLBACK').then(function(response) {
console.log(response);
return response.data;
});
}
};
}
}(angular));
<!DOCTYPE html>
<html ng-app='myApp'>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!--<link rel="stylesheet" href="style.css">-->
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.9/js/jquery.dataTables.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular.min.js"></script>
<script src="https://cdn.rawgit.com/l-lin/angular-datatables/dev/dist/angular-datatables.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular-resource.min.js"></script>
<!--<script src="script.js"></script>-->
</head>
<body ng-controller="myChapterController as chapterCtrl" >
<table datatable="ng" dt-options="chapterCtrl.dtOptions" dt-column-defs="chapterCtrl.dtColumnDefs" id="cb-mychapter-table" class="row-border hover table table-bordered cb-data-table" cellspacing="0" width="100%">
<thead>
<tr>
<th>ID</th>
<th>FirstName</th>
<th>LastName</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="chapter in chapterCtrl.myChapters">
<td>{{chapter.id}}</td>
<td>{{chapter.firstName}}</td>
<td>{{chapter.lastName}}</td>
</tr>
</tbody>
</table>
</body>
</html>
我正在使用 Angular-datatable 来显示数据并且它可以很好地处理静态数据,但是当动态提供数据时它不起作用。这让我很难过。
What i want to achieve is to load data from
ajax
indatatable
.
我正在使用以下代码进行初始化,它工作正常Plunker:
function MyChapterController($scope, $q, DTOptionsBuilder, DTColumnBuilder, chapter) {
var vm = this;
vm.dtOptions = DTOptionsBuilder.newOptions()
.withPaginationType('full_numbers')
.withDisplayLength(2)
.withDOM('pitrfl');
vm.dtColumns = [
DTColumnBuilder.newColumn('id').withTitle('ID'),
DTColumnBuilder.newColumn('title').withTitle('First name'),
DTColumnBuilder.newColumn('lastName').withTitle('Last name').notVisible()
];
vm.myChapters = [];
}
以下代码无效检查 Plunker:
function MyChapterController($scope, $q, $resource, DTOptionsBuilder, DTColumnBuilder, chapter) {
var vm = this;
vm.dtOptions = DTOptionsBuilder.newOptions()
.withPaginationType('full_numbers')
.withDisplayLength(2)
.withDOM('pitrfl');
vm.dtColumns = [
DTColumnBuilder.newColumn('id').withTitle('ID'),
DTColumnBuilder.newColumn('title').withTitle('First name'),
DTColumnBuilder.newColumn('lastName').withTitle('Last name').notVisible()
];
vm.myChapters = [{
"id": 860,
"firstName": "Superman",
"lastName": "Yoda"
}, {
"id": 870,
"firstName": "Foo",
"lastName": "Whateveryournameis"
}, {
"id": 590,
"firstName": "Toto",
"lastName": "Titi"
}, {
"id": 803,
"firstName": "Luke",
"lastName": "Kyle"
}];
}
我也尝试过 dataresource
Plunker 但这里也很不幸。
它已经消耗了我很多时间。所以最后我决定听取大家的建议。任何帮助,将不胜感激。
你错过了两点:
- 控制器未被调用,因为它未添加到标记中。如果您以后要使用
ui-router
没关系,因为您可以在配置中添加控制器。 - 您的 table 未填充,因为您错过了添加绑定,例如
{{chapter.id}}
和myChapters
在$scope
中不可用,因为您使用的是controllerAs
语法。
请查看下面或更新后的 plunkr 中的演示。
在下面的演示中,我只将 $http.get(...)
更改为 $http.jsonp(...)
以从 mocky.io.
更新
要使过滤器成为分页功能,您必须将 table 标记更改为此。
<table datatable="ng" dt-options="chapterCtrl.dtOptions" dt-column-defs="chapterCtrl.dtColumnDefs" id="cb-mychapter-table" class="row-border hover table table-bordered cb-data-table" cellspacing="0" width="100%">
// Code goes here
'use strict';
angular.module('myApp', ['datatables','ngResource']);
(function (angular) {
'use strict';
angular
.module('myApp')
.controller('myChapterController', MyChapterController)
.factory('chapter', ChapterFactory);
MyChapterController.$inject = ['$scope', '$q', '$resource', 'DTOptionsBuilder', 'DTColumnBuilder', 'chapter'];
function MyChapterController($scope, $q, $resource, DTOptionsBuilder, DTColumnBuilder, chapter) {
var vm = this;
vm.dtOptions = DTOptionsBuilder.newOptions()
.withPaginationType('full_numbers')
.withDisplayLength(2)
.withDOM('pitrfl');
vm.dtColumns = [
DTColumnBuilder.newColumn('id').withTitle('ID'),
DTColumnBuilder.newColumn('title').withTitle('First name'),
DTColumnBuilder.newColumn('lastName').withTitle('Last name').notVisible()
];
/*vm.myChapters = [{
"id": 860,
"firstName": "Superman",
"lastName": "Yoda"
}, {
"id": 870,
"firstName": "Foo",
"lastName": "Whateveryournameis"
}, {
"id": 590,
"firstName": "Toto",
"lastName": "Titi"
}, {
"id": 803,
"firstName": "Luke",
"lastName": "Kyle"
}];*/
chapter.getChapters().then(function(chapters) {
vm.myChapters = chapters;
});
}
ChapterFactory.$inject = ["$http"];
function ChapterFactory($http) {
return {
getChapters: function() {
return $http.jsonp('http://www.mocky.io/v2/55f2b4cb0938f5cd069cff10?callback=JSON_CALLBACK').then(function(response) {
console.log(response);
return response.data;
});
}
};
}
}(angular));
<!DOCTYPE html>
<html ng-app='myApp'>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!--<link rel="stylesheet" href="style.css">-->
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.9/js/jquery.dataTables.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular.min.js"></script>
<script src="https://cdn.rawgit.com/l-lin/angular-datatables/dev/dist/angular-datatables.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular-resource.min.js"></script>
<!--<script src="script.js"></script>-->
</head>
<body ng-controller="myChapterController as chapterCtrl" >
<table datatable="ng" dt-options="chapterCtrl.dtOptions" dt-column-defs="chapterCtrl.dtColumnDefs" id="cb-mychapter-table" class="row-border hover table table-bordered cb-data-table" cellspacing="0" width="100%">
<thead>
<tr>
<th>ID</th>
<th>FirstName</th>
<th>LastName</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="chapter in chapterCtrl.myChapters">
<td>{{chapter.id}}</td>
<td>{{chapter.firstName}}</td>
<td>{{chapter.lastName}}</td>
</tr>
</tbody>
</table>
</body>
</html>