ledger_search.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. const resultId = setting.id + '-search-result';
  110. const obj = $(setting.selector);
  111. obj.html(
  112. ' <div class="sjs-bar-1">\n' +
  113. ' <div class="input-group input-group-sm pb-1">\n' +
  114. ' <input id="searchKeyword" type="text" class="form-control" placeholder="可查找 项目节编号/清单编号/名称" aria-label="Recipient\'s username" aria-describedby="button-addon2">\n' +
  115. ' <div class="input-group-append">\n' +
  116. ' <button class="btn btn-outline-secondary" type="button"">搜索</button>\n' +
  117. ' </div>\n' +
  118. ' </div>\n' +
  119. ' </div>\n' +
  120. ' <div id="' + resultId + '" class="sjs-sh-1">\n' +
  121. ' </div>'
  122. );
  123. autoFlashHeight();
  124. const resultSpread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  125. SpreadJsObj.initSheet(resultSpread.getActiveSheet(), setting.resultSpreadSetting);
  126. SpreadJsObj.forbiddenSpreadContextMenu('#' + resultId, resultSpread);
  127. const searchSheet = setting.searchSpread.getActiveSheet();
  128. let searchResult = [];
  129. const searchBills = function () {
  130. const keyword = $('input', obj).val();
  131. searchResult = [];
  132. const sortData = SpreadJsObj.getSortData(searchSheet);
  133. for (const node of sortData) {
  134. if ((node.code && node.code.indexOf(keyword) > -1) ||
  135. node.b_code && node.b_code.indexOf(keyword) > -1 ||
  136. node.name && node.name.indexOf(keyword) > -1) {
  137. const data = JSON.parse(JSON.stringify(node));
  138. data.visible = true;
  139. searchResult.push(data);
  140. }
  141. }
  142. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  143. };
  144. $('input', this.obj).bind('keydown', function (e) {
  145. if (e.keyCode == 13) searchBills();
  146. });
  147. $('button', this.obj).bind('click', () => {searchBills()});
  148. resultSpread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  149. const sheet = info.sheet;
  150. const data = sheet.zh_data;
  151. if (!data) { return }
  152. const curBills = data[info.row];
  153. if (!curBills) { return }
  154. SpreadJsObj.locateTreeNode(searchSheet, curBills.ledger_id, true);
  155. if (setting.afterLocated) {
  156. setting.afterLocated();
  157. }
  158. });
  159. return {spread: resultSpread};
  160. }
  161. })(jQuery);