Hiding entire table resizes it - javascript

Having an HTML page with a simple table and js code to do show / hide on it:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>title</title>
<script type="text/javascript">
function showErrorSteps()
{
var el = document.getElementById("t1");
if(el.style.display=="none")
{
el.style.display="block";
}
else
{
el.style.display="none";
}
}
</script>
</head>
<body>
<br />
<span onclick="showErrorSteps()">[click]</span>
<br />
<br />
<table id="t1" border="1" width="100%" style="table-layout: fixed">
<tr>
<td>s</td>
<td>d</td>
<td>a</td>
</tr>
</table>
</body>
</html>
What happens is that on Mozilla the table gets resized after you click twice(even with the table-layout: fixed css). IE works fine.

Tables shouldn't be set to display: block. Table rows and cells shouldn't either. They have different display values. My advice? Don't do it this way. Use a class:
.hidden {
display: none;
}
and dynamically add it and remove it from the table to avoid problems of setting the right display type on an element that you show.
Edit: To clarify the comment as to why do it this way and what's going on. Try this:
<table>
<tr>
<td>Cell 1</td>
<td style="display: block;">Cell 2</td>
</tr>
</table
It will (or should) screw up your table layout. Why because a <td> element, by default, has display: table-cell not block. Tables are the same. They have display: table.
Unsetting CSS attributes is... problematic.
Thus you are best off using classes to set and unset attributes. It's easier to change (the class resides in a CSS file and isn't code), avoids problems like setting the value back to the correct original value and generally provides a cleaner solution, especially when used with a library like jQuery. In jQuery, you can do:
$("table").toggleClass("hidden");
Done.
Or you can use addClass() and removeClass() if that's more appropriate. For example:
<input type="button" id="hide" value="Hide Table">
...
<table id="mytable">
...
and
$(function() {
$("#hide").click(function() {
if ($("#mytable").is(".hidden")) {
$("#hide").val("Hide Table");
$("#mytable").removeClass("hidden");
} else {
$("#hide").val("Show Table");
$("#mytable").addClass("hidden");
}
});
});
And there you have a robust, succinct and easy-to-understand solution (once you get your head around the jQuery syntax, which doesn't take that long).
Messing about with Javascript directly is so 2002. :-)

This is not a direct answer to your question, but a serious recommendation. I have recently discovered the joys of JQuery. All this kind of stuff can be done effortlessly and there is extensive online examples and references available.
If you haven’t got time to get into it now then I’m sure someone will offer a solution here, but I would recommend anyone who does anything beyond the most cursory JavaScript DOM manipulation to consider JQuery (or a similar framework).
JQuery offers browser independent Hide(), Show() and Toggle() methods. Here’s one of my favourite references.

This might be because you set the style.display to "block". Try to set it to "". You should also set the table width using CSS. (width: 100%;)

Related

How do I left align the one table and right align the other table that uses the same class?

Initially, I can't change the class name for instance:
<table class="firsttable"> This is the firstable that must be left align
<tbody>
<tr>
<td>
<table class="firsttable"> And this is the second table that must be center align
<tbody>
<tr>
<td>
How do I code it in css?
You can use below CSS, It works as intended
<style>
.firsttable:nth-child(1) { text-align:left; }
.firsttable:nth-child(2) { text-align:right; }
</style>
It's CSS3 though.
First table CSS selector:
.firsttable:first-of-type
If second table is last:
.firsttable:last-of-type
Otherwise, second table CSS selector is this:
.firsttable:nth-of-type(2)
Use ids:
HTML:
<table class="firsttable" id="table1">
...
</table>
<table class="firsttable" id="table2">
...
CSS:
#table1 {
text-align:left;
}
#table2 {
text-align:right;
}
Does this work?
In this case it is better or from my sense I always make it by adding an inline style in the table and I think their no need to create any other style for it in CSS.
<table class="firsttable" style="text-align:left"> This is the firstable that must be left align
And the second table may be another inline style of text-align:center
<table class="firsttable" style="text-align:center"> And this is the second table that must be center align
It is the best approach. But you can also can create some different id here or
.firsttable:nth-child(1) { text-align:left; }
.firsttable:nth-child(2) { text-align:center; }
Problem in this is if you use it then if another table appear in this web page area than it will arise a problem. So it is better to use inline style in this case.
If you want to move the table to the left and one to the right add the following to the appropreate table.
For the align right. Add this at the opening tag <table>.
style="float:right;"
For the algin left add the same code to the exactly same place but change the "left" to "right" depending on where you want it to be.
To aling text do as the other people have seggested.
J. Carter :)

Closest H3 above a Table with Class

I try to select the heading of a table where a class is set. In words: look for an element with the class .red and start looking for an h3 element ABOVE this element.
$('.xred').closest('table').addClass('test').prev('h3').addClass('test');
.test { background-color: green; }
.xred { background-color: red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3>Goal</h3>
<table>
<tr>
<td>Blabla</td>
<td class="xred">Red</td>
</tr>
</table>
Works fine, as long I don't put anything in between the table and the H3.
How can I achieve that it will also work when other elements are in between?
$('.xred').closest('table').addClass('test').prev('h3').addClass('test');
.test { background-color: green; }
.xred { background-color: red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3>Goal</h3>
<div>The Problem DIV</div>
<table>
<tr>
<td>Blabla</td>
<td class="xred">Red</td>
</tr>
</table>
Thanks a lot!
I'm not entirely sure why you'd do something so inefficient. Do you not have direct access to the html?
If you do, it might be way easier to do something like this?
HTML:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapperdiv>
<h3>Goal</h3>
<div>The Problem DIV</div>
<table>
<tr>
<td>Blabla</td>
<td class="xred">Red</td>
</tr>
</table>
</div>
Script:
$('.xred').closest('table').parent().find("h3").addClass("test");
Having a wrapper makes it all easier, yeah?
Then again, I would personally never build up anything that I would need to do so much DOM searching. It's wildly inefficient. I'd recommend a good html structure with wrapper divs which you can store in a variable like so:
var $wrapperDiv = $('.wrapperDiv');
$('h3', $wrapperDiv).addClass("test");
But, that's just me...
but why?
Well, whenever you use functions like prevAll and closest, you're traversing the entire DOM. It makes for quite the CPU usage and often laggy responses on slower devices like cellphones. It also drains their batteries quicker.
Besides this, when stacking function on top of function, you'll notice laggy responses on any device.
The best practice is to store any static wrapper within a variable once, so you don't have to traverse the DOM constantly. Then, using as little selectors as possible to get what you need, because again; every selecting function will once more traverse the DOM. It's just CPU intensive.
You need to use .prevAll() that select preceding previous element that matched. .prev() select previous element if matched parameter.
$('.xred').closest('table').addClass('test').prevAll('h3').addClass('test');
.test { background-color: green }
.xred { background-color: red }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3>Goal</h3>
<div>The Problem DIV</div>
<table>
<tr>
<td>Blabla</td>
<td class="xred">Red</td>
</tr>
</table>

ol outside of table element

I just try a table with <ol> as list elements with which it is possible to insert new table row.
<table>
<thead>
<tr>
<th>head</th>
<th>head</th>
<th>head</th>
<th>head</th>
</tr>
</thead>
<tbody>
<ol id="list">
<li><tr><td>row</td><td>row</td><td>row</td></tr></li>
</ol>
</tbody>
However, I have the problem that the element appear outside of my tables. When I add dynamically content via .append(), the formatting is not taken some elements gets removed.
Jsfiddle example
I want to use this solution for counting currently positions in an "container list".
I got a similar function like the example below for counting my lists, that's working great but the insert into the table does not work properly.
countinglists example: Nested ordered lists
Maybe its possible to achieve that counting syntax in a table without the <ol>? or is there any <ol> equivalent?
You need to do some reading on basic HTML: http://www.w3schools.com/html/html_tables.asp
Here is how it should look...
<table>
<thead>
<tr>
<th>head</th>
<th>head</th>
<th>head</th>
<th>head</th>
</tr>
</thead>
<tbody id="list">
<tr>
<td>row</td><td>row</td><td>row</td><td>row</td>
</tr>
<tr>
<td>row</td><td>row</td><td>row</td><td>row</td>
</tr>
<tr>
<td>row</td><td>row</td><td>row</td><td>row</td>
</tr>
</tbody>
In theory, you should be able to use CSS counters.
table {
counter-reset: myTableCounter;
}
thead th:first-child:before {
display: table-cell;
content: "";
}
tbody td:first-child:before {
display: table-cell;
counter-increment: myTableCounter;
content: counter(myTableCounter);
}
However, when I attempted to do that I found there were issues with display: table-cell generated content.
You may have to look at adding additional elements to the table to generate the content inside the first cell of each row.
My question is: what are you trying to achieve? Is this an exercise just to see how much can you stretch the HTML?
For your jsfiddle, the action associated to the click removes some of the HTML tags (at least on my browser) resulting in a <li>rowrowrow</li>, so you end up having a rather odd formatted-table. My renderer takes all <li> tags added by clicking as the content of a row; if you have only <li> tags, the dom parser will likely wrap them into a <ul> (it does on mine).
IMHO you don't need to use the ol to be able to count stuff. You can do it in jquery afaik. If you insist to use lists, then you probably need to style them and use e.g. divs inside (styled too). Emulating a table via a list and divs is madness imho :)
Update - for the hierarchical table
My idea would be to have something similar to this jsfiddle. I basically styled in the .sub and the .main classes. However, things get a bit more complex is you need to add some extra columns. In this case, you'd need something like a treetable.

Slide Down tabe row smoothly and Slowly

I want to Slide Down tabe row smoothly and Slowly.
The problem now is, it is instantly appearing and hiding, how can i make it smooth.
**Please check this fiddle:**
http://jsfiddle.net/5WT9g/2/
HTML:
Show Content
<br><br><br>
<table width="400" border="1">
<tr id="mainContent" style="display:none;">
<td> THIS IS MAIN CONTENT </td>
</tr>
</table>
JS:
$('#showContent').click(function ()
{
$('#mainContent').slideToggle('slow');
});
I think you mean to use slideToggle() and not toggleslide().
However, the animation still won't work smoothly for table cells. It will work slightly more smoothly if you set a height for the tr. For a completely smooth animation, I recommend using divs instead.
Here is a modified version of your code that has a sort of smooth animation with tables:
http://jsfiddle.net/TS77v/1/
As you can see, you will have to do the animation on the td, not the tr. I also had to set the height of the td for this to work, otherwise it will just appear and disappear.
Why doesn't the animation work properly on tables?
From "Learning jQuery" by Chaffer and Swedberg
Table rows present particular obstacles to animation, since browsers
use different values (table-row and block) for their visible display
property. The .hide() and .show() methods, without animation, are
always safe to use with table rows. As of jQuery version 1.1.3,
.fadeIn() and .fadeOut() can be used as well.
For your reference: https://stackoverflow.com/a/920480/3016565
It would be much easier with divs but if you prefer/need tables then tables it is. I'd do it by putting a div inside the table cell ja use the slideToggle to it. Yes, it still adds the div there but atleast you got the table structure. To make it work you need to do just a minor change to your HTML code, JS stays the same:
HTML
Show Content
<br><br><br>
<table width="400" border="1">
<tr>
<td><div style="display: none;" id="mainContent">THIS IS MAIN CONTENT</div></td>
</tr>
</table>
And a fiddle: http://jsfiddle.net/32HR9/1/
AFTER GETTING THE INFORMATION THAT YOU CAN'T CHANGE THE HTML
I assume you can edit the javascript? You haven't said anything about that. This trick isn't neat but there's no need to change the HTML and it gets the job done:
So, with jQuery
Make the tr visible.
Wrap the content of the tr in a div.
Hide the div.
Make the slideToggle work with the created div.
with code
$('#mainContent').css('display', 'table-row');
$('#mainContent > td').wrapInner("<div class='hideshow'></div>");
$('.hideshow').css('display', 'none');
$('#showContent').click(function (){
$('.hideshow').slideToggle('slow');
});
and a fiddle: http://jsfiddle.net/5E5VS/7/
Try this.
Show Content
<table id="mainContent" width="400" border="1">
<tr>
<td> <p style="display: none"> THIS IS MAIN CONTENT</p></td>
</tr>
</table>
$("#showContent").click(function () {
$('#mainContent').find("p").slideToggle();
});

HTML table with fixed headers and a fixed column?

Is there a CSS/JavaScript technique to display a long HTML table such that the column headers stay fixed on-screen and the first coloumn stay fixed and scroll with the data.
I want to be able to scroll through the contents of the table, but to always be able to see the column headers at the top and the first column on the left.
If there is a jQuery plugin that would be great! If it helps the only browser I care about is Firefox.
Working example of link posted by pranav:
http://jsbin.com/nolanole/1/edit?html,js,output
FYI: Tested in IE 6, 7, & 8 (compatibility mode on or off), FF 3 & 3.5, Chrome 2. Not screen-reader-friendly (headers aren't part of content table).
EDIT 5/5/14: moved example to jsBin. This is old, but amazingly still works in current Chrome, IE, and Firefox (though IE and Firefox might require some adjustments to row heights).
The jQuery DataTables plug-in is one excellent way to achieve excel-like fixed column(s) and headers.
Note the examples section of the site and the "extras".
http://datatables.net/examples/
http://datatables.net/extras/
The "Extras" section has tools for fixed columns and fixed headers.
Fixed Columns
http://datatables.net/extras/fixedcolumns/
(I believe the example on this page is the one most appropriate for your question.)
Fixed Header
http://datatables.net/extras/fixedheader/
(Includes an example with a full page spreadsheet style layout: http://datatables.net/release-datatables/extras/FixedHeader/top_bottom_left_right.html)
I see this, although an old question, is a pretty good place to plug my own script:
http://code.google.com/p/js-scroll-table-header/
It just works with no configuration and is really easy to setup.
If what you want is to have the headers stay put while the data in the table scrolls vertically, you should implement a <tbody> styled with "overflow-y: auto" like this:
<table>
<thead>
<tr>
<th>Header1</th>
. . .
</tr>
</thead>
<tbody style="height: 300px; overflow-y: auto">
<tr>
. . .
</tr>
. . .
</tbody>
</table>
If the <tbody> content grows taller than the desired height, it will start scrolling. However, the headers will stay fixed at the top regardless of the scroll position.
In this answer there is also the best answer I found to your question:
HTML table with fixed headers?
and based on pure CSS.
I have created something which has fixed header, fixed footer, fixed left column and also fixed right column. This only works fine in IE. As most of the users are still using IE this can be helpful. Please find the code here in Scrollable Table. Please let me your suggestions.
Meanwhile I am working to fix columns in other browser. I will keep you posted. :-)
<script>
$(document).ready(function () {
$("#GridHeader table").html($('#<%= GridView1.ClientID %>').html());
$("#GridHeader table tbody .rows").remove();
$('#<%= GridView1.ClientID %> tr:first th').hide();
});
</script>
<div id="GridHeader">
<table></table>
</div>
<div style="overflow: auto; height:400px;">
<asp:GridView ID="GridView1" runat="server" />
</div>
Not quite perfect, but it got me closer than some of the top answers here.
Two different tables, one with the header, and the other, wrapped with a div with the content
<table>
<thead>
<tr><th>Stuff</th><th>Second Stuff</th></tr>
</thead>
</table>
<div style="height: 600px; overflow: auto;">
<table>
<tbody>
//Table
</tbody>
</table>
</div>
I know you can do it for MSIE and this limited example seems to work for firefox (not sure how extensible the technique is).
The first column has a scrollbar on the cell right below the headers
<table>
<thead>
<th> Header 1</th>
<th> Header 2</th>
<th> Header 3</th>
</thead>
<tbody>
<tr>
<td>
<div style="width: 50; height:30; overflow-y: scroll">
Tklasdjf alksjf asjdfk jsadfl kajsdl fjasdk fljsaldk
fjlksa djflkasjdflkjsadlkf jsakldjfasdjfklasjdflkjasdlkfjaslkdfjasdf
</div>
</td>
<td>
Hello world
</td>
<td> Hello world2
</tr>
</tbody>
</table>
YUI DataTable
I don't know if YUI DT has this feature but I won't be surprised if it does.
Here is a good jQuery plugin, working in all browsers!
You have a fixed header table without fixing its width.
Check it: https://github.com/benjaminleouzon/tablefixedheader
Disclaimer: I am the author of the plugin.

Categories

Resources