ledger_search.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. 'use strict';
  2. /**
  3. * 台账搜索相关
  4. * 多个页面均使用:
  5. * 0好台账:台账分解、台账审批、台账修订、部位台账
  6. * 期计量:计量台账、部位台账
  7. *
  8. * 搜索基于spreadjs,请放在gc.spread.sheets.all.10.0.1.min.js/spreadjs_zh.js之后
  9. *
  10. * @author Mai
  11. * @date
  12. * @version
  13. */
  14. (function($){
  15. $.posSearch = function (setting) {
  16. if (!setting.selector || !setting.searchSpread) return;
  17. const searchHtml =
  18. ' <div class="ml-2">\n' +
  19. ' <div class="input-group input-group-sm">\n' +
  20. ' <input type="text" class="form-control" placeholder="输入名称查找" id="pos-keyword">\n' +
  21. ' <div class="input-group-append">\n' +
  22. ' <span class="input-group-text" id="pos-search-hint">结果:0</span>\n' +
  23. ' </div>\n' +
  24. ' <div class="input-group-append" >\n' +
  25. ' <button class="btn btn-outline-secondary" type="button" title="上一个" id="search-pre-pos"><i class="fa fa-angle-double-left"></i></button>\n' +
  26. ' <button class="btn btn-outline-secondary" type="button" title="下一个" id="search-next-pos"><i class="fa fa-angle-double-right"></i></button>\n' +
  27. ' </div>\n' +
  28. ' </div>\n' +
  29. ' </div>\n';
  30. $(setting.selector).html(searchHtml);
  31. const sheet = setting.searchSpread.getActiveSheet();
  32. const searchObj = (function () {
  33. let resultArr = [];
  34. const search = function (keyword) {
  35. if (keyword && keyword !== '') {
  36. resultArr = [];
  37. const sortData = sheet.zh_data;
  38. if (sortData) {
  39. for (let i = 0, iLength = sortData.length; i < iLength; i++) {
  40. const sd = sortData[i];
  41. if (sd.name && sd.name.indexOf(keyword) > -1) {
  42. resultArr.push({index: i, data: sd});
  43. }
  44. }
  45. }
  46. } else {
  47. resultArr = [];
  48. }
  49. $('#pos-search-hint').html('结果:' + resultArr.length);
  50. };
  51. const searchAndLocate = function (keyword) {
  52. search(keyword);
  53. if (resultArr.length > 0) {
  54. const sel = sheet.getSelections()[0];
  55. const curRow = sel ? sel.row : 0;
  56. const pos = resultArr[0];
  57. if (pos.index !== curRow) {
  58. sheet.setSelection(pos.index, sel ? sel.col : 0, 1, 1);
  59. sheet.getParent().focus();
  60. sheet.showRow(pos.index, spreadNS.VerticalPosition.center);
  61. }
  62. }
  63. };
  64. const locateNext = function () {
  65. if (resultArr.length > 0) {
  66. const sel = sheet.getSelections()[0];
  67. const curRow = sel ? sel.row : 0;
  68. let next = _.find(resultArr, function (d) {
  69. return d.index > curRow;
  70. });
  71. if (!next) next = resultArr[0];
  72. if (next.index !== curRow) {
  73. sheet.setSelection(next.index, sel ? sel.col : 0, 1, 1);
  74. sheet.getParent().focus();
  75. sheet.showRow(next.index, spreadNS.VerticalPosition.center);
  76. }
  77. }
  78. };
  79. const locatePre = function () {
  80. if (resultArr.length > 0) {
  81. const sel = sheet.getSelections()[0];
  82. const curRow = sel ? sel.row : 0;
  83. let next = _.findLast(resultArr, function (d) {
  84. return d.index < curRow;
  85. });
  86. if (!next) next = resultArr[resultArr.length - 1];
  87. if (next.index !== curRow) {
  88. sheet.setSelection(next.index, sel ? sel.col : 0, 1, 1);
  89. sheet.getParent().focus();
  90. sheet.showRow(next.index, spreadNS.VerticalPosition.center);
  91. }
  92. }
  93. };
  94. return {search, searchAndLocate, locateNext, locatePre};
  95. })();
  96. // $('#pos-keyword').bind('input propertychange', function () {
  97. // posSearch.search(this.value);
  98. // });
  99. $('#pos-keyword').bind('keydown', function(e){
  100. if (e.keyCode == 13) searchObj.searchAndLocate(this.value);
  101. });
  102. $('#search-pre-pos').click(function () {
  103. searchObj.locatePre();
  104. });
  105. $('#search-next-pos').click(function () {
  106. searchObj.locateNext();
  107. });
  108. return searchObj;
  109. };
  110. $.billsSearch = function (setting) {
  111. if (!setting.selector || !setting.searchSpread || !setting.resultSpreadSetting) return;
  112. if (!setting.searchRangeStr) setting.searchRangeStr = '项目节编号/清单编号/名称';
  113. const resultId = setting.id + '-search-result';
  114. const obj = $(setting.selector);
  115. obj.html(
  116. ' <div class="sjs-bar">\n' +
  117. ' <div class="input-group input-group-sm pb-1">\n' +
  118. ' <div class="input-group-prepend">\n' +
  119. (setting.searchOver ? ' <div class="input-group-text"><input type="radio" name="searchType" id="over"> 超计</div>\n' : '') +
  120. (setting.searchEmpty ? ' <div class="input-group-text"><input type="radio" name="searchType" id="empty"> 漏计</div>\n' : '') +
  121. ' </div>' +
  122. ' <input id="searchKeyword" type="text" class="form-control" placeholder="可查找 ' + setting.searchRangeStr + '" aria-label="Recipient\'s username" aria-describedby="button-addon2">\n' +
  123. ' <div class="input-group-append">\n' +
  124. ' <button class="btn btn-outline-secondary" type="button"">搜索</button>\n' +
  125. ' </div>\n' +
  126. ' </div>\n' +
  127. ' </div>\n' +
  128. ' <div id="' + resultId + '" class="sjs-sh">\n' +
  129. ' </div>'
  130. );
  131. autoFlashHeight();
  132. const resultSpread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  133. SpreadJsObj.initSheet(resultSpread.getActiveSheet(), setting.resultSpreadSetting);
  134. SpreadJsObj.forbiddenSpreadContextMenu('#' + resultId, resultSpread);
  135. const searchSheet = setting.searchSpread.getActiveSheet();
  136. let searchResult = [];
  137. const searchBills = function () {
  138. const keyword = $('#searchKeyword', obj).val();
  139. searchResult = [];
  140. const sortData = SpreadJsObj.getSortData(searchSheet);
  141. for (const node of sortData) {
  142. if ((node.code && node.code.indexOf(keyword) > -1) ||
  143. node.b_code && node.b_code.indexOf(keyword) > -1 ||
  144. node.name && node.name.indexOf(keyword) > -1) {
  145. const data = JSON.parse(JSON.stringify(node));
  146. data.visible = true;
  147. searchResult.push(data);
  148. }
  149. }
  150. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  151. };
  152. const calculateCompletePercent = function (searchResult) {
  153. if (!searchResult) return;
  154. for (const sr of searchResult) {
  155. const base = ZhCalc.add(sr.total_price, sr.end_qc_tp);
  156. sr.complete_percent = base !== 0 ? ZhCalc.mul(ZhCalc.div(sr.end_gather_tp, base), 100, 2) : 0;
  157. }
  158. };
  159. const searchOver = function () {
  160. searchResult = [];
  161. const sortData = SpreadJsObj.getSortData(searchSheet);
  162. for (const node of sortData) {
  163. if (node.children && node.children.length > 0) continue;
  164. if (setting.checkOver && setting.checkOver(node)) {
  165. const data = JSON.parse(JSON.stringify(node));
  166. data.visible = true;
  167. searchResult.push(data);
  168. }
  169. }
  170. calculateCompletePercent(searchResult);
  171. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  172. };
  173. const searchEmpty = function () {
  174. searchResult = [];
  175. const sortData = SpreadJsObj.getSortData(searchSheet);
  176. for (const node of sortData) {
  177. if (node.children && node.children.length > 0) continue;
  178. if (setting.checkEmpty && setting.checkEmpty(node)) {
  179. const data = JSON.parse(JSON.stringify(node));
  180. data.visible = true;
  181. searchResult.push(data);
  182. }
  183. }
  184. calculateCompletePercent(searchResult);
  185. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  186. };
  187. const searchLess = function () {
  188. searchResult = [];
  189. const sortData = SpreadJsObj.getSortData(searchSheet);
  190. for (const node of sortData) {
  191. if (node.children && node.children.length > 0) continue;
  192. if (setting.checkLess && setting.checkLess(node)) {
  193. const data = JSON.parse(JSON.stringify(node));
  194. data.visible = true;
  195. searchResult.push(data);
  196. }
  197. }
  198. calculateCompletePercent(searchResult);
  199. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  200. };
  201. $('input', this.obj).bind('keydown', function (e) {
  202. if (e.keyCode == 13) searchBills();
  203. });
  204. $('button', this.obj).bind('click', () => {searchBills()});
  205. $('#over', this.obj).bind('change', () => {searchOver()});
  206. $('#empty', this.obj).bind('change', () => {searchLess()});
  207. resultSpread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  208. const sheet = info.sheet;
  209. const data = sheet.zh_data;
  210. if (!data) { return }
  211. const curBills = data[info.row];
  212. if (!curBills) { return }
  213. SpreadJsObj.locateTreeNode(searchSheet, curBills.ledger_id, true);
  214. if (setting.afterLocated) {
  215. setting.afterLocated();
  216. }
  217. });
  218. return {spread: resultSpread};
  219. };
  220. })(jQuery);