• 使用JS代码固定GridView表头。


    var container = new Array();
    var onResizeHandler;
    
    function scrollbarWidth(){
        var w;
    
        if (! document.body.currentStyle)   document.body.currentStyle = document.body.style;
    
        if (document.body.currentStyle.overflowY == 'visible' || document.body.currentStyle.overflowY == 'scroll'){
            w = document.body.offsetWidth - document.body.clientLeft - document.body.clientWidth;
        }else{
            win = window.open("about:blank", "_blank", "top=0,left=0,width=100,height=100,scrollbars=yes");
            win.document.writeln('scrollbar');
            w = win.document.body.offsetWidth - win.document.body.clientLeft - win.document.body.clientWidth;
            win.close();
        }
    
        return w;
    }
    
    function getActualWidth(e){
        if (! e.currentStyle)   e.currentStyle = e.style;
    
        return  e.clientWidth - parseInt(e.currentStyle.paddingLeft) - parseInt(e.currentStyle.paddingRight);
    }
    
    function findRowWidth(r){
        for (var i=0; i < r.length; i++){
            r[i].actualWidth = getActualWidth(r[i]);
        }
    }
    
    function setRowWidth(r){
        for (var i=0; i < r.length; i++){
            r[i].width = r[i].actualWidth;
            r[i].innerHTML = '<span style="' + r[i].actualWidth + ';">' + r[i].innerHTML + '</span>';
        }
    }
    
    function fixTableWidth(tbl){
        for (var i=0; i < tbl.tHead.rows.length; i++)   findRowWidth(tbl.tHead.rows[i].cells);
        findRowWidth(tbl.tBodies[0].rows[0].cells);
        if (tbl.tFoot)  for (var i=0; i < tbl.tFoot.rows.length; i++)   findRowWidth(tbl.tFoot.rows[i].cells);
    
        //tbl.width = '';
    
        for (var i=0; i < tbl.tHead.rows.length; i++)   setRowWidth(tbl.tHead.rows[i].cells);
        setRowWidth(tbl.tBodies[0].rows[0].cells);
        if (tbl.tFoot)  for (var i=0; i < tbl.tFoot.rows.length; i++)   setRowWidth(tbl.tFoot.rows[i].cells);
    }
    
    function makeScrollableTable(tbl,scrollFooter,height){
        var c, pNode, hdr, ftr, wrapper, rect;
    
        if (typeof tbl == 'string') tbl = document.getElementById(tbl);
        if (tbl == null) {
            return;
        }
        pNode = tbl.parentNode;
        fixTableWidth(tbl);
    
        c = container.length;
        container[c] = document.createElement('<SPAN style="height: 100; overflow: auto;">');
        container[c].id = tbl.id + "Container";
        pNode.insertBefore(container[c], tbl);
        container[c].appendChild(tbl);
        container[c].style.width = tbl.clientWidth + 2 * tbl.clientLeft + scrollbarWidth() + 20;
    
        hdr = tbl.cloneNode(false);
        hdr.id += 'Header';
        hdr.appendChild(tbl.tHead.cloneNode(true));
        tbl.tHead.style.display = 'none';
    
        if (!scrollFooter || !tbl.tFoot){
            ftr = document.createElement('<SPAN style="1;height:1;clip: rect(0 1 1 0);background-color:transparent;">');
            ftr.id = tbl.id + 'Footer';
            ftr.style.border = tbl.style.border;
            ftr.style.width = getActualWidth(tbl) + 2 * tbl.clientLeft;
            ftr.style.borderBottom = ftr.style.borderLeft = ftr.style.borderRight = 'none';
        }else{
            ftr = tbl.cloneNode(false);
            ftr.id += 'Footer';
            ftr.appendChild(tbl.tFoot.cloneNode(true));
            ftr.style.borderTop = 'none';
            tbl.tFoot.style.display = 'none';
        }
    
        wrapper = document.createElement('<table border=0 cellspacing=0 cellpadding=0>');
        wrapper.id = tbl.id + 'Wrapper';
        pNode.insertBefore(wrapper, container[c]);
    
        wrapper.insertRow(0).insertCell(0).appendChild(hdr);
        wrapper.insertRow(1).insertCell(0).appendChild(container[c]);
        wrapper.insertRow(2).insertCell(0).appendChild(ftr);
    
        wrapper.align = tbl.align;
        tbl.align = hdr.align = ftr.align = 'left';
        hdr.style.borderBottom = 'none';
        tbl.style.borderTop = tbl.style.borderBottom = 'none';
    
        // adjust page size
        if (c == 0 && height == 'auto'){
            onResizeAdjustTable();
            onResizeHandler = window.onresize;
            window.onresize = onResizeAdjustTable;
        }else{
            container[c].style.height = height;
        }
    }
    
    function onResizeAdjustTable(){
        if (onResizeHandler) onResizeHandler();
    
        var rect = container[0].getClientRects()(0);
        var h = document.body.clientHeight - (rect.top + (document.body.scrollHeight - rect.bottom));
        container[0].style.height = (h > 0) ? h : 1;
    }
    
    function printPage(){
        var tbs = document.getElementsByTagName('TABLE');
        var e;
    
        for (var i=0; i < container.length; i++)    container[i].style.overflow = '';
    
        window.print();
    
        for (var i=0; i < container.length; i++)    container[i].style.overflow = 'auto';
    }
    View Code

    GridView固定表头方式很多,总体思路其实都差不多。

    之前用的CSS的方式非常方便,可惜从IE8开始就不支持了。

    最近在网上找了一个方式,还算比较方便的,最主要的是兼容性很好。

    首先引用JS,这个JS网上可以去下载,上面贴的代码就是JS里面的代码,很少。

    源文件里面的作者的说明我删掉了,大家用的时候还是去下载原版的比较好,尊重原著嘛。

    不过原版我使用的时候习惯在GV外面放个DIV,这样会出现横向的滚动条,我稍微修改了一点。

    <script src="javascript/ScrollableTable.js" type="text/javascript"></script>

    然后写:<body onload="makeScrollableTable('GridView1',true,'500px')">   // ------这里是重点

    然后注册一下GV的PreRender事件:

    protected void GridView1_PreRender(object sender, EventArgs e)
        {
            GridView1.UseAccessibleHeader 
    = true;
            GridView1.HeaderRow.TableSection 
    = TableRowSection.TableHeader;
        }

    最后要删除页面头部的

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    这段代码的作用解释起来很烦,有兴趣的可以看下http://www.w3school.com.cn

    不过我不太明白为什么要删除这段代码,难道这个方式不符合W3C的规则吗?我看了JS好像也没什么区别啊,不懂,反正删了页面貌似也没什么变化,囧!

    这样配置下来,就可以固定表头了,大家试试吧!

  • 相关阅读:
    ReentrantLock和AQS
    CAS
    java8中ConcurrentHashMap
    java8中的HashMap
    TCP和UDP
    慢查询日志和profiling
    explain的使用
    select、poll、epoll之间的区别
    I/O模型
    生产者-消费者模式
  • 原文地址:https://www.cnblogs.com/hopedilei/p/3216868.html
Copyright © 2020-2023  润新知