Skip to content
Advertisement

Keep table height fixed i.e., inside a block , and also add a scrollview inside my table

I have an html table which shows all the records fetched from mysql database. This is my table created:

JavaScript

My css:

JavaScript

Now, in my javascript, I create the table rows and cells dynamically, based on the number of records fetched. This is my code snippet:

JavaScript

So, for each response, it creates a row and inserts the values to its appropriate cells. So, the table height is dynamic (it depends on the number of records fetched).

What I want is the table to have a fixed height (inside a block), inside which there will be a vertical scrollbar which you can use to scroll up and down to view all the records. Along with that the first row table.rows[0] should be freezed so that it doesn’t move along with the scroll (as it contains the label for each cell).

How do I do that? Any help would be appreciated.

Advertisement

Answer

One way is that you have to wrap your table inside a div and then use overflow: auto; to that div.

Here is the example https://codepen.io/rk-v/pen/BaRqPoB

Advertisement