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

📄 gridview.js

📁 gridview你超强功能很好的东东
💻 JS
字号:
//var self = new Object();

//参数divid: 表外的div层的id
function TableOnMouseDown(divid)
{
    //当前按下的th
    if(self.currentTH != null) 
	{
		return;
    }
    
	//鼠标点击处的object
	var obj = document.elementFromPoint(window.event.clientX, window.event.clientY);
    var objL = document.elementFromPoint(window.event.clientX - 1, window.event.clientY);

	if(obj.tagName.toLowerCase() == "th") 
	{   
	    if(obj.previousSibling == null)
	    {
	        return;
	    }
	    
	    if(objL.nextSibling != null)
	    {
	        if(obj.getAttribute("id") == objL.nextSibling.getAttribute("id"))
	        {
	            obj = obj.previousSibling;
	        }
	        else
	        {
	            return;
	        }
	    }
	  
		if(obj.className == "fixed") 
		{
			return;
		}
        
		//当前鼠标的X坐标
		self.currentX = window.event.clientX;
		//当前被点击的th中的div
		self.currentTH = obj.childNodes[0];
		//捕获th
		self.currentTH.setCapture();
        
        //dv主层的Y坐标
        var e = document.getElementById(divid);
        var t = 0;
        
        while (e = e.offsetParent)
	    {
	        t += e.offsetTop; 
	    }
        
        //创建currentLine
        if(self.currentLine == null)
		{
			self.currentLine = document.createElement("div");
			self.currentLine.style.position = "absolute";
			self.currentLine.style.borderLeftWidth = "1px"
			self.currentLine.style.width = "1px"
			self.currentLine.style.borderLeftColor = "black";
			self.currentLine.style.borderLeftStyle = "dashed";
			self.currentLine.style.zIndex  = 3000;
			self.currentLine.style.height  = document.getElementById(divid).clientHeight;
			self.currentLine.style.posTop  = t;
			self.currentLine.style.posLeft = window.event.clientX - 1;
			window.document.body.insertAdjacentElement("afterBegin", self.currentLine);
		}
		else
		{
		    self.currentLine.style.height  = document.getElementById(divid).clientHeight;
		    self.currentLine.style.posTop  = t;
			self.currentLine.style.posLeft = window.event.clientX - 1;
			self.currentLine.style.visibility = "";
		}
		
		self.currentWidth = 0;
	}
}

function TableOnMouseUp(tbodyid)
{
	if(self.currentTH != null)
	{   
	    if((parseInt(self.currentTH.clientWidth) + self.currentWidth) < 30)
		{
			self.currentTH.style.width = 30+"px";
		}
		else
		{
			//改变th表头列的宽度
			self.currentTH.style.width = parseInt(self.currentTH.clientWidth) + self.currentWidth;
		}

		//改变相关列的宽度
		resizeCell(tbodyid, self.currentTH.columnIndex, self.currentTH.style.width);
        

	    //停止捕获
		self.currentTH.releaseCapture();
		self.currentTH = null;
		self.currentLine.style.visibility = "hidden";
		self.currentWidth = 0;
	}	
}

function TableOnMouseMove()
{
	if(self.currentTH != null)
	{
	    var pos = Math.round(window.event.clientX - self.currentX);
	    
	    //当前被拖动的宽度
		self.currentWidth = pos;
		self.currentLine.style.posLeft = window.event.clientX - 1;
	}
}

function resizeCell(tid, idx, width)
{
    //tbody
	var cells = document.getElementById(tid);
	//tbody中的所有行
	var rows = cells.childNodes;

	for (var i = 0; i < rows.length; i++)
	{   
	    //tbody行中的要被改变宽度的列
		var cell = rows[i].childNodes[idx].childNodes[0];
		var resetPattern = /style=[^\s\t\n]+/;
		cell.outerHTML = cell.outerHTML.toString().replace(resetPattern, "style='width:" + width + "'");
	}
}

//参数divid: 表外的div层的id
function IniCellWidth(divid)
{
    var table = document.getElementById(divid).childNodes[0];
    var childs = table.rows[0].cells;
    
    var wdt = 0;
    var cot = 0;
    
    //清除标题行每列th的宽度
    for(var i = 0;i < childs.length; i++)
    {
	    if(childs[i].style.width != "")
	    {
		    childs[i].childNodes[0].style.width = childs[i].style.width;
		    wdt += parseInt(childs[i].style.width);
		    cot += 1;
		    childs[i].style.width = "";
	    }
    }
    
    var w = (table.clientWidth - wdt - parseInt(table.cellPadding)*childs.length*2 - parseInt(table.cellPadding)) / (childs.length - cot);

    //parseInt(table.cellPadding) - parseInt(table.style.borderWidth)*2*childs.length
    
    if( w > 0)
    {
        //初始化表题行每列的宽度
        for(var i = 0;i < childs.length; i++)
        {
            if(childs[i].childNodes[0].style.width == "")
	        {
		        childs[i].childNodes[0].style.width = w + "px";
	        }
        }
    }
    table.style.width = "";
}

⌨️ 快捷键说明

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