common_spread.js 7.3 KB

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