common_spread.js 7.1 KB

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