ledger_search.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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="mt-1 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.showRow(pos.index, spreadNS.VerticalPosition.center);
  57. }
  58. }
  59. };
  60. const locateNext = function () {
  61. if (resultArr.length > 0) {
  62. const sel = sheet.getSelections()[0];
  63. const curRow = sel ? sel.row : 0;
  64. let next = _.find(resultArr, function (d) {
  65. return d.index > curRow;
  66. });
  67. if (!next) next = resultArr[0];
  68. if (next.index !== curRow) {
  69. sheet.setSelection(next.index, sel ? sel.col : 0, 1, 1);
  70. sheet.showRow(next.index, spreadNS.VerticalPosition.center);
  71. }
  72. }
  73. };
  74. const locatePre = function () {
  75. if (resultArr.length > 0) {
  76. const sel = sheet.getSelections()[0];
  77. const curRow = sel ? sel.row : 0;
  78. let next = _.findLast(resultArr, function (d) {
  79. return d.index < curRow;
  80. });
  81. if (!next) next = resultArr[resultArr.length - 1];
  82. if (next.index !== curRow) {
  83. sheet.setSelection(next.index, sel ? sel.col : 0, 1, 1);
  84. sheet.showRow(next.index, spreadNS.VerticalPosition.center);
  85. }
  86. }
  87. };
  88. return {search, searchAndLocate, locateNext, locatePre};
  89. })();
  90. // $('#pos-keyword').bind('input propertychange', function () {
  91. // posSearch.search(this.value);
  92. // });
  93. $('#pos-keyword').bind('keydown', function(e){
  94. if (e.keyCode == 13) searchObj.searchAndLocate(this.value);
  95. });
  96. $('#search-pre-pos').click(function () {
  97. searchObj.locatePre();
  98. });
  99. $('#search-next-pos').click(function () {
  100. searchObj.locateNext();
  101. });
  102. return searchObj;
  103. }
  104. $.billsSearch = function (setting) {
  105. if (!setting.selector || !setting.searchSpread || !setting.resultSpreadSetting) return;
  106. const resultId = setting.id + '-search-result';
  107. const obj = $(setting.selector);
  108. obj.html(
  109. ' <div class="sjs-bar-1">\n' +
  110. ' <div class="input-group input-group-sm">\n' +
  111. ' <input id="searchKeyword" type="text" class="form-control" placeholder="可查找 项目节编号/清单编号/名称" aria-label="Recipient\'s username" aria-describedby="button-addon2">\n' +
  112. ' <div class="input-group-append">\n' +
  113. ' <button class="btn btn-outline-secondary" type="button"">搜索</button>\n' +
  114. ' </div>\n' +
  115. ' </div>\n' +
  116. ' </div>\n' +
  117. ' <div id="' + resultId + '" class="sjs-sh-1">\n' +
  118. ' </div>'
  119. );
  120. autoFlashHeight();
  121. const resultSpread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  122. SpreadJsObj.initSheet(resultSpread.getActiveSheet(), setting.resultSpreadSetting);
  123. SpreadJsObj.forbiddenSpreadContextMenu('#' + resultId, resultSpread);
  124. const searchSheet = setting.searchSpread.getActiveSheet();
  125. let searchResult = [];
  126. const searchBills = function () {
  127. const keyword = $('input', obj).val();
  128. searchResult = [];
  129. const sortData = SpreadJsObj.getSortData(searchSheet);
  130. for (const node of sortData) {
  131. if ((node.code && node.code.indexOf(keyword) > -1) ||
  132. node.b_code && node.b_code.indexOf(keyword) > -1 ||
  133. node.name && node.name.indexOf(keyword) > -1) {
  134. const data = JSON.parse(JSON.stringify(node));
  135. data.visible = true;
  136. searchResult.push(data);
  137. }
  138. }
  139. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  140. };
  141. $('input', this.obj).bind('keydown', function (e) {
  142. if (e.keyCode == 13) searchBills();
  143. });
  144. $('button', this.obj).bind('click', () => {searchBills()});
  145. resultSpread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  146. const sheet = info.sheet;
  147. const data = sheet.zh_data;
  148. if (!data) { return }
  149. const curBills = data[info.row];
  150. if (!curBills) { return }
  151. SpreadJsObj.locateTreeNode(searchSheet, curBills.ledger_id, true);
  152. if (setting.afterLocated) {
  153. setting.afterLocated();
  154. }
  155. });
  156. return {spread: resultSpread};
  157. }
  158. })(jQuery);