glj_index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * 聊天室相关
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/6/15
  6. * @version
  7. */
  8. $(document).ready(function () {
  9. // excel
  10. let header = [
  11. {name: '编码', field: 'code', visible: true},
  12. {name: '名称', field: 'name', visible: true},
  13. {name: '规格型号', field: 'specs', visible: true},
  14. {name: '单位', field: 'unit', visible: true},
  15. {name: 'ID', field: 'id', visible: false},
  16. {name: '类型', field: 'type', visible: false},
  17. {name: '人工工种', field: 'type_of_work', visible: false},
  18. {name: '总消耗量', field: '', visible: true},
  19. {name: '基价单价', field: "unit_price.base_price", visible: true},
  20. {name: '调整基价', field: 'adjust_price', visible: true},
  21. {name: '市场单价', field: "unit_price.market_price", visible: true},
  22. {name: '是否暂估', field: 'is_evaluate', visible: true, cellType: new GC.Spread.Sheets.CellTypes.CheckBox()},
  23. {name: '供货方式', field: 'supply', visible: true},
  24. {name: '甲供数量', field: 'supply_quantity', visible: true},
  25. {name: '交货方式', field: 'delivery', visible: true},
  26. {name: '送达地点', field: 'delivery_address', visible: true},
  27. {name: '不调价', field: 'is_adjust_price', visible: true, cellType: new GC.Spread.Sheets.CellTypes.CheckBox()},
  28. ];
  29. let setting = {
  30. header: []
  31. };
  32. for(let tmp of header) {
  33. setting.header.push({headerName: tmp.name, headerWidth: 120});
  34. }
  35. let spread = sheetCommonObj.buildSheet(document.getElementById("test"), setting, 3);
  36. spread.options.scrollbarShowMax = true;
  37. spread.options.scrollbarMaxAlign = true;
  38. spread.options.showHorizontalScrollbar = true;
  39. let sheet = spread.getActiveSheet();
  40. // 设置表单不可编辑
  41. sheet.options.isProtected = true;
  42. // 居中样式
  43. let centerStyleSetting = {hAlign: 1};
  44. sheet.setStyle(-1, 11, getStyle(centerStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
  45. sheet.setStyle(-1, 16, getStyle(centerStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
  46. sheet.setStyle(-1, 3, getStyle(centerStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
  47. // 向右对齐样式
  48. let rightStyleSetting = {hAlign: GC.Spread.Sheets.HorizontalAlign.right};
  49. sheet.setStyle(-1, 7, getStyle(rightStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
  50. sheet.setStyle(-1, 8, getStyle(rightStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
  51. sheet.setStyle(-1, 9, getStyle(rightStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
  52. sheet.setStyle(-1, 10, getStyle(rightStyleSetting), GC.Spread.Sheets.SheetArea.viewport);
  53. // 设置可编辑列
  54. sheet.getRange(-1, 10, -1, 1).locked(false);
  55. sheet.getRange(-1, 11, -1, 1).locked(false);
  56. sheet.getRange(-1, 16, -1, 1).locked(false);
  57. // 设置json数据
  58. let sourceData = JSON.parse(jsonData);
  59. let rowCounter = 0;
  60. for(let data of sourceData) {
  61. let columnCounter = 0;
  62. for(let columnInfo of header) {
  63. // 设置是否显示
  64. sheet.setColumnVisible(columnCounter, columnInfo.visible);
  65. let dataString = columnInfo.field !== '' && columnInfo.field !== undefined ?
  66. "data." + columnInfo.field : '';
  67. let cellData = eval(dataString);
  68. // 设置复选框
  69. if (columnInfo.cellType !== undefined) {
  70. let checkbox = new GC.Spread.Sheets.CellTypes.CheckBox();
  71. sheet.setCellType(rowCounter, columnCounter, checkbox, GC.Spread.Sheets.SheetArea.viewport);
  72. }
  73. // 如果不为材料“是否暂估列”根据条件显示
  74. if (columnInfo.field === 'is_evaluate' && materialIdList.indexOf(data.type + '') < 0) {
  75. let string = new GC.Spread.Sheets.CellTypes.Text();
  76. sheet.setCellType(rowCounter, columnCounter, string, GC.Spread.Sheets.SheetArea.viewport);
  77. cellData = '';
  78. // 并且锁定该单元格
  79. sheet.getRange(rowCounter, columnCounter, 1, 1).locked(true);
  80. }
  81. sheet.setValue(rowCounter, columnCounter, cellData, GC.Spread.Sheets.SheetArea.viewport);
  82. columnCounter++;
  83. }
  84. rowCounter++;
  85. }
  86. /*
  87. let socket = io('http://notify.smartcost.com.cn:3300');
  88. socket.on('connect', function () {
  89. socket.emit('join', project);
  90. console.log('连接成功');
  91. });
  92. // 接受到改变
  93. let changeInfo = [];
  94. socket.on('dataChange', function(data) {
  95. data = JSON.parse(data);
  96. if (data.newValue === undefined) {
  97. return false;
  98. }
  99. changeInfo.push(data);
  100. $("#message").html('基价单位已被修改,<a href="javascript:void(0);" id="load-data">点击加载</a>');
  101. $("#notify").slideDown('fast');
  102. initiativeChange = false;
  103. });
  104. // 是否主动更改数据
  105. let initiativeChange = false;
  106. $("#message").on('click', '#load-data', function() {
  107. $("#notify").slideUp('fast');
  108. if (changeInfo.length > 0) {
  109. for (let index in changeInfo) {
  110. let cell = sheet.getCell(changeInfo[index].row, changeInfo[index].col, GC.Spread.Sheets.SheetArea.viewport);
  111. cell.value(changeInfo[index].newValue);
  112. }
  113. }
  114. changeInfo = [];
  115. });
  116. // 进入单元格后设置为主动设置数据
  117. sheet.bind(GC.Spread.Sheets.Events.EditEnding, function (element, info) {
  118. initiativeChange = true;
  119. });
  120. // 绑定事件
  121. sheet.bind(GC.Spread.Sheets.Events.CellChanged, function (element, info) {
  122. // 如果修改了数据且为主动修改,先存库再广播给其他页面
  123. if (info.newValue !== undefined && info.oldValue !== undefined && initiativeChange) {
  124. let id = sheet.getCell(info.row, 0).value();
  125. $.ajax({
  126. url: '/save',
  127. type: 'post',
  128. data: {priceValue: info.newValue, id: id},
  129. error: function() {
  130. // @todo 替换为更好地错误提示
  131. alert('error');
  132. },
  133. beforeSend: function() {
  134. },
  135. success: function(response) {
  136. if (response.err === 0) {
  137. socket.emit('dataNotify', JSON.stringify(info));
  138. } else {
  139. // @todo 替换为更好地错误提示
  140. alert(response.msg);
  141. }
  142. }
  143. });
  144. }
  145. });
  146. */
  147. });
  148. function getStyle(setting) {
  149. let style = new GC.Spread.Sheets.Style();
  150. style.locked = setting.readOnly === undefined ? true : setting.readOnly;
  151. style.hAlign = setting.hAlign === undefined ? GC.Spread.Sheets.HorizontalAlign.center : setting.hAlign;
  152. return style;
  153. }