jstreeAJAX.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. $(document).ready(function () {
  2. const types = ["工程概要", "工程計算書", "設計圖", "數量計算書", "工程預算書", "施工規範及特定條款", "設計報告書"];
  3. var path = '';
  4. var category = '';
  5. var project_id = '';
  6. var project_name = '';
  7. var project = '';
  8. var blueprint_SN = '';
  9. var blueprint_path = '';
  10. var notes = [];
  11. var tempTable = [];
  12. var tempTable2 = [];
  13. var tree_data; //save getTree ajax data
  14. var start = true;
  15. var folderPath = "";
  16. $('#datatable-ajax').dataTable({
  17. bProcessing: true,
  18. responsive: true,
  19. "pagingType": "full_numbers",
  20. "bLengthChange": false,
  21. "searching": false,
  22. "pageLength": 7,
  23. "language": {
  24. "processing": "處理中...",
  25. "loadingRecords": "載入中...",
  26. "lengthMenu": "顯示 _MENU_ 項結果",
  27. "zeroRecords": "沒有符合的結果或是沒有資料",
  28. "info": "顯示第 _START_ 至 _END_ 項結果,共 _TOTAL_ 項",
  29. "infoEmpty": "顯示第 0 至 0 項結果,共 0 項",
  30. "infoFiltered": "(從 _MAX_ 項結果中過濾)",
  31. "infoPostFix": "",
  32. "search": "搜尋:",
  33. "paginate": {
  34. "first": '<i class="fa fa-step-backward"></i>',
  35. "previous": '<i class="fa fa-backward"></i>',
  36. "next": '<i class="fa fa-forward"></i>',
  37. "last": '<i class="fa fa-step-forward"></i>'
  38. },
  39. "aria": {
  40. "sortAscending": ": 升冪排列",
  41. "sortDescending": ": 降冪排列"
  42. }
  43. },
  44. "columns": [{
  45. "width": "5%"
  46. },
  47. null,
  48. null,
  49. {
  50. "width": "5%"
  51. },
  52. ],
  53. data: [
  54. [
  55. " ", "", "", "",
  56. ],
  57. [
  58. " ", "", "", "",
  59. ],
  60. [
  61. " ", "", "", "",
  62. ],
  63. [
  64. " ", "", "", "",
  65. ],
  66. [
  67. " ", "", "", "",
  68. ],
  69. [
  70. " ", "", "", "",
  71. ],
  72. [
  73. " ", "", "", "",
  74. ]
  75. ],
  76. drawCallback: function () {
  77. if (!start) {
  78. var api = this.api();
  79. var rowCount = api.rows({
  80. page: 'current'
  81. }).count();
  82. for (var i = 0; i < api.page.len() - (rowCount === 0 ? 1 : rowCount); i++) {
  83. $('#datatable-ajax tbody').append($("<tr ><td>&nbsp;</td><td></td><td></td><td></td></tr>"));
  84. }
  85. }
  86. start = false;
  87. }
  88. });
  89. $.ajax({
  90. url: "./script/php/getTree.php",
  91. contentType: "application/json",
  92. dataType: "json",
  93. type: "GET",
  94. async: false
  95. })
  96. .success(function (response) {
  97. tree_data = response;
  98. }).error(function (error) {
  99. console.log(error);
  100. });
  101. var table = $('#datatable-ajax').DataTable();
  102. displayDetail(false);
  103. $('#treeAjaxHTML').jstree({
  104. 'core': {
  105. 'themes': {
  106. variant: 'large'
  107. },
  108. 'data': tree_data,
  109. 'multiple': false,
  110. 'animation': 40,
  111. 'expand_selected_onload': true
  112. },
  113. 'types': {
  114. 'default': {
  115. 'icon': 'fa fa-folder'
  116. },
  117. 'file': {
  118. 'icon': 'fa fa-file'
  119. }
  120. },
  121. 'plugins': ['types', 'search', 'wholerow']
  122. }).bind("loaded.jstree", function (event, data) {
  123. var searchURL = window.location.search;
  124. searchURL = searchURL.substring(1, searchURL.length);
  125. if (searchURL.includes("=")) {
  126. type = decodeURIComponent(searchURL.split("&")[0].split("=")[1]);
  127. $(this).jstree("open_all");
  128. }
  129. }).on(
  130. 'select_node.jstree',
  131. function (event, data) {
  132. if (data.node.text.includes("-")) {
  133. $("#folderPath").empty();
  134. displayDetail(false);
  135. project_id = data.node.text.split("-")[0];
  136. project_name = data.node.text.split("-")[1];
  137. project = data.node.text;
  138. $("#project-id").val(project_id)
  139. $("#header").text(project);
  140. category = $('#treeAjaxHTML').jstree(true).get_node(data.node.parents[0]).text;
  141. let i = 1;
  142. table.clear().draw();
  143. tempTable = [];
  144. types.forEach(function (data) {
  145. let temp = [
  146. i,
  147. data,
  148. category,
  149. '',
  150. project,
  151. ];
  152. table.row.add(temp).draw();
  153. tempTable.push(temp);
  154. i++;
  155. });
  156. $("#tableDetail td").empty();
  157. } else {
  158. data.instance.toggle_node(data.node);
  159. }
  160. });
  161. $('#btn-search').click(function () {
  162. $("#folderPath").empty();
  163. $("#header").text("搜尋結果");
  164. $.ajax({
  165. url: "./script/php/search.php",
  166. data: {
  167. search: $("#search").val(),
  168. },
  169. async: false,
  170. contentType: "application/json",
  171. dataType: "json",
  172. type: "GET",
  173. })
  174. .success(function (response) {
  175. displayDetail(true);
  176. table.clear().draw();
  177. $("#tableDetail td").empty();
  178. response.forEach(function (data) {
  179. let temp = [
  180. data["SN"],
  181. data["original_name"].slice(6),
  182. data["keyword"],
  183. '1.0.0',
  184. data["project_id"],
  185. data["project_name"],
  186. data["type_id"],
  187. data["category_name"],
  188. data["notes"],
  189. data["filename"],
  190. ];
  191. table.row.add(temp).draw();
  192. i++;
  193. });
  194. })
  195. .error(function (error) {
  196. console.log(error);
  197. })
  198. .complete(function () {});
  199. });
  200. $('#search').keyup(function (e) {
  201. if (e.keyCode == 13) {
  202. $('#btn-search').click();
  203. }
  204. });
  205. $('#reset').click(function () {
  206. folderPath.pop();
  207. let index = folderPath.length;
  208. $("#folderPath").children().eq(index).remove();
  209. table.clear().draw();
  210. if(is_blueprint2){
  211. tempTable2.forEach(function(data) {
  212. table.row.add(data).draw();
  213. });
  214. is_blueprint2 = false;
  215. }else{
  216. tempTable.forEach(function(data) {
  217. table.row.add(data).draw();
  218. });
  219. }
  220. $("#tableDetail td").empty();
  221. displayDetail(false);
  222. });
  223. var is_blueprint2 = false;
  224. $('#datatable-ajax tbody').on('click', 'tr', function () {
  225. $(this).parent().find("tr").css('background-color', '');
  226. $(this).css("background-color", "#cccccc");
  227. $("#tableDetail td").empty();
  228. var data = table.row(this).data();
  229. if (data == null) {
  230. data = ["", "", "null", "", "", "", "", "", "", ""];
  231. }
  232. let project_id = data[4].split("-")[0];
  233. let SN = data[0];
  234. let is_blueprint = data[1] == "設計圖" ? true : false;
  235. let note = notes.includes(data[1]) ? data[1] : '';
  236. if (data[2] == 'null') {
  237. $("#tableDetail td").empty();
  238. } else if (data[3] == '') {
  239. path = data[1];
  240. $.ajax({
  241. url: "./script/php/getDetail.php",
  242. data: {
  243. project_id: project_id,
  244. type_id: SN,
  245. is_blueprint: is_blueprint,
  246. note: note
  247. },
  248. async: false,
  249. contentType: "application/json",
  250. dataType: "json",
  251. type: "GET",
  252. })
  253. .success(function (response) {
  254. var span = $('<span>' + data[1] + ' > </span>');
  255. span.on("click", function (e) {
  256. let clicked = $(e.target).text().split(" > ")[0];
  257. let index = folderPath.indexOf(clicked);
  258. for(k = index+1; k < folderPath.length; k++){
  259. $("#folderPath").children().eq(k).remove();
  260. folderPath.pop();
  261. }
  262. addTable(data, notes, response, SN, path);
  263. is_blueprint2 = false;
  264. });
  265. $("#folderPath").append(span);
  266. folderPath = $("#folderPath").text().split(" > ");
  267. folderPath.pop();
  268. addTable(data, notes, response, SN, path);
  269. })
  270. .error(function (error) {
  271. console.log(error);
  272. })
  273. .complete(function () {});
  274. } else {
  275. let type = types[parseInt(data[6]) - 1];
  276. let path = '';
  277. //let category = data[8];
  278. if (data[6] == 3) {
  279. path = data[7] + "/" + data[4] + "-" + data[5] + "/" + data[6] + "_" + type + "/" + data[8] + "/" + data[9];
  280. } else {
  281. path = data[7] + "/" + data[4] + "-" + data[5] + "/" + data[6] + "_" + type + "/" + data[9];
  282. }
  283. $("#plan_code").text(data[4]);
  284. $("#plan_name").text(data[5]);
  285. $("#download").append('<img src="./assets/images/' + 'pdf-icon.png" style="height: 30px; display: inline-block;"> ');
  286. $("#download").append('<a target="_blank" href="./assets/建置資料/' + path + '">' + data[9] + '</a>');
  287. $("#version").text(data[3]);
  288. $("#fileName").text(data[1]);
  289. }
  290. });
  291. function displayDetail(status) {
  292. if (status) {
  293. table.column(2).visible(true);
  294. table.column(3).visible(true);
  295. $(".table-responsive").show();
  296. $("#SN").text("圖號");
  297. $("#name").text("圖名");
  298. } else {
  299. table.column(2).visible(false);
  300. table.column(3).visible(false);
  301. $(".table-responsive").hide();
  302. $("#SN").text("項次");
  303. $("#name").text("類別");
  304. }
  305. }
  306. function addTable(data, notes, response, SN, path) {
  307. table.clear().draw();
  308. if (data[1] != "設計圖") {
  309. displayDetail(true);
  310. if (notes.includes(data[1])) {
  311. is_blueprint2 = true;
  312. }
  313. for (i = 0; i < response.length; i++) {
  314. table.row.add([
  315. response[i]["SN"],
  316. response[i]["original_name"].slice(6),
  317. response[i]["keyword"],
  318. '1.0.0',
  319. response[i]["project_id"],
  320. response[i]["project_name"],
  321. response[i]["type_id"],
  322. response[i]["category_name"],
  323. response[i]["notes"],
  324. response[i]["filename"],
  325. ])
  326. table.draw();
  327. }
  328. } else {
  329. blueprint_SN = SN;
  330. blueprint_path = path;
  331. tempTable2 = [];
  332. for (i = 0; i < response.length; i++) {
  333. notes[i] = response[i]['notes'];
  334. let temp = [
  335. i + 1,
  336. response[i]['notes'],
  337. '',
  338. '',
  339. data[4],
  340. ];
  341. table.row.add(temp);
  342. tempTable2.push(temp);
  343. }
  344. table.draw();
  345. }
  346. }
  347. });