具有angularjs的数据表不适用于Columns和dtOptions
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了具有angularjs的数据表不适用于Columns和dtOptions相关的知识,希望对你有一定的参考价值。
以下是我的代码
1)在index.html文件中包含js和css在下面
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="assets/js/angular.min.js"></script>
<script src="bower_components/datatables.net/js/jquery.dataTables.min.js"></script>
<script src="bower_components/angular-datatables/demo/src/archives/dist/angular-datatables.min.js"></script>
<link rel="stylesheet" href="bower_components/angular-datatables/demo/src/archives/dist/css/angular-datatables.min.css" type="text/css" />
2)app.js
var app1 = angular.module('app', ['datatables']);
3)在app modules curd_datatable curd.controller.js中
'use strict';
curdController.$inject = ['$scope', '$http', '$state', 'config', 'DTOptionsBuilder', 'DTColumnBuilder', ];
function curdController($scope, config, DTOptionsBuilder, DTColumnBuilder) {
$scope.dtOptions = DTOptionsBuilder.newOptions()
.withOption('ajax', {
url: 'data.json',
type: 'GET'
})
.withDataProp('data')
.withOption('serverSide', true)
.withOption('processing', true)
.withOption('order', [[0, 'asc'], [1, 'asc']])
.withPaginationType('full_numbers');
$scope.dtColumns = [
DTColumnBuilder.newColumn('id').withTitle('ID'),
DTColumnBuilder.newColumn('firstName').withTitle('First name'),
DTColumnBuilder.newColumn('lastName').withTitle('Last name').notVisible()
];
}
module.exports = curdController;
4)app modules curd_datatable curd.html
<div class="container">
<div class="table-responsive">
<div ng-controller="curdController as showCase">
<table datatable="" dt-options="showCase.dtOptions" dt-columns="showCase.dtColumns" class="row-border hover"></table>
</div>
</div>
</div>
5)app modules curd_datatable index.js
angular.module('app').controller('curdController', require('./curd.controller'));
6)app modules curd_datatable data.json
[{
"id": 860,
"firstName": "Superman",
"lastName": "Yoda"
}, {
"id": 870,
"firstName": "Foo",
"lastName": "Whateveryournameis"
}, {
"id": 590,
"firstName": "Toto",
"lastName": "Titi"
}, {
"id": 803,
"firstName": "Luke",
"lastName": "Kyle"
}
]
它给我的错误如下,
angular.min.js:116 TypeError: Cannot read property 'aDataSort' of undefined
首先改变index.html
中的脚本序列
更新了index.html
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="bower_components/datatables.net/js/jquery.dataTables.min.js"></script>
<script src="assets/js/angular.min.js"></script>
<script src="bower_components/angular-datatables/demo/src/archives/dist/angular-datatables.min.js"></script>
<link rel="stylesheet" href="bower_components/angular-datatables/demo/src/archives/dist/css/angular-datatables.min.css" type="text/css" />
应用模块 curd_datatable curd.controller.js
控制器中的组件顺序应相同。
更换:
function curdController($scope, $http, $state, config, DTOptionsBuilder, DTColumnBuilder) {
附:
function curdController($scope, config, DTOptionsBuilder, DTColumnBuilder) {
最终守则
'use strict';
curdController.$inject = ['$scope', '$http', '$state', 'config', 'DTOptionsBuilder', 'DTColumnBuilder'];
function curdController($scope, $http, $state, config, DTOptionsBuilder, DTColumnBuilder) {
$scope.dtOptions = DTOptionsBuilder.newOptions()
.withOption('ajax', {
url: 'data.json',
type: 'GET'
})
.withDataProp('data')
.withOption('serverSide', true)
.withOption('processing', true)
.withOption('order', [[0, 'asc'], [1, 'asc']])
.withPaginationType('full_numbers');
$scope.dtColumns = [
DTColumnBuilder.newColumn('id').withTitle('ID'),
DTColumnBuilder.newColumn('firstName').withTitle('First name'),
DTColumnBuilder.newColumn('lastName').withTitle('Last name').notVisible()
];
}
module.exports = curdController;
应用模块 curd_datatable curd.html
删除ng-controller
和showCase
,因为我们不需要它来访问控制器变量。
<div class="container">
<div class="table-responsive">
<table datatable="" dt-options="dtOptions" dt-columns="dtColumns" class="row-border hover"></table>
</div>
</div>
以上是关于具有angularjs的数据表不适用于Columns和dtOptions的主要内容,如果未能解决你的问题,请参考以下文章
带有 angularjs 的数据表不适用于 Columns 和 dtOptions
DataTable Bootstrap 不适用于 AngularJs
CORS 适用于 plnkr,但不适用于浏览器:“响应具有 HTTP 状态代码 403”
$http.POST 不适用于 DATETIME 数据类型(AngularJS、Spring 4.2、MySQL、Hibernate)