I'm working on a table which, depending on user data, may be too wide to fit on the display, so we made it "overflow: auto;".
I want to be able to automatically scroll the table so that a certain named cell -- say: ('cell_' + row + '_' + col) is visible.
How do I get this to happen? I've seen a couple of suggestions here involving jQuery but couldn't get them to do anything.
I don't need smooth animation, in fact I'd rather there wasn't any as I find it's a distraction for the users.
Check out this fiddle:
JavaScript
var container = $(".narrow");
container.scrollTop(0);
container.scrollLeft(0);
container.scrollTop($("#cell_3_3").offset().top - container.offset().top);
container.scrollLeft($("#cell_3_3").offset().left - container.offset().left);
HTML
<div class="narrow">
<table>
<tr>
<td id="cell_1_1">1. Content ...</td>
.....
</tr>
.....
</table>
</div>
CSS
.narrow {
max-width: 100px;
max-height: 200px;
overflow: auto;
}
UPDATE 1
New fiddle to demonstrate better. Looping through all cells.
Related
I am using AngularJS to generate a row of Material buttons in an HTML table. The selected button is highlighted.
<tr>
<td ng-click="pbmain.selectWC(WC.WCName)" ng-repeat="WC in pbmain.WCList">
md-button(ng-class="WC.WCName == pbmain.selectedWCName ? 'md-raised md-primary' : 'md-raised'") {{WC.WCName}}
</td>
</tr>
The number of buttons and text on the buttons can be quite long and the row is often 2-3 times the width of the browser window. For the moment, I scroll across but I am looking for a more user-friendly solution.
I would like to be able to split the row automatically, according to the page width - something like the example below.
You can achieve this by applying proper CSS to table and button elements. No need to split a row into multiple rows.
Example:
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span><span>TEST</span>
</td>
</tr>
CSS
table {
table-layout: fixed;
width: 100%
}
span {
border: 1px solid black;
margin: 10px;
width: 100px;
display: inline-block;
}
Have a look at this fiddle.
Well, I was searching by splitting rows into multiple rows and I got here.
It seems that your problem could be better solver by using display: inline-block; for every button.
However, if you really need a table for other purpose check at this next approach.
It would redrawn your linear tabular long table into a multilined table by only using css.
Than you can play around with your screen size.
I have no idea how to accomplish this easily within a table. Maybe you could make some pre-work on your array to make it two-dimensional and then ng-repeat tr and td tags as well.
My solution would be a clean grid system with Bootstrap.
With this you can achieve a fully responsive screen. If you do not need to stick with a table system, then you should consider this.
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.
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.
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.
kinda hard to explain, but let me try:
I'd like to put a table inside a container . this container has a fixed height, say 500px, and the table is rather long, say 2100 and each row's height can be different.
When the table is created, all the rows are invisible, now I want to do a calculation based on container's height to find out how many rows should appear inside the container. it could be first 15 rows, or first 17 rows (because some row's height is bigger).
After I do that, I let those rows stay there for some time, and hide them again, and do another calculation to fetch next page, and so on... Now the hard part it how do I do the calculation using jquery?
You can get the heights computed by the browser by using jQuery.innerHeight and jQuery.outerHeight functions. So you can first get the computed height of the container. Then you can iterate through rows and add their computed height until the sum is bigger than the container's computed height and so on..
Hope this helps.
This doesn't tackle the "stay for a while and fetch next set of rows after a while" part, but here's a simple calculation for heights that shows the appropriate rows.
http://jsfiddle.net/yvAtW/
JS
allowedHeight = $("div").height();
actualHeight = 0;
$("tr").each(function(){
actualHeight += $(this).height();
if(actualHeight < allowedHeight) {
$(this).show();
}
});
HTML
<div>
<table>
<tr class="height0">
<td>Row</td>
</tr>
<tr class="height1">
<td>Row</td>
</tr>
<tr class="height2">
<td>Row</td>
</tr>
<tr class="height1">
<td>Row</td>
</tr>
<tr class="height0">
<td>Row</td>
</tr>
</table>
</div>
CSS
div{
height:100px; /* fixed height container */
overflow:hidden; /* Ensures no overflowing content if JS doesn't work */
padding:10px 15px;
background:#777;
}
table{
width:100%;
}
tr{
display:none; /* all hidden by default */
}
/* different height trs */
.height0{
height:20px;
background:#900;
}
.height1{
height:30px;
background:#090;
}
.height2{
height:40px;
background:#009;
}
Iterate through the rows summing their height. Once you get over 500, take the set of rows except for the last row. Display those. Keep track of where you are at with some sort of marker, maybe a variable or a data- annotation.
perhaps you can do something like this:
fits = true;
while(fits){
$(table).append('<tr>...</tr>');
if($(table).css('height') >= $('#container').css('height'))
fits = false;
}