Przeglądaj źródła

edit table

tags/0.1.0
Robin Thoni 8 lat temu
rodzic
commit
05b790e862
2 zmienionych plików z 176 dodań i 0 usunięć
  1. 35
    0
      src/lutable.html
  2. 141
    0
      src/lutable.js

+ 35
- 0
src/lutable.html Wyświetl plik

@@ -0,0 +1,35 @@
1
+<div lu-busy="itemList">
2
+    <table class="col-sm-12 table table-hover">
3
+        <thead>
4
+        <tr>
5
+            <th>
6
+                <input type="checkbox" ng-click="toggleSelectAll()"
7
+                       ng-checked="selectedItems.length == items.Data.length && items.Data.length != 0">
8
+            </th>
9
+            <th class="col-sm-3" ng-repeat="col in columns">{{ col.name }}</th>
10
+        </tr>
11
+        </thead>
12
+        <tbody>
13
+        <tr ng-repeat="item in items.Data" style="cursor: pointer" ng-click="displayItem(item)">
14
+            <td>
15
+                <input name="selectedItems[]" type="checkbox" ng-checked="selectedItems.indexOf(item.Id) > -1"
16
+                       ng-click="$event.stopPropagation();toggleSelectedItem(item.Id)" >
17
+            </td>
18
+            <td ng-repeat="col in columns">{{ col.getValue(item) }}</td>
19
+        </tr>
20
+        </tbody>
21
+    </table>
22
+
23
+    <div class="col-sm-12 text-center">
24
+        <a class="{{ p == page ? 'pagination-current' : 'pagination-not-current'}}" href="" ng-repeat="p in pages" ng-click="loadPage(p)">{{ p + 1 }}&nbsp;</a>
25
+    </div>
26
+
27
+    <div class="col-sm-12">
28
+        <button class="btn btn-default" type="button" ng-click="removeItems()" ng-disabled="selectedItems.length == 0">
29
+            <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Remove
30
+        </button>
31
+        <button class="btn btn-default" type="button" ng-click="addItem()">
32
+            <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add
33
+        </button>
34
+    </div>
35
+</div>

+ 141
- 0
src/lutable.js Wyświetl plik

@@ -0,0 +1,141 @@
1
+/**
2
+ * Created by robin on 11/3/15.
3
+ */
4
+
5
+angular.module('luticateUtils')
6
+    .directive('luTable', ['dialogs', 'luticateDialogErrorHelper',
7
+        function(dialogs, luticateDialogErrorHelper) {
8
+            return {
9
+                restrict: 'E',
10
+                templateUrl: "/luticate/lutable.html",
11
+                link: function ($scope, element, attrs) {
12
+                    $scope.page = 0;
13
+                    $scope.perPage = 15;
14
+                    $scope.items = [];
15
+                    $scope.pages = [];
16
+                    $scope.selectedItems = [];
17
+
18
+                    var promiseLoadItems = {
19
+                        id: "promiseLoadItems",
20
+                        groups: ['itemList']
21
+                    };
22
+                    var promiseDelItems = {
23
+                        id: "promiseDelItems",
24
+                        loadGroups: ['itemList']
25
+                    };
26
+
27
+                    $scope.loadPage = function (page) {
28
+                        $scope.getLoadPagePromise(page, $scope.perPage, promiseLoadItems)
29
+                            .then(function (items) {
30
+                                $scope.page = page;
31
+                                $scope.items = items;
32
+                                $scope.pages = [];
33
+                                var start = Math.max(0, $scope.page - 5);
34
+                                var end = Math.min(start + 10, (items.Count / $scope.perPage) + (items.Count % $scope.perPage == 0 ? -1 : 0));
35
+                                for (var i = start; i < end; ++i) {
36
+                                    $scope.pages.push(i);
37
+                                }
38
+                            }, function (error) {
39
+                            });
40
+                    };
41
+
42
+                    $scope.displayItem = function (item) {
43
+                        var ctrl = $scope.getEditController();
44
+                        dialogs.create('views/modals/' + ctrl.toLowerCase() + '.html', ctrl + 'Controller', item)
45
+                            .result.then(function (data) {
46
+                                $scope.loadPage($scope.page);
47
+                            });
48
+                    };
49
+
50
+                    $scope.removeItems = function () {
51
+                        if ($scope.selectedItems.length == 0) {
52
+                            $scope.loadPage($scope.page);
53
+                            return;
54
+                        }
55
+                        $scope.getDelPromise($scope.selectedItems[0], promiseDelItems)
56
+                            .then(function (data) {
57
+                                $scope.selectedItems.splice(0, 1);
58
+                                $scope.removeItems();
59
+                            }, function (error) {
60
+                                luticateDialogErrorHelper.errorDialog(error)
61
+                                    .result.then(function (data) {
62
+                                        $scope.loadPage($scope.page);
63
+                                    }, function (error) {
64
+                                    });
65
+                            });
66
+                    };
67
+
68
+                    $scope.addItem = function () {
69
+                        var ctrl = $scope.getEditController();
70
+                        dialogs.create('views/modals/' + ctrl.toLowerCase() + '.html', ctrl + 'Controller', null)
71
+                            .result.then(function (data) {
72
+                                $scope.loadPage($scope.page);
73
+                            });
74
+                    };
75
+
76
+                    $scope.toggleSelectedItem = function (id) {
77
+                        var idx = $scope.selectedItems.indexOf(id);
78
+                        if (idx > -1) {
79
+                            $scope.selectedItems.splice(idx, 1);
80
+                        }
81
+                        else {
82
+                            $scope.selectedItems.push(id);
83
+                        }
84
+                    };
85
+
86
+                    $scope.toggleSelectAll = function () {
87
+                        if ($scope.selectedItems.length == $scope.items.Data.length) {
88
+                            $scope.selectedItems = [];
89
+                        }
90
+                        else {
91
+                            $scope.selectedItems = [];
92
+                            for (var i = 0; i < $scope.items.Data.length; ++i) {
93
+                                $scope.selectedItems.push($scope.items.Data[i].Id);
94
+                            }
95
+                        }
96
+                    };
97
+
98
+                    $scope.loadPage($scope.page);
99
+                }
100
+            };
101
+        }
102
+    ]);
103
+
104
+angular.module('luticateUtils').run(['$templateCache', function($templateCache)
105
+{
106
+    $templateCache.put('/luticate/lutable.html', '<div lu-busy="itemList">' +
107
+'        <table class="col-sm-12 table table-hover">' +
108
+'    <thead>' +
109
+'    <tr>' +
110
+'    <th>' +
111
+'    <input type="checkbox" ng-click="toggleSelectAll()"' +
112
+'    ng-checked="selectedItems.length == items.Data.length && items.Data.length != 0">' +
113
+'        </th>' +
114
+'        <th class="col-sm-{{ col.width }}" ng-repeat="col in columns">{{ col.name }}</th>' +
115
+'</tr>' +
116
+'</thead>' +
117
+'<tbody>' +
118
+'<tr ng-repeat="item in items.Data" style="cursor: pointer" ng-click="displayItem(item)">' +
119
+'    <td>' +
120
+'    <input name="selectedItems[]" type="checkbox" ng-checked="selectedItems.indexOf(item.Id) > -1"' +
121
+ '   ng-click="$event.stopPropagation();toggleSelectedItem(item.Id)" >' +
122
+'        </td>' +
123
+'        <td ng-repeat="col in columns">{{ col.getValue(item) }}</td>' +
124
+'</tr>' +
125
+'</tbody>' +
126
+'</table>' +
127
+
128
+'<div class="col-sm-12 text-center">' +
129
+'    <a class="{{ p == page ? \'pagination-current\' : \'pagination-not-current\'}}" href="" ng-repeat="p in pages" ng-click="loadPage(p)">{{ p + 1 }}&nbsp;</a>' +
130
+'</div>' +
131
+
132
+'<div class="col-sm-12">' +
133
+'    <button class="btn btn-default" type="button" ng-click="removeItems()" ng-disabled="selectedItems.length == 0">' +
134
+'    <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Remove' +
135
+'    </button>' +
136
+'    <button class="btn btn-default" type="button" ng-click="addItem()">' +
137
+'    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add' +
138
+'    </button>' +
139
+'    </div>' +
140
+'    </div>');
141
+}]);

Ładowanie…
Anuluj
Zapisz