jstreeAJAX.js 14 KB

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