123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- /**
- * Created by Syusuke on 2017/3/17.
- *
- */
- (function($){
- var _spSetting = {
- spType:"",
- header:[],
- view:{
- comboBox:[],
- lockedCells:[]
- },
- private:{
- spType:"",
- colCount:0,
- sectionSelected:null,
- cellPrimValue:null,
- cellCurrValue:null
- }
- }
- _tools = {
- isArray:function(arr){
- return Object.prototype.toString.apply(arr) === "[object Array]"
- },
- clone:function(obj){
- if(obj===null) return null;
- var o = _tools.isArray(obj)?[]:{};
- for(var i in obj){
- o[i] = (obj[i] instanceof Date) ? new Date(obj[i].getTime()): (typeof obj[i] === "object"? _tools.clone(obj[i]): obj[i]);
- }
- return o;
- }
- }
- var _initSpread = function(spID,spSetting){
- spSetting.private.colCount = spSetting.header.length;
- spSetting.private.spType = spSetting.spType;
- var spread = new GC.Spread.Sheets.Workbook(spID[0],{sheetCount:1});
- var ActiveSheet = spread.getActiveSheet();
- ActiveSheet.setColumnCount(spSetting.header.length, GC.Spread.Sheets.SheetArea.viewport);
- ActiveSheet.options.isProtected = true;
- ActiveSheet.getRange(-1, 0, -1 ,100, GC.Spread.Sheets.SheetArea.viewport).locked(false);
- spread.options.tabStripVisible = false;
- spread.options.scrollbarMaxAlign = false;
- for(var i=0;i<spSetting.header.length;i++){
- ActiveSheet.setValue(0, i, spSetting.header[i].headerName, GC.Spread.Sheets.SheetArea.colHeader);
- ActiveSheet.setColumnWidth(i,spSetting.header[i].headerWidth, GC.Spread.Sheets.SheetArea.colHeader);
- }
- for(var i=0;i<spSetting.view.comboBox.length;i++){
- var c = spSetting.view.comboBox[i]
- var cellType = new GC.Spread.Sheets.CellTypes.ComboBox();
- cellType.items(c.items);
- ActiveSheet.getRange(c.row,c.col,c.rowCount,c.colCount, GC.Spread.Sheets.SheetArea.viewport).cellType(cellType);
- }
- ActiveSheet.getRange(-1, 1, -1 , 100, GC.Spread.Sheets.SheetArea.viewport).locked(true);
- ActiveSheet.bind(GC.Spread.Sheets.Events.EditStarting,function(sender,args){
- spSetting.private.cellCurrValue = null;
- spSetting.private.cellPrimValue = ActiveSheet.getValue(args.row,args.col);
- })
- ActiveSheet.bind(GC.Spread.Sheets.Events.EditEnding,function(sender,args){
- spSetting.private.cellCurrValue = args.editingText;
- if(args.col == 0){
- var a = spSetting.view.lockedCells;
- var b = [];
- ActiveSheet.getRange(args.row, 1, 1 , 100, GC.Spread.Sheets.SheetArea.viewport).locked(args.editingText == null);
- a.forEach(function(x){
- b.push(x.col);
- })
- b.forEach(function(x){
- ActiveSheet.getCell(args.row,x).locked(true);
- });
- if(args.editingText == null){
- removeAction(spSetting)
- for(var i=0;i<spSetting.private.colCount;i++){
- ActiveSheet.setValue(args.row,i,null);
- }
- }else{
- insertAction(spSetting,function(status){
- if(status == 204)
- ActiveSheet.setValue(args.row,0,null)
- spSetting.private.cellCurrValue = null;
- spSetting.private.cellPrimValue = null;
- });
- }
- }else
- // var code = ActiveSheet.getValue(args.row,0);
- {
- var data = [];
- for(var i=0;i<spSetting.header.length;i++){
- data.push(ActiveSheet.getValue(args.row,i))
- }
- data[args.col] = args.editingText
- updateAction(spSetting,data);
- }
- })
- return spread;
- }
- function insertAction(spSetting,callback){
- var value =JSON.stringify(spSetting.private);
- $.ajax({
- type:"POST",
- url:"http://localhost:6060/rationLibEditor/insert" + spSetting.private.spType,
- data:{"rationLibName":params.realLibName,"value":value},
- dataType:"json",
- cache:false,
- timeout:50000,
- success:function(result,textStatus,status){
- if(status.status == 204){
- alert("编号为"+spSetting.private.cellCurrValue+"已存在,不可重复添加!")
- callback(status.status)
- }
- spSetting.private.cellCurrValue = null;
- spSetting.private.cellPrimValue = null;
- },
- error:function(err){
- alert(err.statusText);
- spSetting.private.cellCurrValue = null;
- spSetting.private.cellPrimValue = null;
- }
- })
- }
- function updateAction(spSetting,data){
- var value = JSON.stringify(spSetting.private);
- var datas = JSON.stringify(data);
- $.ajax({
- type:"POST",
- url:"http://localhost:6060/rationLibEditor/update" + spSetting.private.spType,
- data:{"rationLibName":params.realLibName,"value":value,"data":datas},
- dataType:"json",
- cache:false,
- timeout:50000,
- success:function(result,textStatus,status){
- spSetting.private.cellCurrValue = null;
- spSetting.private.cellPrimValue = null;
- },
- error:function(err){
- alert(err.responseText.error)
- spSetting.private.cellCurrValue = null;
- spSetting.private.cellPrimValue = null;
- }
- })
- }
- function removeAction(spSetting){
- var value = JSON.stringify(spSetting.private);
- $.ajax({
- type:"POST",
- url:"http://localhost:6060/rationLibEditor/remove" + spSetting.private.spType,
- data:{"rationLibName":params.realLibName,"value":value},
- dataType:"json",
- cache:false,
- timeout:50000,
- success:function(result){
- spSetting.private.cellCurrValue = null;
- spSetting.private.cellPrimValue = null;
- },
- error:function(){
- }
- })
- }
- $.fn.Spread = {
- init:function(spID,zSetting){
- var spSetting = _tools.clone(_spSetting);
- $.extend(true,spSetting,zSetting);
- return _initSpread(spID,spSetting);
- }
- }
- }(jQuery))
|