How can I lock the first row and first column of a table when scrolling, possibly using JavaScript and CSS?

后端 未结 13 2049
攒了一身酷
攒了一身酷 2020-11-27 03:11

How can I create a table that has its first row and first column both locked, as in Excel, when you activate \'freeze panes\'? I need the table to both scroll horizontally

相关标签:
13条回答
  • 2020-11-27 03:59

    I've posted my jQuery plugin solution here: Frozen table header inside scrollable div

    It does exactly what you want and is really lightweight and easy to use.

    0 讨论(0)
  • 2020-11-27 04:00

    There are quite a few cross browser solutions for this today, among which are SuperTable which I like due to its elegance and simplicity (now being continued with MooGrid) and SlickGrid with its awesome set of features.

    0 讨论(0)
  • 2020-11-27 04:02

    You can do it, without javascript

    see this link: http://yonax73.blogspot.com/2014/09/tabla-con-cabecera-estatica-cuerpo-con.html

    or live demo: http://jsfiddle.net/yonatanalexis22/aeeme8mt/7/

    table{
    border-spacing: 0;
    display: flex;/*Se ajuste dinamicamente al tamano del dispositivo**/
    max-height: 40vh; /*El alto que necesitemos**/
    overflow-y: auto; /**El scroll verticalmente cuando sea necesario*/
    overflow-x: hidden;/*Sin scroll horizontal*/
    table-layout: fixed;/**Forzamos a que las filas tenga el mismo ancho**/
    width: 98vw; /*El ancho que necesitemos*/
    border:1px solid gray;}
    
    0 讨论(0)
  • 2020-11-27 04:03

    You'd have to test it but if you embedded an iframe within your page then used CSS to absolutely position the 1st row & column at 0,0 in the iframe page would that solve your problem?

    0 讨论(0)
  • 2020-11-27 04:04

    You need two tables, where the first one is an exact overlay over the second one. The second one contains all the data, where the first one just contains the first column. You have to synchronize it's width and depending on the content also the height of it's rows.

    Additional to this two tables, you need a third one. That's the first row, which lays exactly between the other two and has to be synchronized in the same way.

    You will need absolute positioning here. Next, you would synchronize the scrolling of the data table with the scrolling positions of the head row and first column table.

    That works very well in all major browsers, except for one issue: The synchronized scrolling will flutter. To fix that, you need two outher div containers that hold a clone of the content of the header row and the first column. When scrolling vertically, you display the header row clone to prevent fluttering, while you reposition the original in the background. When scrolling horizontally, you would show the first row clone. Same thing here.

    0 讨论(0)
  • 2020-11-27 04:05

    Oh well, I looked up for scrollable table with fixed column to understand the need of this specific requirement and your question was one of it with no close answers..

    I answered this question Large dynamically sized html table with a fixed scroll row and fixed scroll column which inspired to showcase my work as a plugin https://github.com/meetselva/fixed-table-rows-cols

    The plugin basically converts a well formatted HTML table to a scrollable table with fixed table header and columns.

    The usage is as below,

    $('#myTable').fxdHdrCol({
        fixedCols    : 3,       /* 3 fixed columns */
        width        : "100%",  /* set the width of the container (fixed or percentage)*/
        height       : 500      /* set the height of the container */
    });
    

    You can check the demo and documentation here

    0 讨论(0)
提交回复
热议问题