|
13 | 13 | <script src="../semantic/semantic.js"></script> |
14 | 14 |
|
15 | 15 | <style> |
16 | | - td.actions > a { |
17 | | - min-width: 5em; |
18 | | - } |
19 | | - td.actions > a:not(:first-child) { |
20 | | - border-left: 2px solid #a333c8; |
21 | | - padding-left: 4px; |
22 | | - } |
23 | 16 | </style> |
| 17 | + |
| 18 | + <script> |
| 19 | + // 載入資料 |
| 20 | + function load() { |
| 21 | + $.get('category_ajax.php').then(function(response) { |
| 22 | + // 伺服器回傳資料,檢查是否成功 |
| 23 | + if (response['success']) { |
| 24 | + return response['results'] |
| 25 | + } else { |
| 26 | + return $.Deferred().reject(response['reason']) |
| 27 | + } |
| 28 | + }).then(function(results) { |
| 29 | + console.dir(results) |
| 30 | + }).fail(function(reason) { |
| 31 | + console.error(reason) |
| 32 | + $('#load-error-modal').modal('show'); |
| 33 | + }) |
| 34 | + } |
| 35 | + |
| 36 | + $(function() { |
| 37 | + // 設定無法載入資料時 |
| 38 | + // 所顯示的 Modal |
| 39 | + $('#load-error-modal').modal({ |
| 40 | + // 使用者一定要按其中兩個按鈕,才能夠繼續 |
| 41 | + closable: false, |
| 42 | + |
| 43 | + // 只是好像很好玩而已 |
| 44 | + transition: 'drop', |
| 45 | + |
| 46 | + // 因為使用 basic 樣式會變得很奇怪, |
| 47 | + // 所以關掉 |
| 48 | + // blurring: true, |
| 49 | + |
| 50 | + // 按下重新載入的 Callback |
| 51 | + onApprove: function() { |
| 52 | + // 再一次試圖載入 |
| 53 | + load() |
| 54 | + }, |
| 55 | + |
| 56 | + // 按下取消的 Callback |
| 57 | + onDeny: function() { |
| 58 | + // 應該在表格顯示畫面 |
| 59 | + } |
| 60 | + |
| 61 | + }) |
| 62 | + |
| 63 | + load(); |
| 64 | + }) |
| 65 | + </script> |
24 | 66 | </head> |
25 | 67 | <body> |
26 | 68 | <div class="ui container"> |
|
34 | 76 | 分類 |
35 | 77 | </h1> |
36 | 78 |
|
37 | | - <?php |
38 | | - include './../util/connect.php'; |
39 | | - ?> |
40 | | - |
41 | | - <?php |
42 | | - function category_count($categoryID) { |
43 | | - global $mysqli; |
44 | | - $stmt = $mysqli -> prepare('SELECT COUNT(*) FROM group_12.products WHERE categories = ' . intval($categoryID)); |
45 | | - $stmt -> bind_result($c); |
46 | | - $stmt -> execute(); |
47 | | - $stmt -> fetch(); |
48 | | - |
49 | | - if (is_null($c) || !isset($c)){ |
50 | | - $c = 0; |
51 | | - } |
52 | | - |
53 | | - return $c; |
54 | | - } |
55 | | - ?> |
56 | | - |
57 | | - <?php |
58 | | - $result = $mysqli->query("SELECT * FROM categories WHERE parent IS NULL"); |
59 | | - $total = mysqli_num_rows($result); |
60 | | - $categories = array(); |
61 | | - |
62 | | - for ($i = 0; $i < $total; $i++) { |
63 | | - $row = mysqli_fetch_row($result); |
64 | | - |
65 | | - array_push($categories, array( |
66 | | - "name" => $row[1], |
67 | | - "id" => $row[0], |
68 | | - "parent" => $row[2], |
69 | | - "depth" => 0, |
70 | | - "children" => array() |
71 | | - )); |
72 | | - } |
73 | | - |
74 | | - mysqli_free_result($result); |
75 | | - |
76 | | - foreach ($categories as &$category) { |
77 | | - echo 'Iterating ' . $category['name'] . '<br/>'; |
78 | | - $id = $category['id']; |
79 | | - $category['item'] = category_count($id); |
80 | | - $category['siblingHasItems'] = false; |
81 | | - } |
82 | | - |
83 | | - unset($category); |
84 | | - |
85 | | - function organize($id) |
86 | | - { |
87 | | - global $mysqli; |
88 | | - $result = $mysqli->query("SELECT * from categories WHERE parent = {$id}"); |
89 | | - if ($result == false) { |
90 | | - return array(); |
91 | | - } |
92 | | - $row_count = mysqli_num_rows($result); |
93 | | - $data = array(); |
94 | | - for ($i = 0; $i < $row_count; $i++) { |
95 | | - $row = mysqli_fetch_row($result); |
96 | | - |
97 | | - array_push($data, array( |
98 | | - "name" => $row[1], |
99 | | - "id" => $row[0], |
100 | | - "parent" => $row[2], |
101 | | - "depth" => 0, |
102 | | - "children" => array() |
103 | | - )); |
104 | | - } |
105 | | - mysqli_free_result($result); |
106 | | - |
107 | | - $someHasItems = false; |
108 | | - |
109 | | - foreach ($data as &$category) { |
110 | | - $id = $category['id']; |
111 | | - $category['item'] = category_count($id); |
112 | | - |
113 | | - if ($category['item'] > 0) { |
114 | | - $someHasItems = true; |
115 | | - } |
116 | | - } |
117 | | - |
118 | | - foreach ($data as &$category) { |
119 | | - $category['siblingHasItems'] = $someHasItems; |
120 | | - } |
121 | | - |
122 | | - unset($category); |
123 | | - |
124 | | - return $data; |
125 | | - } |
126 | | - |
127 | | - function recurseOnCategory(array &$category) |
128 | | - { |
129 | | - $category["children"] = organize($category["id"]); |
130 | | - $depth = 1; |
131 | | - foreach ($category["children"] as $key => &$value) { |
132 | | - $newDepth = recurseOnCategory($value) + 1; |
133 | | - if ($newDepth > $depth) { |
134 | | - $depth = $newDepth; |
135 | | - } |
136 | | - } |
137 | | - |
138 | | - $category["depth"] = $depth; |
139 | | - return $depth; |
140 | | - } |
141 | | - |
142 | | - foreach ($categories as &$category) { |
143 | | - recurseOnCategory($category); |
144 | | - } |
145 | | - |
146 | | - unset($category); |
147 | | - |
148 | | - ?> |
149 | | - <pre> |
150 | | - <?php |
151 | | - print_r(json_encode($categories, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); |
152 | | - ?> |
153 | | - </pre> |
154 | | - |
155 | | - <?php |
156 | | - // 先自己,再小孩 |
157 | | - function render($depth, array $self) { |
158 | | - ?> |
159 | | - <tr> |
160 | | - <?php |
161 | | - if ($depth > 0) { |
162 | | - ?> |
163 | | - <td colspan="<?=$depth?>"></td> |
164 | | - <?php |
165 | | - } |
166 | | - ?> |
167 | | - <td colspan="<?=(3 - $depth)?>"><?=$self['name']?></td> |
168 | | - <td><?=$self['item']?></td> |
169 | | - <td class="actions"> |
170 | | - <?php |
171 | | - if ($depth != 2 && $self['item'] == 0 || !empty($self['children'])) { |
172 | | - ?> |
173 | | - <a href="category_new.php?id=<?=$self['id']?>">新增子分類</a> |
174 | | - <?php |
175 | | - } |
176 | | - ?> |
177 | | - <a href="category_edit.php?id=<?=$self['id']?>">編輯名稱</a> |
178 | | - <?php |
179 | | - if ($self['item'] == 0 && empty($self['children']) && !$self['siblingHasItems']) { |
180 | | - ?> |
181 | | - <a href="category_delete.php?id=<?=$self['id']?>">刪除分類</a> |
182 | | - <?php |
183 | | - } |
184 | | - ?> |
185 | | - </td> |
186 | | - </tr> |
187 | | - <?php |
188 | | - |
189 | | - if (!empty($self['children'])) { |
190 | | - foreach ($self['children'] as $child) { |
191 | | - render($depth + 1, $child); |
192 | | - } |
193 | | - } |
194 | | - } |
195 | | - ?> |
196 | | - |
197 | | - <?php |
198 | | - foreach ($categories as $category) { |
199 | | - ?> |
| 79 | + <div class="basic ui segment"> |
200 | 80 | <table class="ui celled structured table"> |
201 | | - <thead> |
| 81 | + <thead id="header"> |
202 | 82 | <tr> |
203 | | - <th colspan="3">名稱</th> |
| 83 | + <th colspan="3" style="width: 45%">名稱</th> |
204 | 84 | <th style="width: 25%;">產品數量</th> |
205 | | - <th style="width: 25%;">動作</th> |
206 | | - </tr> |
207 | | - <tr style=""> |
208 | | - <th style="width: 12.5%;">第一層</th> |
209 | | - <th style="width: 12.5%;">第二層</th> |
210 | | - <th style="width: 12.5%;">第三層</th> |
211 | | - <th colspan="3" style="width: 62.5%;"></th> |
| 85 | + <th style="width: 30%;">動作</th> |
212 | 86 | </tr> |
213 | 87 | </thead> |
214 | | - <tbody> |
215 | | - <?php |
216 | | - render(0, $category); |
217 | | - ?> |
| 88 | + <tbody id="content"> |
218 | 89 | </tbody> |
219 | 90 | </table> |
220 | | - <?php |
221 | | - } |
222 | | - ?> |
| 91 | + </div> |
| 92 | +</div> |
223 | 93 |
|
224 | | - <?php |
225 | | - include './../util/close.php'; |
226 | | - ?> |
| 94 | +<div id="load-error-modal" class="ui basic small modal"> |
| 95 | + <h2 class="ui red icon header"> |
| 96 | + <i class="warning icon"></i> |
| 97 | + <div style="padding-left: 0;" class="content">錯誤</div> |
| 98 | + </h2> |
| 99 | + <div class="centered content"> |
| 100 | + <p>載入資料時發生錯誤,是否重新載入?</p> |
| 101 | + </div> |
| 102 | + <div class="actions"> |
| 103 | + <div style="color: black;" class="ui yellow cancel button">取消</div> |
| 104 | + <div class="ui blue approve button">重新載入</div> |
| 105 | + </div> |
227 | 106 | </div> |
228 | 107 | </body> |
229 | 108 | </html> |
0 commit comments