cs_tools.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. 'use strict';
  2. /**
  3. * cs_errorList:错误列表
  4. * 使用范围:
  5. * 台账分解(原报)、台账修订(原报)、计量台账(所有角色)
  6. *
  7. * posSearch & billsSearch:台账搜索相关
  8. * 使用范围:
  9. * 0号台账:台账分解、台账审批、台账修订、部位台账;
  10. * 期计量:计量台账、部位台账
  11. *
  12. * 所有工具均基于spreadjs,请放在gc.spread.sheets.all.10.0.1.min.js/spreadjs_zh.js之后
  13. *
  14. * @author Mai
  15. * @date
  16. * @version
  17. */
  18. const showSideTools = function (show) {
  19. const left = $('#left-view'), right = $('#right-view'), parent = left.parent();
  20. if (show) {
  21. right.show();
  22. autoFlashHeight();
  23. /**
  24. * right.show()后, parent被撑开成2倍left.height, 导致parent.width减少了10px
  25. * 第一次left.width调整后,parent的缩回left.height, 此时parent.width又增加了10px
  26. * 故需要通过最终的parent.width再计算一次left.width
  27. *
  28. * Q: 为什么不通过先计算left.width的宽度,以避免计算两次left.width?
  29. * A: 右侧工具栏不一定显示,当右侧工具栏显示过一次后,就必须使用parent和right来计算left.width
  30. *
  31. */
  32. //left.css('width', parent.width() - right.outerWidth());
  33. //left.css('width', parent.width() - right.outerWidth());
  34. const percent = 100 - right.outerWidth() /parent.width() * 100;
  35. left.css('width', percent + '%');
  36. } else {
  37. left.width(parent.width());
  38. right.hide();
  39. }
  40. };
  41. (function($){
  42. /**
  43. * 错误列表
  44. * @param setting
  45. * {
  46. * tabSelector: 'a[content=#error-list]',
  47. * selector: '#error-list',
  48. * relaSpread: ledgerSpread,
  49. * storeKey: 'ledger-error-' + tenderId,
  50. * }
  51. * @returns {{spread: *}}
  52. */
  53. $.cs_errorList = function (setting) {
  54. if (!setting.selector || !setting.relaSpread) return;
  55. if (!setting.spreadSetting) {
  56. setting.spreadSetting = {
  57. cols: [
  58. {title: '行号', field: 'serialNo', width: 80, formatter: '@'},
  59. {title: '清单编号', field: 'b_code', width: 150, formatter: '@'},
  60. {title: '清单名称', field: 'name', width: 230, formatter: '@'},
  61. ],
  62. emptyRows: 0,
  63. headRows: 1,
  64. headRowHeight: [32],
  65. defaultRowHeight: 21,
  66. headerFont: '12px 微软雅黑',
  67. font: '12px 微软雅黑',
  68. selectedBackColor: '#fffacd',
  69. readOnly: true,
  70. };
  71. }
  72. const resultId = setting.id + '-spread';
  73. const obj = $(setting.selector);
  74. obj.html(
  75. ' <div id="' + resultId + '" class="sjs-sh">\n' +
  76. ' </div>'
  77. );
  78. autoFlashHeight();
  79. const spread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  80. const sheet = spread.getActiveSheet();
  81. SpreadJsObj.initSheet(sheet, setting.spreadSetting);
  82. SpreadJsObj.forbiddenSpreadContextMenu('#' + resultId, spread);
  83. spread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  84. const sheet = info.sheet;
  85. const data = sheet.zh_data;
  86. if (!data) { return }
  87. const curBills = data[info.row];
  88. if (!curBills) { return }
  89. SpreadJsObj.locateTreeNode(setting.relaSpread.getActiveSheet(), curBills.ledger_id, true);
  90. console.log(curBills);
  91. if (setting.afterLocated) {
  92. setting.afterLocated();
  93. }
  94. });
  95. const loadErrorData = function (data, his = false) {
  96. data.sort(function (a, b) {
  97. return a.serialNo - b.serialNo;
  98. });
  99. SpreadJsObj.loadSheetData(sheet, SpreadJsObj.DataType.Data, data);
  100. if (!his && setting.storeKey) {
  101. setLocalCache(setting.storeKey, JSON.stringify(data));
  102. }
  103. $(setting.tabSelector).show();
  104. };
  105. const clearErrorData = function () {
  106. if (setting.storeKey) removeLocalCache(setting.storeKey);
  107. };
  108. if (setting.storeKey) {
  109. const storeStr = getLocalCache(setting.storeKey);
  110. const storeData = storeStr ? JSON.parse(storeStr) : [];
  111. if (storeData.length > 0) loadErrorData(storeData, true);
  112. }
  113. const showErrorList = function () {
  114. const tab = $(setting.tabSelector), tabPanel = $(tab.attr('content'));
  115. $('a', '#side-menu').removeClass('active');
  116. tab.addClass('active');
  117. $('.tab-content .tab-pane').removeClass('active');
  118. tabPanel.addClass('active');
  119. showSideTools(true);
  120. spread.refresh();
  121. if (setting.afterShow) setting.afterShow();
  122. };
  123. return {spread: spread, loadErrorData: loadErrorData, clearErrorData: clearErrorData, show: showErrorList};
  124. };
  125. $.posSearch = function (setting) {
  126. if (!setting.selector || !setting.searchSpread) return;
  127. const searchHtml =
  128. ' <div class="ml-2">\n' +
  129. ' <div class="input-group input-group-sm">\n' +
  130. ' <input type="text" class="form-control" placeholder="输入名称查找" id="pos-keyword">\n' +
  131. ' <div class="input-group-append">\n' +
  132. ' <span class="input-group-text" id="pos-search-hint">结果:0</span>\n' +
  133. ' </div>\n' +
  134. ' <div class="input-group-append" >\n' +
  135. ' <button class="btn btn-outline-secondary" type="button" title="上一个" id="search-pre-pos"><i class="fa fa-angle-double-left"></i></button>\n' +
  136. ' <button class="btn btn-outline-secondary" type="button" title="下一个" id="search-next-pos"><i class="fa fa-angle-double-right"></i></button>\n' +
  137. ' </div>\n' +
  138. ' </div>\n' +
  139. ' </div>\n';
  140. $(setting.selector).html(searchHtml);
  141. const sheet = setting.searchSpread.getActiveSheet();
  142. const searchObj = (function () {
  143. let resultArr = [];
  144. const search = function (keyword) {
  145. if (keyword && keyword !== '') {
  146. resultArr = [];
  147. const sortData = sheet.zh_data;
  148. if (sortData) {
  149. for (let i = 0, iLength = sortData.length; i < iLength; i++) {
  150. const sd = sortData[i];
  151. if (sd.name && sd.name.indexOf(keyword) > -1) {
  152. resultArr.push({index: i, data: sd});
  153. }
  154. }
  155. }
  156. } else {
  157. resultArr = [];
  158. }
  159. $('#pos-search-hint').html('结果:' + resultArr.length);
  160. };
  161. const searchAndLocate = function (keyword) {
  162. search(keyword);
  163. if (resultArr.length > 0) {
  164. const sel = sheet.getSelections()[0];
  165. const curRow = sel ? sel.row : 0;
  166. const pos = resultArr[0];
  167. if (pos.index !== curRow) {
  168. sheet.setSelection(pos.index, sel ? sel.col : 0, 1, 1);
  169. sheet.getParent().focus();
  170. sheet.showRow(pos.index, spreadNS.VerticalPosition.center);
  171. }
  172. }
  173. };
  174. const locateNext = function () {
  175. if (resultArr.length > 0) {
  176. const sel = sheet.getSelections()[0];
  177. const curRow = sel ? sel.row : 0;
  178. let next = _.find(resultArr, function (d) {
  179. return d.index > curRow;
  180. });
  181. if (!next) next = resultArr[0];
  182. if (next.index !== curRow) {
  183. sheet.setSelection(next.index, sel ? sel.col : 0, 1, 1);
  184. sheet.getParent().focus();
  185. sheet.showRow(next.index, spreadNS.VerticalPosition.center);
  186. }
  187. }
  188. };
  189. const locatePre = function () {
  190. if (resultArr.length > 0) {
  191. const sel = sheet.getSelections()[0];
  192. const curRow = sel ? sel.row : 0;
  193. let next = _.findLast(resultArr, function (d) {
  194. return d.index < curRow;
  195. });
  196. if (!next) next = resultArr[resultArr.length - 1];
  197. if (next.index !== curRow) {
  198. sheet.setSelection(next.index, sel ? sel.col : 0, 1, 1);
  199. sheet.getParent().focus();
  200. sheet.showRow(next.index, spreadNS.VerticalPosition.center);
  201. }
  202. }
  203. };
  204. return {search, searchAndLocate, locateNext, locatePre};
  205. })();
  206. // $('#pos-keyword').bind('input propertychange', function () {
  207. // posSearch.search(this.value);
  208. // });
  209. $('#pos-keyword').bind('keydown', function(e){
  210. if (e.keyCode == 13) searchObj.searchAndLocate(this.value);
  211. });
  212. $('#search-pre-pos').click(function () {
  213. searchObj.locatePre();
  214. });
  215. $('#search-next-pos').click(function () {
  216. searchObj.locateNext();
  217. });
  218. return searchObj;
  219. };
  220. $.billsSearch = function (setting) {
  221. if (!setting.selector || !setting.searchSpread || !setting.resultSpreadSetting) return;
  222. if (!setting.searchRangeStr) setting.searchRangeStr = '项目节编号/清单编号/名称';
  223. const resultId = setting.id + '-search-result';
  224. const obj = $(setting.selector);
  225. obj.html(
  226. ' <div class="sjs-bar">\n' +
  227. ' <div class="input-group input-group-sm pb-1">\n' +
  228. ' <div class="input-group-prepend">\n' +
  229. (setting.searchOver ? ' <div class="input-group-text"><input type="radio" name="searchType" id="over"> 超计</div>\n' : '') +
  230. (setting.searchEmpty ? ' <div class="input-group-text"><input type="radio" name="searchType" id="empty"> 漏计</div>\n' : '') +
  231. ' </div>' +
  232. ' <input id="searchKeyword" type="text" class="form-control" placeholder="可查找 ' + setting.searchRangeStr + '" aria-label="Recipient\'s username" aria-describedby="button-addon2">\n' +
  233. ' <div class="input-group-append">\n' +
  234. ' <button class="btn btn-outline-secondary" type="button"">搜索</button>\n' +
  235. ' </div>\n' +
  236. ' </div>\n' +
  237. ' </div>\n' +
  238. ' <div id="' + resultId + '" class="sjs-sh">\n' +
  239. ' </div>'
  240. );
  241. autoFlashHeight();
  242. const resultSpread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  243. SpreadJsObj.initSheet(resultSpread.getActiveSheet(), setting.resultSpreadSetting);
  244. SpreadJsObj.forbiddenSpreadContextMenu('#' + resultId, resultSpread);
  245. const searchSheet = setting.searchSpread.getActiveSheet();
  246. let searchResult = [];
  247. const searchBills = function () {
  248. const keyword = $('#searchKeyword', obj).val();
  249. searchResult = [];
  250. const sortData = SpreadJsObj.getSortData(searchSheet);
  251. for (const node of sortData) {
  252. if ((node.code && node.code.indexOf(keyword) > -1) ||
  253. node.b_code && node.b_code.indexOf(keyword) > -1 ||
  254. node.name && node.name.indexOf(keyword) > -1) {
  255. const data = JSON.parse(JSON.stringify(node));
  256. data.visible = true;
  257. searchResult.push(data);
  258. }
  259. }
  260. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  261. };
  262. const calculateCompletePercent = function (searchResult) {
  263. if (!searchResult) return;
  264. for (const sr of searchResult) {
  265. const base = ZhCalc.add(sr.total_price, sr.end_qc_tp);
  266. sr.complete_percent = base !== 0 ? ZhCalc.mul(ZhCalc.div(sr.end_gather_tp, base), 100, 2) : 0;
  267. }
  268. };
  269. const searchOver = function () {
  270. searchResult = [];
  271. const sortData = SpreadJsObj.getSortData(searchSheet);
  272. for (const node of sortData) {
  273. if (node.children && node.children.length > 0) continue;
  274. if (setting.checkOver && setting.checkOver(node)) {
  275. const data = JSON.parse(JSON.stringify(node));
  276. data.visible = true;
  277. searchResult.push(data);
  278. }
  279. }
  280. calculateCompletePercent(searchResult);
  281. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  282. };
  283. const searchEmpty = function () {
  284. searchResult = [];
  285. const sortData = SpreadJsObj.getSortData(searchSheet);
  286. for (const node of sortData) {
  287. if (node.children && node.children.length > 0) continue;
  288. if (setting.checkEmpty && setting.checkEmpty(node)) {
  289. const data = JSON.parse(JSON.stringify(node));
  290. data.visible = true;
  291. searchResult.push(data);
  292. }
  293. }
  294. calculateCompletePercent(searchResult);
  295. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  296. };
  297. const searchLess = function () {
  298. searchResult = [];
  299. const sortData = SpreadJsObj.getSortData(searchSheet);
  300. for (const node of sortData) {
  301. if (node.children && node.children.length > 0) continue;
  302. if (setting.checkLess && setting.checkLess(node)) {
  303. const data = JSON.parse(JSON.stringify(node));
  304. data.visible = true;
  305. searchResult.push(data);
  306. }
  307. }
  308. calculateCompletePercent(searchResult);
  309. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  310. };
  311. $('input', obj).bind('keydown', function (e) {
  312. if (e.keyCode == 13) searchBills();
  313. });
  314. $('button', obj).bind('click', () => {searchBills()});
  315. $('#over', obj).bind('change', () => {searchOver()});
  316. $('#empty', obj).bind('change', () => {searchLess()});
  317. resultSpread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  318. const sheet = info.sheet;
  319. const data = sheet.zh_data;
  320. if (!data) { return }
  321. const curBills = data[info.row];
  322. if (!curBills) { return }
  323. SpreadJsObj.locateTreeNode(searchSheet, curBills.ledger_id, true);
  324. if (setting.afterLocated) {
  325. setting.afterLocated();
  326. }
  327. });
  328. return {spread: resultSpread};
  329. };
  330. })(jQuery);