⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dhtmlxdataprocessor.js

📁 dhtmlxGrid是DHTMLX公司的一个非常不错的JAVASCRIPT的网格组件
💻 JS
📖 第 1 页 / 共 2 页
字号:
/*
Copyright Scand LLC http://www.scbr.com
This version of Software is free for using in non-commercial applications.
For commercial use please contact info@scbr.com to obtain license
*/


	/**
	* 	@desc: data processor object
	*	@param: serverProcessorURL - url used for update script
	*	@type: public
	*/
function dataProcessor(serverProcessorURL){
    this.serverProcessor = serverProcessorURL;
	this.obj = null;
	this.mandatoryFields = new Array(0);//array of fields which should be varified (indexes of columns as indexes) with corresponding verificators (as values)
	this.updatedRows = new Array(0);//array of rows which are(were) updated
	this.autoUpdate = true;//if rows should be send to server automaticaly (based on cell edit finished)
	this.updateMode = "cell";
    this._waitMode=0;
    this._tMode="GET";
    return this;
    }

	/**
	* 	@desc: select GET or POST transaction model
	*	@param: mode - GET/POST
	*	@param: total - true/false - send records row by row or all at once (currently available only for grid)
	*	@type: public
	*/
	dataProcessor.prototype.setTransactionMode = function(mode,total){
        this._tMode=mode;
		this._tSend=total;
    }


	/**
	*     @desc: set function called after row updated
	*     @param: func - event handling function (or its name)
	*     @type: public
	*     @topic: 10
	*	  @event: onAfterUpdate
	*     @eventdesc: Event raised after row updated on server side
	*     @eventparam:  ID of clicked row
	*     @eventparam:  type of command
	*     @eventparam:  new Id, for _insert_ command
	*/
	dataProcessor.prototype.setOnAfterUpdate = function(ev){
        if (typeof(ev)!="function") ev=eval(ev);
        this._afterUEvent=ev;
	}


                  
	/**
	* 	@desc: get state of updating
	*	@returns:   true - all in sync with server, false - some items not updated yet.
	*	@type: public;
	*/
	dataProcessor.prototype.getSyncState = function(){
		for(var i=0;i<this.updatedRows.length;i++)
				if(this.updatedRows[i])
                    return false;
        return true;
	}

	/**
	* 	@desc: enable/disable debuging
	*	@param:   mode - true/false
	*	@type: public;
	*/
	dataProcessor.prototype.enableDebug = function(mode){
		this._debug=convertStringToBoolean(mode);
	}

	/**
	* 	@desc: enable/disable named field for data syncing, will use column ids for grid
	*	@param:   mode - true/false
	*	@type: public;
	*/
	dataProcessor.prototype.enableDataNames = function(mode){
		this._endnm=convertStringToBoolean(mode);
	}

	/**
	* 	@desc: set if rows should be send to server automaticaly
	*	@param: mode - "row" - based on row selection changed, "cell" - based on cell editing finished, "off" - manual data sending
	*	@type: public;
	*/
	dataProcessor.prototype.setUpdateMode = function(mode){
		if(mode=="cell")
			this.autoUpdate = true;
		else{
			this.autoUpdate = false;
		}
		this.updateMode = mode;
	}


	dataProcessor.prototype.findRow = function(pattern){
    	for(var i=0;i<this.updatedRows.length;i++){
		    if(pattern==this.updatedRows[i])
	    			return i;
    	}
	    return -1;
    }

	/**
	* 	@desc: mark row as updated/normal. check mandatory fields,initiate autoupdate (if turned on)
	*	@param: rowId - id of row to set update-status for
	*	@param: state - true for "updated", false for "not updated"
	*	@param: forceUpdate - true to force immediately sync row in question with server
	*	@type: public;
	*/
	dataProcessor.prototype.setUpdated = function(rowId,state,forceUpdate){
		var rowInArray = this.findRow(rowId)
		if(rowInArray==-1)
			rowInArray = this.updatedRows.length;
		if(state){
			this.updatedRows[rowInArray] = rowId;
			this.setRowTextBold(rowId);
			this.checkBeforeUpdate(rowId,this.autoUpdate||forceUpdate);
		}else{
			this.updatedRows[rowInArray] = null;
			this.setRowTextNormal(rowId);
			//this.grid.setUserData(rowId,"!nativeeditor_status","")
			//var nstatus = this.grid.getUserData(rowId,"!nativeeditor_status")
			//if(nstatus=="deleted")
			//	this.grid.deleteRow(rowId)
		}

	}

    dataProcessor.prototype.setUpdatedTM = function(rowId,state){
        this._lccm=true;

        if (this._waitMode){
           this.autoUpdate=false;
           this.setUpdated(rowId,true);
           this.autoUpdate=true;
           }
        else
           this.setUpdated(rowId,true)

    }

    dataProcessor.prototype.setRowTextBold=function(rowId){
        if (this.obj.mytype=="tree")
            this.obj.setItemStyle(rowId,"font-weight:bold;");
        else
            this.obj.setRowTextBold(rowId);
    };
    dataProcessor.prototype.setRowTextNormal=function(rowId){
        if (this.obj.mytype=="tree")
            this.obj.setItemStyle(rowId,"font-weight:normal;");
        else
            this.obj.setRowTextNormal(rowId);
    };
	/**
	* 	@desc: check mandatory fields and varify values of cells, initiate update (if specified)
	*	@param: rowId - id of row to set update-status for
	*	@param: updateFl - true to start update process
	*	@type: public;
	*/
	dataProcessor.prototype.checkBeforeUpdate = function(rowId,updateFl){
		var fl = true;
		var mandExists = false;
		for(var i=0;i<this.mandatoryFields.length;i++){
			if(this.mandatoryFields[i]){
				mandExists = true;
				var val = this.obj.cells(rowId,i).getValue()
				var colName = this.obj.getHeaderCol(i)
				if((typeof(this.mandatoryFields[i])=="function" && this.mandatoryFields[i](val,colName)) || (typeof(this.mandatoryFields[i])!="function" && val.toString()._dhx_trim()!="")){
					this.obj.cells(rowId,i).cell.style.borderColor = "";
				}else{
					fl = false;
					this.obj.cells(rowId,i).cell.style.borderColor = "red";
				}
			}
		}
		if((fl || !mandExists) && updateFl)//if all mandatory fields are ok or there were no mandatory fields
		{
			this.sendData(rowId);
		}
	}
	/**
	* 	@desc: send row(s) values to server
	*	@param: rowId - id of row which data to send. If not specified, then all "updated" rows will be send
	*	@type: public;
	*/
	dataProcessor.prototype.sendData = function(rowId){
		if(rowId){//send some row, not all
			//[send data to server]
				if (!this._tSend)
	                var a1=this._getRowData(rowId);
				else
					var a1=this._getAllData();
                var a2=new dtmlXMLLoaderObject(this.afterUpdate,this,true);
                var a3=this.serverProcessor+((this.serverProcessor.indexOf("?")!=-1)?"&":"?");

				if ((this.onBUpd)&&(!this.onBUpd(rowId,this.obj.getUserData(rowId,"!nativeeditor_status")||"updated"))) return false;

				if (this._debug)
					alert("Send data to server \n URL:"+a3+"\n Data:"+a1);
                if (this._tMode!="POST")
                   a2.loadXML(a3+a1);
                else
                   a2.loadXML(a3,true,a1);


              this._waitMode++;
		}else{//send all rows one by one
			for(var i=0;i<this.updatedRows.length;i++){
				if(this.updatedRows[i]){
					this.checkBeforeUpdate(this.updatedRows[i],true)
					if (this._tSend) break;
				}
			}
		}
	}

	dataProcessor.prototype._getAllData = function(rowId){
		var out=new Array();
		var rs=new Array();
		for(var i=0;i<this.updatedRows.length;i++)
			if(this.updatedRows[i]){
				out[out.length]=this._getRowData(this.updatedRows[i],this.updatedRows[i]+"_");
				rs[rs.length]=this.updatedRows[i];
			}
		out[out.length]="ids="+rs.join(",");
		return out.join("&");
	}


	/**
	* 	@desc: define custom actions
	*	@param: name - name of action, same as value of action attribute
	*	@param: handler - custom function, which receives a XMl response content for action
	*	@type: private
	*/
	dataProcessor.prototype.defineAction = function(name,handler){
        if (!this._uActions) this._uActions=new Array();
            this._uActions[name]=handler;
    }


	/**

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -