jstreeAJAX.js 12 KB

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