当前位置: 代码迷 >> Web前端 >> ligerUi grid打包调用方法
  详细解决方案

ligerUi grid打包调用方法

热度:938   发布时间:2013-11-02 19:41:10.0
ligerUi grid封装调用方法
/**
 * 获取页面参数
 */
function getPageSize(){
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac   
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
     }else { 
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
     }
    var windowWidth, windowHeight;
    if (self.innerHeight) { 
        if (document.documentElement.clientWidth) {
            windowWidth = document.documentElement.clientWidth;
        }
        else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    }else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode   
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
     }else if (document.body) { // other Explorers   
                windowWidth = document.body.clientWidth;
                windowHeight = document.body.clientHeight;
    }
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    }
    else {
        pageHeight = yScroll;
    }
    if (xScroll < windowWidth) {
        pageWidth = xScroll;
    }
    else {
        pageWidth = windowWidth;
    }
    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
    return arrayPageSize;
}

/**
 * 自适应iframe
 * @param {Object} down
 */
function dyniframesize(down){
    var pTar = null;
    if (document.getElementById) {
        pTar = document.getElementById(down);
    }
    else {
        eval('pTar = ' + down + ';');
    }
    if (pTar && !window.opera) {
        pTar.style.display = "block"
        if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight) {
            pTar.height = pTar.contentDocument.body.offsetHeight + 20;
            pTar.width = pTar.contentDocument.body.scrollWidth + 20;
        }
        else 
            if (pTar.Document && pTar.Document.body.scrollHeight) {
                pTar.height = pTar.Document.body.scrollHeight;
                pTar.width = pTar.Document.body.scrollWidth;
            }
    }
}

/**
 * 判断iframe是否加载完毕
 */
function frameIsReady(iframe){
    if (!/*@cc_on!@*/0) { //if not IE
        iframe.onload = function(){
			return true;
        };
    }
    else {
        iframe.onreadystatechange = function(){
            if (iframe.readyState == "complete") {
				return true;
            }
        };
    }
	return false;
}
/**
 * 如果iframe还没有加载完成则延迟几秒后加载
 */
function delayLoad(s,call){
	window.setTimeout(call,s);
}
//重置grid
function resizeGrid(){
    if (jQuery(".l-panel-body").find(".l-grid-body2").css("height") != undefined) {
        var viewHeightTemp = window.parent.jQuery('#gridFrame').parent().height();
        var viewHeight = viewHeightTemp - 184;
        jQuery(".l-panel-body").find(".l-grid-body2").css("height", viewHeight);
        var viewWidth = 0;
        var grid = jQuery("#dg_pastbusicustInfo").ligerGrid();
        for (i = 0; i < grid.columns.length; i++) {
            if (grid.columns[i]._hide != true && grid.columns[i]._hide != "true") {
                viewWidth = viewWidth + grid.columns[i]._width;
            }
        }
        viewWidth = viewWidth - 1;
        jQuery(".l-panel-body").find(".l-grid-header-inner").css("width", viewWidth);
        jQuery(".l-panel-body").find(".l-grid-body-inner").css("width", viewWidth);
    }
}

/**
 * 合并单元格
 * @param {Object} gridid
 * @param {Object} columnid
 */
function mergecell(gridid, columnid){ //参数:表格id和列索引关键字,根据表格具体值来动态合并单元格
    var i, j, k, x;
    i = -1;
    k = 1;
    j = "";
    x = 0;
    var cellname = "";
    jQuery("td[idjQuery='|" + columnid + "']", gridid).each(function(){
        if (j == jQuery("div", this).text() || jQuery("div", this).text() == '') {
            jQuery(this).addClass("l-remove");
            k++;
            jQuery("td[id='" + cellname + "']", gridid).attr("rowspan", k.toString());
        }
        else {
            j = jQuery("div", this).text();
            var a = jQuery(this);
            cellname = a.attr("id"); //得到点击处的id
            k = 1;
            x = i;
        }
        i++;
    });
    jQuery(".l-remove").remove();
}

/**
 *模拟map
 */
Map = function(){
    var mapAddM = {
        /**
         * entry函数
         * @param {Object} key
         * @param {Object} val
         */
        entry: function(key, val, flag){
            this.key = key;
            this.value = val;
            this.flag = flag;
        },
        //put方法
        put: function(key, val, flag){
            this.store[this.store.length] = new this.entry(key, val, flag);
        },
        //get方法
        get: function(key){
            for (var i = 0; i < this.store.length; i++) {
                if (this.store[i].key === key) 
                    return this.store[i].value;
            }
        },
        //get方法
        getFlag: function(key){
            for (var i = 0; i < this.store.length; i++) {
                if (this.store[i].key === key) 
                    return this.store[i].flag;
            }
        },
        //remove方法
        remove: function(key){
            for (var i = 0; i < this.store.length; i++) {
                this.store[i].key === key && this.store.splice(i, 1);
            }
        },
        //keyset
        keySet: function(){
            var keyset = new Array;
            for (var i = 0; i < this.store.length; i++) 
                keyset.push(this.store[i].key);
            return keyset;
        },
        //valset
        valSet: function(){
            var valSet = new Array;
            for (var i = 0; i < this.store.length; i++) 
                valSet.push(this.store[i].value);
            return valSet;
        },
        //flagSet
        flagSet: function(){
            var flagSet = new Array;
            for (var i = 0; i < this.store.length; i++) 
                flagSet.push(this.store[i].flag);
            return flagSet;
        },
        //clear
        clear: function(){
            this.store.length = 0;
        },
        //size 
        size: function(){
            return this.store.length;
        },
        /**
         *  迭代子
         */
        iterator: function(){
            //TODO 待实现
            var obj = this.keySet();//所有的key集合
            var idx = 0;
            var me = {
                /**
                 * 当前key
                 */
                current: function(){
                    return obj[idx - 1];
                },
                /**
                 * 第一个key
                 */
                first: function(){
                    return obj[0];
                },
                /**
                 * 最后一个key
                 */
                last: function(){
                    return obj[obj.length - 1];
                },
                /**
                 * 是否还有下一个元素
                 */
                hasNext: function(){
                    idx++;
                    if (idx > obj.length || null == obj[obj.length - 1]) 
                        return false;
                    return true;
                }
            };
            return me;
        }
    };
    for (var method in mapAddM) {
        this.store = new Array;
        Map.prototype[method] = mapAddM[method];
    }
}

/**
 * 生成column
 * @param {Object} map
 */
function createColumns(map,gridWidth){
    var columns = [];
    var i = 0;
    for (var it = map.iterator(); it.hasNext();) {
        var currentKey = it.current();//本次循环的key
        var currentVal = map.get(currentKey);//当前value
        var currentFlag = map.getFlag(currentKey);//判断是否隐藏该列
        var columni = {};
        columni.name = currentKey;
        columni.display = currentVal;
        columni.align = 'center';
        columni.width = gridWidth?gridWidth:120;
        if ((currentFlag && (currentFlag == "hide"))) {
            columni.hide = 'true';
            columni.width = 0.1;
            
        }
        columns.push(columni);
        i++;
    }
    return columns;
}


/**
 * 请求数据之前
 */
function beforeLoad(){

}

/**
 * 数据渲染完成后
 */
function afterDataRend(){
    jQuery.ligerDialog.closeWaitting();
}

/**
 * 渲染grid
 * @param {Object} year
 * @param {Object} customer
 */
function renderLigerGrid(divId, data, map,gridWidth,callback){
	var opptions = {
        columns: createColumns(map,gridWidth),
        data: data,
		page:1,
        pageSize: 30,
        width: '100%',
        height: '100%',
        dataAction: 'server',
        onLoading: beforeLoad,
		cssClass:"myGrid",
		//rownumbers:true,//是否显示行号
		rowHeight:25,
        onLoaded: function (){
			afterDataRend();//加载完成之后
		},
		onAfterShowData : function(currenData){
		},
		onRendered:function (){
		},
        colDraggable: false,
        rowDraggable: false,
        enabledEdit: true,
        clickToEdit: false,
        clickToEdit: false,
		checkbox:window.parent.checkBox, 
        isScroll: true
    };
	if(!manager){
		//避免注册多次事件
		opptions.onDblClickRow = function (data, rowindex, rowobj){
			if(window.parent.dbClickRow)
				window.parent.dbClickRow(data,rowindex,rowobj);//如果父窗口有此事件则调用父窗口事件
        }
		manager = jQuery("#" + divId).ligerGrid(opptions);
	}else{
		//重新渲染
		manager.options.newPage = 1;
		manager.options.data = $.extend(true, {}, data);
        manager.loadData(true);
	}
}

/**
 * 通用ajax请求
 * @param {Object} _url
 * @param {Object} divId
 */
function render(_url, _data, divId, map,gridWidth,callback){
	try {
		//本地数据
		if(jQuery.parseJSON(_url)){
		   var d = jQuery.parseJSON(_url);
		   if (d.Total > 0) {
			   
                  renderLigerGrid(divId, d, map,gridWidth,callback);//请求成功
			}else{
                 renderLigerGrid(divId, d, map,gridWidth,callback);//请求成功
			}
		}
	} catch (e) {
	   //服务器数据
	    try {
	        jQuery.ligerDialog.waitting('正在加载数据...');
	        jQuery.ajax({
	            url: _url,
	            type: "post",
	            data: {
	                param: JSON.stringify(_data)
	            },
	            dataType: "json",
	            success: function(d){
	                if (d.Total > 0) {
		                    renderLigerGrid(divId, d, map,gridWidth,callback);//请求成功
					}
	                else{
		                    renderLigerGrid(divId, d, map,gridWidth,callback);//请求成功
					}
	                jQuery.ligerDialog.closeWaitting();
					if(callback){
						afterGridRendCallBack(callback);//渲染完成后回调函数
					}
	            },
	            error: function(a, b, c){
	            	jQuery.ligerDialog.error(a.status);
					jQuery.ligerDialog.error(a.readyState);
					jQuery.ligerDialog.error(b);
					jQuery.ligerDialog.error(c);
	                jQuery.ligerDialog.closeWaitting();
	            }
	        });
	    } 
	    catch (e) {
	        jQuery.ligerDialog.error(e);
	    }
	}
}

var manager;//ligerGridManager对象
/**
 * 生成文件
 * @param {Object} data
 */
function dataToTxt(data){
    var file_name = "C://temp.txt";
    file_name = file_name.split("//").join("////");
    var FSO = new ActiveXObject("Scripting.FileSystemObject");
    var f1 = FSO.CreateTextFile(file_name, true);
    f1.write(data);
    f1.close();
}
/**
 * 自定义表格渲染完后回调函数
 */
function afterGridRendCallBack(callback){
	var gridBody = jQuery("div.l-grid-body2");
	callback.apply(gridBody,arguments);
}

?/**
? * 获取url参数(主要解决iframe传递参数)
? */
?function getUrlParam(_url,key){
??if (_url.lastIndexOf("?") != -1) {
???var queryStr = _url.substring(_url.lastIndexOf("?") + 1, _url.length);
???if(!key)
????return queryStr;//返回所有参数
???else{
????var params? = queryStr.split("&");
????for(var j = 0 ;j < params.length;j++){
?????? var tmp = params[j].split("=");
?????? if(tmp[0]==key){
????????? return tmp[1];
????????? break;
?????? }
????}
???}
????
??}
?}

?

  相关解决方案