jstreeAJAX.js 15 KB

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