【问题标题】:Creating HTML table with sticky headers and columns using only CSS仅使用 CSS 创建带有粘性标题和列的 HTML 表格
【发布时间】:2016-02-11 19:15:57
【问题描述】:

我有一个表格,我正在尝试在其中制作标题,并且其左侧的任意数量的列都是“粘性的”。就像在表格数据垂直滚动时一样,标题保持在顶部。如果有足够的列垂直滚动,数据和标题是垂直滚动的,但有些列会粘住。

这是一个带有粘性标题的可垂直和水平滚动的表格。

http://jsfiddle.net/7b29Lkwy/5/

重要的部分是

.sticky-table {
    width: 100%;
    height: 100%;
    overflow-x: scroll;
}
.scroll {
    float: left; /* Makes with total content width */
    min-width: 100%; /* In case there isn't enough columns */
    height: calc(100% - 35px); /* Assuming we know head height */
    overflow: scroll;
}

但我不确定如何在包含粘性列的同时做到这一点。

【问题讨论】:

  • 将标题放在一个单独的 div 中,不会随其余部分一起滚动。

标签: css google-chrome


【解决方案1】:

你需要一些js来让它工作。

滚动条:

<button title="Up" onmousedown="upp();" onmouseup="upp(1);">Up</button>
<button title="Left" onmousedown="left();" 
        onmouseup="left(1);">&lt;&lt;</button>
<button title="Right" onmousedown="right();" 
        onmouseup="right(1);">&gt;&gt;</button>
<button title="Down" onmousedown="down();" onmouseup="down(1);">Dn</button>

将 t1 替换为您的表 ID

if(!myTable){myTable=document.getElementById("t1");

当然还有工作的js

var myRow=1;
  var myCol=1;
  var myTable;
  var noRows;
  var myCells,ID;

function setUp(){
    if(!myTable){myTable=document.getElementById("t1");}
     myCells = myTable.rows[0].cells.length;
    noRows=myTable.rows.length;

    for( var x = 0; x < myTable.rows[0].cells.length; x++ ) {
        colWdth=myTable.rows[0].cells[x].offsetWidth;
        myTable.rows[0].cells[x].setAttribute("width",colWdth-4);

    } 
}

function right(up){
    if(up){window.clearTimeout(ID);return;}
    if(!myTable){setUp();}

    if(myCol<(myCells)){
        for( var x = 0; x < noRows; x++ ) {
            myTable.rows[x].cells[myCol].style.display="";
        }
        if(myCol >1){myCol--;}

        ID = window.setTimeout('right()',100);
    }
}

function left(up){
    if(up){window.clearTimeout(ID);return;}
    if(!myTable){setUp();}

    if(myCol<(myCells-1)){
        for( var x = 0; x < noRows; x++ ) {
            myTable.rows[x].cells[myCol].style.display="none";
        }
        myCol++
        ID = window.setTimeout('left()',100);

    }
}

function down(up){
    if(up){window.clearTimeout(ID);return;}
    if(!myTable){setUp();}

    if(myRow<(noRows-1)){
            myTable.rows[myRow].style.display="none";
            myRow++    ;

            ID = window.setTimeout('down()',100);
    }    
}

function upp(up){
    if(up){window.clearTimeout(ID);return;}
    if(!myTable){setUp();}

    if(myRow<=noRows){
        myTable.rows[myRow].style.display="";
        if(myRow >1){myRow--;}
        ID = window.setTimeout('upp()',100);
    }    
}

详细说明请看这里 http://www.codeproject.com/Articles/20017/Freeze-Panes-like-Excel

更新: 问题的解决方法是使用这个jquery table plugin -> jtable

【讨论】:

  • 我更愿意支持原生滚动和滚动条。
  • 这个another SO?
  • 具有固定列但没有标题。我希望两者都做。看来我只能找到一个解决方案,而不是两个。
  • jtable Jquery 表插件?
  • 这可能行得通。虽然我注意到他们的例子有一些错误。我会关注它的开发,谢谢!
猜你喜欢
  • 1970-01-01
  • 2020-01-30
  • 2017-10-15
  • 2021-09-08
  • 2021-10-29
  • 2019-10-29
  • 1970-01-01
  • 2020-03-26
  • 1970-01-01
相关资源
最近更新 更多