common_spread.js 7.3 KB

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