网格视图列宽 - 如何停止“收缩到适合”行为

3

我目前正在尝试实现一个带有冻结表头的GridView。我已经使用在网上找到的JavaScript将表头冻结。以下是代码:

    var GridId = "<%=dgContacts.ClientID %>";
    var ScrollHeight = 180;
    var ScrollWidth = 700;
    window.onload = function () {
        enablePostLog();
        var grid = document.getElementById(GridId);
        var gridWidth = grid.offsetWidth;
        var headerCellWidths = new Array();
        for (var i = 0; i < grid.getElementsByTagName("TH").length; i++) {
            headerCellWidths[i] = grid.getElementsByTagName("TH")[i].offsetWidth;
        }
        grid.parentNode.appendChild(document.createElement("div"));
        var parentDiv = grid.parentNode;

        var table = document.createElement("table");
        for (i = 0; i < grid.attributes.length; i++) {
            if (grid.attributes[i].specified && grid.attributes[i].name != "id") {
                table.setAttribute(grid.attributes[i].name, grid.attributes[i].value);
            }
        }
        table.style.cssText = grid.style.cssText;
        table.style.width = gridWidth + "px";
        table.style.tableLayout = "fixed";
        table.appendChild(document.createElement("tbody"));
        table.getElementsByTagName("tbody")[0].appendChild(grid.getElementsByTagName("TR")[0]);
        var headerRow = table.getElementsByTagName("TH");

        var gridRow = grid.getElementsByTagName("TR")[0];
        for (var i = 0; i < headerRow.length; i++) {
            var width;
            if (headerCellWidths[i] > gridRow.getElementsByTagName("TD")[i].offsetWidth) {
                width = headerCellWidths[i];
            }
            else {
                width = gridRow.getElementsByTagName("TD")[i].offsetWidth;
            }
            headerRow[i].style.width = parseInt(width - 3) + "px";
            gridRow.getElementsByTagName("TD")[i].style.width = parseInt(width - 3) + "px";
        }
        parentDiv.removeChild(grid);

        var dummyHeader = document.createElement("div");
        dummyHeader.setAttribute('id', 'divHeader');
        dummyHeader.style.cssText = "margin-left: auto; margin-right: auto; overflow: hidden; width: " + ScrollWidth + "px";
        dummyHeader.appendChild(table);
        parentDiv.appendChild(dummyHeader);
        var scrollableDiv = document.createElement("div");
        gridWidth = parseInt(gridWidth) + 17;
        scrollableDiv.setAttribute('id', 'divBody');
        scrollableDiv.style.cssText = "margin-left: auto; margin-right: auto; overflow: auto; height: " + ScrollHeight + "px; width: " + ScrollWidth + "px";
        scrollableDiv.appendChild(grid);
        scrollableDiv.onscroll = scrollHeader;
        parentDiv.appendChild(scrollableDiv);
    }

    function scrollHeader() {
        var header = document.getElementById('divHeader');
        var body = document.getElementById('divBody');
        header.scrollLeft = body.scrollLeft;
    }

我遇到的问题是GridView位于一个固定宽度的div内,列添加的宽度样式被忽略了。
最终的HTML代码如下: http://i.stack.imgur.com/xDzez.png 实际渲染的表格如下(“View”和“Notify”列的问题最为明显): http://i.stack.imgur.com/rA35z.png 如果我移除外层div的固定宽度,列的宽度就会自动调整,但是我会失去滚动条。它似乎试图缩小表格单元格中的空白以适应div。这并不必要,因为外层div已经使用了overflow: auto。是否有样式或其他内容可以阻止浏览器这样做?

1
嘿..我使用了相同类型的脚本。我有一个包含5000条记录的网格。当我在window.onload()上调用此脚本时,页面会被阻塞,直到脚本执行完毕。这会影响性能。你能指导我如何提高性能吗? - JayOnDotNet
1个回答

6

结果表明,我需要在两个表格中都使用 table-layout: fixedwidth: 100%。我已经尝试了 table-layout 的解决方案,但没有意识到也需要将宽度设置为 100%。

最终的 JavaScript 代码:

    var GridId = "<%=dgContacts.ClientID %>";
    var ScrollHeight = 180;
    var ScrollWidth = 700;
    window.onload = function () {
        enablePostLog();


        var grid = document.getElementById(GridId);
        var gridWidth = grid.offsetWidth;
        var headerCellWidths = new Array();
        for (var i = 0; i < grid.getElementsByTagName("TH").length; i++) {
            headerCellWidths[i] = grid.getElementsByTagName("TH")[i].offsetWidth;
        }
        grid.parentNode.appendChild(document.createElement("div"));
        var parentDiv = grid.parentNode;

        var table = document.createElement("table");
        for (i = 0; i < grid.attributes.length; i++) {
            if (grid.attributes[i].specified && grid.attributes[i].name != "id") {
                table.setAttribute(grid.attributes[i].name, grid.attributes[i].value);
            }
        }
        table.style.cssText = grid.style.cssText;
        table.appendChild(document.createElement("tbody"));
        table.getElementsByTagName("tbody")[0].appendChild(grid.getElementsByTagName("TR")[0]);
        var headerRow = table.getElementsByTagName("TH");

        var gridRow = grid.getElementsByTagName("TR")[0];
        for (var i = 0; i < headerRow.length; i++) {
            var width;
            if (headerCellWidths[i] > gridRow.getElementsByTagName("TD")[i].offsetWidth) {
                width = headerCellWidths[i];
            }
            else {
                width = gridRow.getElementsByTagName("TD")[i].offsetWidth;
            }
            gridRow.getElementsByTagName("TD")[i].style.width = parseInt(width - 3) + "px";
            if (i == headerRow.length - 1) {
                width = width + getScrollBarWidth();
            }
            headerRow[i].style.width = parseInt(width - 3) + "px";
        }
        parentDiv.removeChild(grid);
        grid.style.tableLayout = "fixed";
        table.style.tableLayout = "fixed";
        grid.style.width = "100%";
        table.style.width = "100%";
        var dummyHeader = document.createElement("div");
        dummyHeader.setAttribute('id', 'divHeader');
        dummyHeader.style.cssText = "margin-left: auto; margin-right: auto; overflow: hidden; width: " + ScrollWidth + "px";
        dummyHeader.appendChild(table);
        parentDiv.appendChild(dummyHeader);
        var scrollableDiv = document.createElement("div");
        scrollableDiv.setAttribute('id', 'divBody');
        scrollableDiv.style.cssText = "margin-left: auto; margin-right: auto; overflow: auto; height: " + ScrollHeight + "px; width: " + ScrollWidth + "px";
        scrollableDiv.appendChild(grid);
        scrollableDiv.onscroll = scrollHeader;
        parentDiv.appendChild(scrollableDiv);
    }

    function scrollHeader() {
        var header = document.getElementById('divHeader');
        var body = document.getElementById('divBody');
        header.scrollLeft = body.scrollLeft;
    }

    function getScrollBarWidth() {
        var inner = document.createElement('p');
        inner.style.width = "100%";
        inner.style.height = "200px";

        var outer = document.createElement('div');
        outer.style.position = "absolute";
        outer.style.top = "0px";
        outer.style.left = "0px";
        outer.style.visibility = "hidden";
        outer.style.width = "200px";
        outer.style.height = "150px";
        outer.style.overflow = "hidden";
        outer.appendChild(inner);

        document.body.appendChild(outer);
        var w1 = inner.offsetWidth;
        outer.style.overflow = 'scroll';
        var w2 = inner.offsetWidth;
        if (w1 == w2) w2 = outer.clientWidth;

        document.body.removeChild(outer);

        return (w1 - w2);
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接