If I defined an empty table in my index.html:
<body>
<table width="800" border="0" class="my-table">
<tr>
</tr>
</table>
</body>
Then, I add row and columns to my-table by invoke the following Javascript code:
var myTable = $('.my-table');
var myArr=GET_FROM_SERVER //server returns an arry of object, myArr.length>50
for(var i=0; i<myArr.length)
myTable.append("<tr id="+i+">"
+" <td>"+myArr[i].name+"</td>"
+"<td>"+myArr[i].address+"</td>"
+"</tr>");
myArr
is an array of object get from server, the length of this array could be more than 50.
I got all of this working successfully, my question is, how can I add scroll bar to this table so that if there are too many rows, user could use scroll bar to check the table content.
If I defined an empty table in my index.html:
<body>
<table width="800" border="0" class="my-table">
<tr>
</tr>
</table>
</body>
Then, I add row and columns to my-table by invoke the following Javascript code:
var myTable = $('.my-table');
var myArr=GET_FROM_SERVER //server returns an arry of object, myArr.length>50
for(var i=0; i<myArr.length)
myTable.append("<tr id="+i+">"
+" <td>"+myArr[i].name+"</td>"
+"<td>"+myArr[i].address+"</td>"
+"</tr>");
myArr
is an array of object get from server, the length of this array could be more than 50.
I got all of this working successfully, my question is, how can I add scroll bar to this table so that if there are too many rows, user could use scroll bar to check the table content.
Share Improve this question edited Aug 26, 2019 at 19:15 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Jun 14, 2011 at 14:35 LeemLeem 18.3k39 gold badges112 silver badges164 bronze badges4 Answers
Reset to default 16I would wrap the table with a div
<body>
<div style="overflow:scroll;height:80px;width:100%;overflow:auto">
<table width="800" border="0" class="my-table">
<tr> </tr>
</table>
</div>
</body>
The quick and easy answer is CSS overflow:scroll;
on the parent element.
However, if you're trying to jazz up your tables, I'd suggest going one step further, and use a JQuery plugin like Fixed Table Header.
The nice thing about this is that it means you can scroll the table body while keeping the header in place, which makes it much easier to read when you've got large amounts of data.
You might also want a script that allows your users to click in the header and sort the table by different columns. In that case, FlexiGrid might be an even better option.
From a UI standpoint, it's going to be easier to interact with a long table if it's paged, not scrolling. Scrolling can cause some behaviors that make it difficult for users with disabilities to interact. It's easy to click a next page link, not always so easy to scroll.
I attack table problems using a grid, and my grid of choice is DataTables. It gives you paging, filtering, sorting, ordering, and ajax loading of content along with the opportunity to scroll with a fixed header if you are determined to go this route. You can even setup a download to excel, pdf, printer, etc and style on the fly with just a couple of additions. All can be setup with a few simple lines of code. By far, it's the best, quickest trick I use to make complex data quickly available to my users.
if you want to see scroll only x position (horizantal) you can use style="overflow-x:scroll"