ledger_search.js 8.1 KB

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