common_spread.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * 工料机SpreadJS类
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/7/18
  6. * @version
  7. */
  8. /**
  9. * 构造函数
  10. *
  11. * @param {Object} header
  12. * @return {void}
  13. */
  14. function CommonSpreadJs (header) {
  15. this.header = header;
  16. this.spread = null;
  17. this.sheet = null;
  18. this.columnInfo = [];
  19. this.isChanging = false;
  20. }
  21. /**
  22. * 初始化
  23. *
  24. * @param {String} target
  25. * @return {Object}
  26. */
  27. CommonSpreadJs.prototype.init = function(target) {
  28. let setting = {
  29. header: []
  30. };
  31. this.columnInfo = [];
  32. for(let tmp of this.header) {
  33. setting.header.push({headerName: tmp.name, headerWidth: 120});
  34. this.columnInfo.push({name: tmp.field, displayName: tmp.name, visible: tmp.visible, cellType: tmp.cellType, size: 120});
  35. }
  36. this.spread = sheetCommonObj.buildSheet(document.getElementById(target), setting, 10);
  37. this.spread.options.scrollbarShowMax = true;
  38. this.spread.options.scrollbarMaxAlign = true;
  39. this.spread.options.showHorizontalScrollbar = true;
  40. this.sheet = this.spread.getActiveSheet();
  41. this.sheet.autoGenerateColumns = false;
  42. // 设置表单不可编辑
  43. this.sheet.options.isProtected = true;
  44. return this.spread;
  45. };
  46. /**
  47. * 获取对应字段的列号
  48. *
  49. * @param {String} field
  50. * @return {Number}
  51. */
  52. CommonSpreadJs.prototype.getFieldColumn = function(field) {
  53. let result = -1;
  54. if (this.header.length <= 0) {
  55. return result;
  56. }
  57. for (let tmp in this.header) {
  58. if (this.header[tmp].field === field) {
  59. result = tmp;
  60. break;
  61. }
  62. }
  63. return result;
  64. };
  65. /**
  66. * 根据列号获取对应的字段
  67. *
  68. * @param {Number} column
  69. * @return {String}
  70. */
  71. CommonSpreadJs.prototype.getColumnField = function(column) {
  72. let field = this.header[column] !== undefined && this.header[column].field !== undefined ?
  73. this.header[column].field : '';
  74. return field;
  75. };
  76. /**
  77. * 设置指定列可编辑
  78. *
  79. * @param {Number} column
  80. * @return {void}
  81. */
  82. CommonSpreadJs.prototype.setColumnEditable = function(column) {
  83. this.sheet.getRange(-1, column, -1, 1).locked(false);
  84. };
  85. /**
  86. * 设置指定列可编辑
  87. *
  88. *
  89. * @param {Number} column
  90. * @return {void}
  91. */
  92. CommonSpreadJs.prototype.setColumnLock = function(row, column) {
  93. this.sheet.getRange(-1, column, -1, 1).locked(true);
  94. };
  95. /**
  96. * 设置样式
  97. *
  98. * @param {Number} row
  99. * @param {Number} column
  100. * @param {Object} styleSetting
  101. */
  102. CommonSpreadJs.prototype.setStyle = function(row, column, styleSetting) {
  103. let style = new GC.Spread.Sheets.Style();
  104. style.locked = styleSetting.readOnly === undefined ? true : styleSetting.readOnly;
  105. style.hAlign = styleSetting.hAlign === undefined ? GC.Spread.Sheets.HorizontalAlign.center : styleSetting.hAlign;
  106. this.sheet.setStyle(row, column, style, GC.Spread.Sheets.SheetArea.viewport);
  107. };
  108. /**
  109. * 获取sheet对象
  110. *
  111. * @return {Object}
  112. */
  113. CommonSpreadJs.prototype.getSheet = function() {
  114. return this.sheet;
  115. };
  116. /**
  117. * 设置数据
  118. *
  119. * @param {Object} data
  120. * @return {void}
  121. */
  122. CommonSpreadJs.prototype.setData = function(data) {
  123. this.sheet.setDataSource(data);
  124. this.sheet.bindColumns(this.columnInfo);
  125. this.spread.refresh();
  126. };
  127. /**
  128. * 绑定事件
  129. *
  130. * @param {Number} event
  131. * @param {function} callback
  132. * @return {void}
  133. */
  134. CommonSpreadJs.prototype.bind = function (event, callback) {
  135. this.sheet.bind(event, callback);
  136. };
  137. /**
  138. * 查找指定数据并返回行号
  139. *
  140. * @param {String} keyword
  141. * @param {Number} rowStart
  142. * @param {Number} columnStart
  143. * @return {Number}
  144. */
  145. CommonSpreadJs.prototype.searchKeyword = function(keyword, rowStart = 0, columnStart = 0) {
  146. let condition = new GC.Spread.Sheets.Search.SearchCondition();
  147. condition.searchString = keyword;
  148. condition.startSheetIndex = 0;
  149. condition.endSheetIndex = 0;
  150. condition.searchFlags = GC.Spread.Sheets.Search.SearchFlags.ignoreCase | GC.Spread.Sheets.Search.SearchFlags.blockRange;
  151. condition.searchOrder = GC.Spread.Sheets.Search.SearchOrder.nOrder;
  152. condition.searchTarget = GC.Spread.Sheets.Search.SearchFoundFlags.cellText;
  153. condition.sheetArea = GC.Spread.Sheets.SheetArea.viewport;
  154. condition.rowStart = rowStart;
  155. condition.columnStart = columnStart;
  156. let result = this.spread.search(condition);
  157. return result.foundRowIndex;
  158. };
  159. /**
  160. * 过滤数据
  161. *
  162. * @param {String} field
  163. * @param {Array} filterList
  164. * @return {void}
  165. */
  166. CommonSpreadJs.prototype.filterData = function(field, filterList) {
  167. let fieldColumn = this.getFieldColumn(field);
  168. fieldColumn = parseInt(fieldColumn);
  169. let filter = new GC.Spread.Sheets.Filter.HideRowFilter(new GC.Spread.Sheets.Range(-1, 5, -1, 1));
  170. this.sheet.rowFilter(filter);
  171. let rowFilter = this.sheet.rowFilter();
  172. rowFilter.filterButtonVisible(false);
  173. rowFilter.removeFilterItems(fieldColumn);
  174. for (let tmp of filterList) {
  175. let condition = new GC.Spread.Sheets.ConditionalFormatting.Condition(GC.Spread.Sheets.ConditionalFormatting.ConditionType.numberCondition, {
  176. compareType: GC.Spread.Sheets.ConditionalFormatting.GeneralComparisonOperators.equalsTo,
  177. expected: tmp
  178. });
  179. rowFilter.addFilterItem(fieldColumn, condition);
  180. }
  181. rowFilter.filter(fieldColumn);
  182. this.sheet.invalidateLayout();
  183. this.sheet.repaint();
  184. };
  185. /**
  186. * 选中指定行
  187. *
  188. * @param {Number} row
  189. * @return {void}
  190. */
  191. CommonSpreadJs.prototype.selectRow = function(row) {
  192. this.sheet.setSelection(row, 0, 1, 1);
  193. };
  194. /**
  195. * 验证数据
  196. *
  197. * @param {Number} column
  198. * @param {String} value
  199. * @return {boolean}
  200. */
  201. CommonSpreadJs.prototype.checkData = function(column, value) {
  202. let result = true;
  203. let validator = this.header[column].validator !== undefined ? this.header[column].validator : null;
  204. if (validator === null) {
  205. return result;
  206. }
  207. switch (validator) {
  208. case 'number':
  209. let regular = /^\d+(\.\d+)?$/;
  210. result = regular.test(value);
  211. break;
  212. case 'boolean':
  213. let booleanValue = [true, false];
  214. result = booleanValue.indexOf(value) >= 0;
  215. break;
  216. }
  217. return result;
  218. };
  219. /**
  220. * 设置对应字段数值
  221. *
  222. * @param {String} field
  223. * @param {Number} value
  224. * @param {boolean} appendMode
  225. * @return {void}
  226. */
  227. CommonSpreadJs.prototype.setCellByField = function(field, value, appendMode) {
  228. let row = this.sheet.getActiveRowIndex();
  229. let columnIndex = this.getFieldColumn(field);
  230. if (appendMode) {
  231. let oldValue = this.sheet.getValue(row, columnIndex);
  232. value = (oldValue + value).toDecimal(2);
  233. }
  234. this.sheet.setValue(row, columnIndex, value);
  235. };
  236. /**
  237. * 获取当前选中行指定字段的值
  238. *
  239. * @param {String} field
  240. * @return {Mixed}
  241. */
  242. CommonSpreadJs.prototype.getActiveDataByField = function(field) {
  243. let result = null;
  244. let activeGLJRow = this.sheet.getActiveRowIndex();
  245. let column = this.getFieldColumn(field);
  246. if (column < 0) {
  247. return result;
  248. }
  249. result = this.sheet.getValue(activeGLJRow, column);
  250. return result;
  251. };