소스 검색

basic display table

tags/0.1.0
Robin Thoni 9 년 전
부모
커밋
135b73b4cf
1개의 변경된 파일141개의 추가작업 그리고 0개의 파일을 삭제
  1. 141
    0
      src/lubasictable.js

+ 141
- 0
src/lubasictable.js 파일 보기

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

Loading…
취소
저장