1. Angularjs 与 dataTable的结合
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="node_modules/datatables/media/css/jquery.dataTables.min.css">
</head>
<body>
<div ng-app="tableExample">
<div ng-controller="Ctrl">
<button ng-click="addData()">Add Data</button>
<h2>This table uses columns defined in controller</h2>
<table my-table options="options">
</table>
</div>
</div>
<script src="node_modules/jquery/dist/jquery.min.js"></script>
<script src="node_modules/datatables/media/js/jquery.dataTables.js"></script>
<script type="text/javascript" src="node_modules/angular/angular.min.js"></script>
<script src="./app.js"></script>
</body>
</html>
2. angularjs部分
angular.module('tableExample', [])
.directive('myTable', function () {
return {
restrict: 'E, A, C',
link: function (scope, element, attrs, controller) {
var dataTable = element.dataTable(scope.options);
scope.$watch('options.aaData', handleModelUpdates, true);
function handleModelUpdates(newData) {
var data = newData || null;
if (data) {
dataTable.fnClearTable();
dataTable.fnAddData(data);
}
}
},
scope: {
options: "="
}
};
})
.controller('Ctrl' , function ($scope){
$scope.options = {
aoColumns: [{
"sTitle": "Surname"
}, {
"sTitle": "First Name"
}],
aoColumnDefs: [{
"bSortable": false,
"aTargets": [0, 1]
}],
bJQueryUI: true,
bDestroy: true,
aaData: [
["Webber", "Adam"]
]
};
$scope.addData = function () {
$scope.counter = $scope.counter + 1;
$scope.options.aaData.push([$scope.counter, $scope.counter * 2]);
};
$scope.counter = 0;
})
显示效果
这中方式angular对数据表的操作主要是datatablejs 的监控scope.aaData 的变化刷新数据表这种,变化比较僵硬,没有利用好angular的模版