Enabling vertical scroll on HTML tables - javascript

I'm creating a table and populating it using javascript. I want to add a vertical scroll bar to the table, but on the data only, i.e. I want the table header to remain fixed, i.e. not be included in the scroll.
I want to use only javascript, CSS and valid HTML5 elements. At this stage I want to exclude jQuery. There have been many discussions on this topic, usually including horizontal scroll bars. I'm only interested in vertical scrolling and to me it is essential that the header columns must be aligned with the table data when scrolled. The discussions and my attempts to achieve this, lead me to believe this is impossible. If this is not the case can someone tell me how this is done?
So far I have made a number of attempts. Adding
table {
display: block;
height: 300px;
overflow: scroll;
}
causes vertical scrolling of the whole table, including the header and data, but the headers and data are aligned. Adding
thead {
display: block;
}
tbody {
display: block;
height: 300px;
overflow: scroll;
}
results in the header not scrolling, but the headers and data columns are no longer aligned. What appears to be happening is that the width of the header columns no longer takes account of the data columns.
I've tried numerous other approaches without success. I've had partial success by adding the same width value to header and data field columns based on the maximum field size, in characters, of the header and data values. With this approach I can get the header to align with the data for most columns. However, I don't regard this as an adequate solution, as it is in general not possible, or at least very difficult, to determine a value in em, px or mm, that will hold n characters exactly.
In the discussions I've seen, it seems that what I want to achieve is possible using jQuery. I've not yet embraced jQuery, but if it can easily enable what I want, it may be the time to do so. Can anyone confirm if what I want is possible in jQuery?
I find it hard to believe that what I want to do is not easily possible, as I would regard it as essential for any adequate handling of tables.

Maybe not the answer you're looking for, but a rather simple solution would be to put all the scrollable rows and columns within a scrollable div, and keep the headers and footers outside that div.

If you are willing to set your columns at a fixed width, you can basically make two tables - one for the header and one for the body, and then just set scrolling on the body.
HTML:
<table class="header">
<thead>
<td>Header</td><td>Header</td><td>Header</td>
</thead>
</table>
<table class="body">
<tbody>
<tr><td>data</td><td>data</td><td>data</td></tr>
...
</tbody>
</table>
And your css:
td, th{
width:50px;
}
table.body{
display: block;
height: 300px;
width:200px;
overflow: scroll;
}
See this jsFiddle.

Related

Horizontally scrolling table with fixed first column

I want to create a table with a fixed first column so that when the user scrolls horizontally, the first column stays where it is, making it easier to interpret the data in the table. I gave the first column position: absolute but the issue with that is the <td> elements don't resize when the content takes up multiple lines. How do I fix this? I wouldn't mind a solution using javascript for this.
What I have so far: http://jsfiddle.net/Yw679/778/
Also, would it be possible to make the <thead> fixed also so that it doesn't go out of view when the user scrolls down but moves with the rest of the columns when the user scrolls horizontally?
I added a new column with class style-hidden for each td and th and some tricky css like this. I hope this will help:
.style-hidden{
width: 0;
opacity: 0;
display: block;
}
JSFiddle

Alternate to table to display data?

I am trying to display a table in HTML but it seems the table is unable to meet the specs. I would like to data to be in the same place on the screen otherwise it will cause all kinds of problems with the users as they mis-read the data. So I need fixed width columns. I also would like the column headers to remian fixed if the data exceeds the height of the windows because some users do use them.
I do not want to install any 3rd party controls if possible. The owner will not buy anything anyway. I don't mind something that downloads with the page.
I was thinking of trying to make a scrollable div with textboxes or labels but thought I should check to see if someone has already run into this issue and came up with a solution.
I asked here: How to make a scrolling table with dynamic data
but no one has a clue as to whats wrong.
Then I asked here: How to get a table to fit its parent container?
And the key here seems the column widths. But without the widths using the table seems pointless.
Asking this: Why don't my column headings line up?
Seems also to indicate that a table will never work.
Setting this:
display: table-row-group
Makes the column headings line up, but then the table can not scroll.
display: block
Makes the table scroll but the column headings are wrong.
Here's an example that works on its own. http://jsfiddle.net/kjzcv9g2/
<style type="text/css" media="screen">
table, th, td {border: solid;}
thead {
display: block;
color: #f00;
background: #eee;
height: 35px;
overflow-y: scroll;
}
tbody {
display: block;
height: 100px;
overflow-y: scroll;
}
th, td {
width: 10em;
font-weight: normal;
}
</style>
<table>
<thead>
<tr>
<th>foo</th>
<th>bar</th>
</tr>
</thead>
<tbody>
<tr>
<td>foo</td>
<td>bar</td>
</tr>
</tbody>
</table>
But getting this to work seems impossible.
Any suggestions on whether to use labels or textboxes to create my own table?
JSFIDDLE
If I understand the question correctly the above should work. Add a min-width to the overall table and you'll have fixed width columns below the min width, and the columns will fill the container about the min width.

Table sorter width 100% does not work if table is contained into others tables

I use tablesorter (https://github.com/Mottie/tablesorter), with widget scroller.
You can find examples here : http://mottie.github.io/tablesorter/docs/example-widget-scroller.html
My problem is when tablesorter is contained into other tables like
<table>
<tr>
<td>
table sorter table
</td>
</tr>
</table>
table sorter does not take 100% of the width... And the horizontal scrollbar does not appear automatically.
You can find a example here : http://jsfiddle.net/oqfq47fc/8/
Whereas if table sorter is not contained in other table, its works...
You can find a example here : http://jsfiddle.net/oqfq47fc/9/
Can you help me? I works in portal context and contents portlets are inside table, and it is not possible to remove nested tables.
I think it's just a matter of css, but I know no more...
EDIT
I need that table takes 100% of width like this picture :
And when I resize the browser and that the all table can not display in the available space, a horizontal scrollbar appear at the bottom of the table like this picture :
It works good in my first example.
But in my second example with the outer table, the table look like this picture :
And when I resize the browser, the horizontal scroll bar does not appear on table but on window like this picture :
table{
display: block;
max-width: 100%;
overflow-y: hidden;
overflow-x: scroll;
width: 100%;
}
table.data-table,table#tvar_1{
display: table;
}
html{
overflow-x: hidden;
}
.tablesorter-scroller-table{
width: 100% !important;
}
Fiddle
Adding this set of code is as close as I can get to reproducing what you want with strictly tables. You are using tables for layout and you really shouldn't be doing that. The best way to go is to use the div the way you are in the second iteration where it is correct. It obviously displays the correct and best way. Also, with what I had to do to these tables it is not nice.
Do your best to avoid tables where tables are not needed. This is tabular data so it is needed, but using a table to construct the page is not the right way to go.

JQuery Datatables columns overflow

I have problem with my datatable: it has to have a specific width, but also I want to display all its columns. The whole issue looks like this:
I found solutions Datatables Width Overflow For A Lot Of Columns and JQuery Datatables overflow, but I would rather use word breaking then have a scrollbar in my table.
So, I found another solution jQuery DataTable overflow and text-wrapping issues according to which i had to set:
table.display { table-layout:fixed; }
th, td { word-wrap:break-word; overflow:hidden; text-overflow: ellipsis; }
However, with this I had a problem with column width; all columns have same width and option "bAutoWidth: true" is ignored:
For example, there is an empty horizontal space in Price column, or after icons at last column. Moreover word-broken headers (th) seem very ugly, and it would look better if Code, Count, Price and Place columns were at their minimum size (their width matched width of header text).
I would like to specify (somehow) preferred width for each column, or after how many letters it should wrap. I found breakCellText plugin in this post jQuery DataTable overflow and text-wrapping issues. Nevertheless, this plugin is not working with my datatables 1.9.4.
I had the same problem. Here is my solution:
.dt-index {
column-width: auto !important;
}
Please use this style for your table.
I hope that will be your solution.
Greetz
Vegeta_77

Side Panel in CSS

I have a div called calendar that is inside a div called cal-container. The calendar has width:100% so currently it takes up the whole cal-container.
I need to add a side-panel div. This div will have a fixed width of 150 pixels. Thus, #calendar width should be #cal-container width - 150px. Is this possible with CSS or am I forced to use a table?
If it is possible, is there an example? I googled it but nothing like what I want came up.
The side-panel can be hidden and shown by click a button so adding padding will not work.
Here is an idea of what I mean:
The days part is #calendar, and the Unscheduled part is the side panel of 150px.
I tried floating the calendar left, and cloating the side panel right and giving it a width of 150px. But the idea is if I hide that div, the calendar should then take 100%.
Thanks
Like this, the blue would be side and calendar be the left, but calendar needs to take up the room side does not when hidden.
http://www.456bereastreet.com/lab/developing_with_web_standards/csslayout/2-col/finished.html
Result of float:
Got a working solution for you here.
The code to get this working basically hinges on the following structure:
<div class="sideBar">
...
</div>
<div class="tableWrapper">
<table>
...
</table>
</div>
Next, make sure the elements have these significant CSS properties:
.sideBar {
float: right;
}
.tableWrapper {
overflow: hidden;
}
table {
width: 100%;
}
What's happening here is that the .sideBar floats right, and takes up whatever space it needs to. Meanwhile, the .tableWrapper will take up whatever space is left by virtue of overflow: hidden. Finally, tell the table to take up 100% of its available width.
Click the button in the demo to see the table automatically resize.
All major browsers and IE10 support flexbox. Not supported < IE10.

Categories