_zSpread.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * Created by Syusuke on 2017/3/17.
  3. *
  4. */
  5. (function($){
  6. var _spSetting = {
  7. spType:"",
  8. header:[],
  9. view:{
  10. comboBox:[],
  11. lockedCells:[]
  12. },
  13. private:{
  14. spType:"",
  15. colCount:0,
  16. sectionSelected:null,
  17. cellPrimValue:null,
  18. cellCurrValue:null
  19. }
  20. }
  21. _tools = {
  22. isArray:function(arr){
  23. return Object.prototype.toString.apply(arr) === "[object Array]"
  24. },
  25. clone:function(obj){ //���colon���� �������ֱ����ȸ��ƣ����󲻿���
  26. if(obj===null) return null;
  27. var o = _tools.isArray(obj)?[]:{};
  28. for(var i in obj){
  29. o[i] = (obj[i] instanceof Date) ? new Date(obj[i].getTime()): (typeof obj[i] === "object"? _tools.clone(obj[i]): obj[i]);
  30. }
  31. return o;
  32. }
  33. }
  34. var _initSpread = function(spID,spSetting){
  35. spSetting.private.colCount = spSetting.header.length;
  36. spSetting.private.spType = spSetting.spType;
  37. var spread = new GC.Spread.Sheets.Workbook(spID[0],{sheetCount:1});
  38. var ActiveSheet = spread.getActiveSheet();
  39. ActiveSheet.setColumnCount(spSetting.header.length, GC.Spread.Sheets.SheetArea.viewport);
  40. ActiveSheet.options.isProtected = true;
  41. ActiveSheet.getRange(-1, 0, -1 ,100, GC.Spread.Sheets.SheetArea.viewport).locked(false);
  42. spread.options.tabStripVisible = false;
  43. spread.options.scrollbarMaxAlign = false;
  44. for(var i=0;i<spSetting.header.length;i++){
  45. ActiveSheet.setValue(0, i, spSetting.header[i].headerName, GC.Spread.Sheets.SheetArea.colHeader);
  46. ActiveSheet.setColumnWidth(i,spSetting.header[i].headerWidth, GC.Spread.Sheets.SheetArea.colHeader);
  47. }
  48. for(var i=0;i<spSetting.view.comboBox.length;i++){
  49. var c = spSetting.view.comboBox[i]
  50. var cellType = new GC.Spread.Sheets.CellTypes.ComboBox();
  51. cellType.items(c.items);
  52. ActiveSheet.getRange(c.row,c.col,c.rowCount,c.colCount, GC.Spread.Sheets.SheetArea.viewport).cellType(cellType);
  53. }
  54. ActiveSheet.getRange(-1, 1, -1 , 100, GC.Spread.Sheets.SheetArea.viewport).locked(true);
  55. ActiveSheet.bind(GC.Spread.Sheets.Events.EditStarting,function(sender,args){
  56. spSetting.private.cellCurrValue = null;
  57. spSetting.private.cellPrimValue = ActiveSheet.getValue(args.row,args.col);
  58. })
  59. ActiveSheet.bind(GC.Spread.Sheets.Events.EditEnding,function(sender,args){
  60. spSetting.private.cellCurrValue = args.editingText;
  61. if(args.col == 0){
  62. var a = spSetting.view.lockedCells;
  63. var b = [];
  64. ActiveSheet.getRange(args.row, 1, 1 , 100, GC.Spread.Sheets.SheetArea.viewport).locked(args.editingText == null);
  65. a.forEach(function(x){
  66. b.push(x.col);
  67. })
  68. b.forEach(function(x){
  69. ActiveSheet.getCell(args.row,x).locked(true);
  70. });
  71. if(args.editingText == null){
  72. removeAction(spSetting)
  73. for(var i=0;i<spSetting.private.colCount;i++){
  74. ActiveSheet.setValue(args.row,i,null);
  75. }
  76. }else{
  77. insertAction(spSetting,function(status){
  78. if(status == 204)
  79. ActiveSheet.setValue(args.row,0,null)
  80. spSetting.private.cellCurrValue = null;
  81. spSetting.private.cellPrimValue = null;
  82. });
  83. }
  84. }else
  85. // var code = ActiveSheet.getValue(args.row,0);
  86. {
  87. var data = [];
  88. for(var i=0;i<spSetting.header.length;i++){
  89. data.push(ActiveSheet.getValue(args.row,i))
  90. }
  91. data[args.col] = args.editingText
  92. updateAction(spSetting,data);
  93. }
  94. })
  95. return spread;
  96. }
  97. function insertAction(spSetting,callback){
  98. var value =JSON.stringify(spSetting.private);
  99. $.ajax({
  100. type:"POST",
  101. url:"http://localhost:6060/rationLibEditor/insert" + spSetting.private.spType,
  102. data:{"rationLibName":params.realLibName,"value":value},
  103. dataType:"json",
  104. cache:false,
  105. timeout:50000,
  106. success:function(result,textStatus,status){
  107. if(status.status == 204){
  108. alert("编号为"+spSetting.private.cellCurrValue+"已存在,不可重复添加!")
  109. callback(status.status)
  110. }
  111. spSetting.private.cellCurrValue = null;
  112. spSetting.private.cellPrimValue = null;
  113. },
  114. error:function(err){
  115. alert(err.statusText);
  116. spSetting.private.cellCurrValue = null;
  117. spSetting.private.cellPrimValue = null;
  118. }
  119. })
  120. }
  121. function updateAction(spSetting,data){
  122. var value = JSON.stringify(spSetting.private);
  123. var datas = JSON.stringify(data);
  124. $.ajax({
  125. type:"POST",
  126. url:"http://localhost:6060/rationLibEditor/update" + spSetting.private.spType,
  127. data:{"rationLibName":params.realLibName,"value":value,"data":datas},
  128. dataType:"json",
  129. cache:false,
  130. timeout:50000,
  131. success:function(result,textStatus,status){
  132. spSetting.private.cellCurrValue = null;
  133. spSetting.private.cellPrimValue = null;
  134. },
  135. error:function(err){
  136. alert(err.responseText.error)
  137. spSetting.private.cellCurrValue = null;
  138. spSetting.private.cellPrimValue = null;
  139. }
  140. })
  141. }
  142. function removeAction(spSetting){
  143. var value = JSON.stringify(spSetting.private);
  144. $.ajax({
  145. type:"POST",
  146. url:"http://localhost:6060/rationLibEditor/remove" + spSetting.private.spType,
  147. data:{"rationLibName":params.realLibName,"value":value},
  148. dataType:"json",
  149. cache:false,
  150. timeout:50000,
  151. success:function(result){
  152. spSetting.private.cellCurrValue = null;
  153. spSetting.private.cellPrimValue = null;
  154. },
  155. error:function(){
  156. }
  157. })
  158. }
  159. $.fn.Spread = {
  160. init:function(spID,zSetting){
  161. var spSetting = _tools.clone(_spSetting);
  162. $.extend(true,spSetting,zSetting);
  163. return _initSpread(spID,spSetting);
  164. }
  165. }
  166. }(jQuery))