@@ -11,62 +11,67 @@ <h1><i class="fa fa-play"></i> Binding Angular directive to the DataTable</
11
11
< tabset >
12
12
< tab heading ="Preview ">
13
13
< article class ="preview ">
14
- < div ng-controller ="BindAngularDirectiveCtrl ">
15
- < p class ="text-danger "> < strong > {{ message }}</ strong > </ p >
14
+ < div ng-controller ="BindAngularDirectiveCtrl as showCase ">
15
+ < p class ="text-danger "> < strong > {{ showCase. message }}</ strong > </ p >
16
16
< br />
17
- < table datatable dt-options ="dtOptions " dt-columns ="dtColumns " class ="row-border hover "> </ table >
17
+ < table datatable dt-options ="showCase. dtOptions " dt-columns ="showCase. dtColumns " class ="row-border hover "> </ table >
18
18
</ div >
19
19
</ article >
20
20
</ tab >
21
21
< tab heading ="HTML ">
22
22
< div hljs >
23
- < div ng-controller ="BindAngularDirectiveCtrl ">
24
- < p class ="text-danger "> < strong > {{ message }}</ strong > </ p >
23
+ < div ng-controller ="BindAngularDirectiveCtrl as showCase ">
24
+ < p class ="text-danger "> < strong > {{ showCase. message }}</ strong > </ p >
25
25
< br />
26
- < table datatable dt-options ="dtOptions " dt-columns ="dtColumns " class ="row-border hover "> </ table >
26
+ < table datatable dt-options ="showCase. dtOptions " dt-columns ="showCase. dtColumns " class ="row-border hover "> </ table >
27
27
</ div >
28
28
</ div >
29
29
</ tab >
30
30
< tab heading ="JS ">
31
31
< div hljs language ="js ">
32
32
angular.module('datatablesSampleApp', ['datatables']).controller('BindAngularDirectiveCtrl', BindAngularDirectiveCtrl);
33
33
34
- function BindAngularDirectiveCtrl($scope, $compile, DTOptionsBuilder, DTColumnBuilder) {
35
- $scope.message = '';
36
- $scope.edit = edit;
37
- $scope.delete = deleteRow;
38
- $scope.dtOptions = DTOptionsBuilder.fromSource('data1.json')
34
+ function BindAngularDirectiveCtrl($scope, $compile, DTOptionsBuilder, DTColumnBuilder, DTInstances) {
35
+ var vm = this;
36
+ vm.message = '';
37
+ vm.edit = edit;
38
+ vm.delete = deleteRow;
39
+ vm.dtOptions = DTOptionsBuilder.fromSource('data1.json')
39
40
.withPaginationType('full_numbers')
40
41
.withOption('createdRow', createdRow);
41
- $scope .dtColumns = [
42
+ vm .dtColumns = [
42
43
DTColumnBuilder.newColumn('id').withTitle('ID'),
43
44
DTColumnBuilder.newColumn('firstName').withTitle('First name'),
44
45
DTColumnBuilder.newColumn('lastName').withTitle('Last name'),
45
46
DTColumnBuilder.newColumn(null).withTitle('Actions').notSortable()
46
47
.renderWith(actionsHtml)
47
48
];
48
49
50
+ DTInstances.getLast().then(function (dtInstance) {
51
+ vm.dtInstance = dtInstance;
52
+ });
53
+
49
54
function edit(id) {
50
- $scope .message = 'You are trying to edit the row with ID: ' + id;
55
+ vm .message = 'You are trying to edit the row with ID: ' + id;
51
56
// Edit some data and call server to make changes...
52
57
// Then reload the data so that DT is refreshed
53
- $scope.dtOptions .reloadData();
58
+ vm.dtInstance .reloadData();
54
59
}
55
60
function deleteRow(id) {
56
- $scope .message = 'You are trying to remove the row with ID: ' + id;
61
+ vm .message = 'You are trying to remove the row with ID: ' + id;
57
62
// Delete some data and call server to make changes...
58
63
// Then reload the data so that DT is refreshed
59
- $scope.dtOptions .reloadData();
64
+ vm.dtInstance .reloadData();
60
65
}
61
66
function createdRow(row, data, dataIndex) {
62
67
// Recompiling so we can bind Angular directive to the DT
63
68
$compile(angular.element(row).contents())($scope);
64
69
}
65
70
function actionsHtml(data, type, full, meta) {
66
- return '< button class ="btn btn-warning " ng-click ="edit(' + data.id + ') "> ' +
71
+ return '< button class ="btn btn-warning " ng-click ="showCase. edit(' + data.id + ') "> ' +
67
72
' < i class ="fa fa-edit "> </ i > ' +
68
73
'</ button > ' +
69
- '< button class ="btn btn-danger " ng-click ="delete(' + data.id + ') "> ' +
74
+ '< button class ="btn btn-danger " ng-click ="showCase. delete(' + data.id + ') "> ' +
70
75
' < i class ="fa fa-trash-o "> </ i > ' +
71
76
'</ button > ';
72
77
}
0 commit comments