jQuery prepend tr after tr heading - html

I have the following HTML table.
<table id="table">
<tbody>
<tr>
<th>City</th>
</tr>
<tr>
<td>Madrid</td>
</tr>
<tr class="show-on-top">
<td data-order="3">London</td>
</tr>
<tr>
<td>Berlin</td>
</tr>
<tr>
<td data-order="1">Paris</td>
</tr>
<tr class="show-on-top">
<td data-order="2">Rome</td>
</tr>
</tbody>
London, Paris and Rome have a class named show-on-top. As the name suggests I want to move all tr with show-on-top on top of the table. I can do that with the following code.
$("#table tbody").prepend($('.show-on-top'));
But the problem is that London, Paris and Rome are shown before <th> (the "City" heading). Of course I want to place show-on-top rows on top of the table but after heading (first row). So I came up with the following idea.
$("#table tbody tr:eq(1)").prepend($('.show-on-top'));
I'm almost there. All my show-on-top are placed after heading but they're nested inside a tr like follows.
<table id="table">
<tbody>
<tr>
<th>City</th>
</tr>
<tr>
<tr class="show-on-top">
<td data-order="3">London</td>
</tr>
<tr>
<td data-order="1">Paris</td>
</tr>
<tr class="show-on-top">
<td data-order="2">Rome</td>
</tr>
</tr>
<tr>
<td>Madrid</td>
</tr>
<tr>
<td>Berlin</td>
</tr>
</tbody>
I can't understand how the heck all my rows are nested inside a tr. I'm tried with hundreds of combinations of parent, after, prepend, append with no success.
Bonus question. show-on-top rows have data-order attribute that represents the order I want to sort th rows in question (Paris > Rome > London). I used sort.data('order') but nothing happens. I can't even see anything in console.log().
Thank you for your time.

One thing you can do is move the heading rows out of <tbody> into <thead>
$("#table tbody").prepend($('.show-on-top'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="table">
<thead>
<tr>
<th>City</th>
</tr>
</thead>
<tbody>
<tr>
<td>Madrid</td>
</tr>
<tr class="show-on-top">
<td data-order="3">London</td>
</tr>
<tr>
<td>Berlin</td>
</tr>
<tr>
<td data-order="1">Paris</td>
</tr>
<tr class="show-on-top">
<td data-order="2">Rome</td>
</tr>
</tbody>
</table>
Another solution is to use .after() to put the rows after the last row containing th.
$("#table tbody tr:has(th):last").after($(".show-on-top"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="table">
<tbody>
<tr>
<th>City</th>
</tr>
<tr>
<td>Madrid</td>
</tr>
<tr class="show-on-top">
<td data-order="3">London</td>
</tr>
<tr>
<td>Berlin</td>
</tr>
<tr>
<td data-order="1">Paris</td>
</tr>
<tr class="show-on-top">
<td data-order="2">Rome</td>
</tr>
</tbody>
</table>

You could use insertAfter() and :first selector
$(".show-on-top").insertAfter('#table tbody tr:first');
.show-on-top{ color:red}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="table">
<tbody>
<tr>
<th>City</th>
</tr>
<tr>
<td>Madrid</td>
</tr>
<tr class="show-on-top">
<td data-order="3">London</td>
</tr>
<tr>
<td>Berlin</td>
</tr>
<tr>
<td data-order="1">Paris</td>
</tr>
<tr class="show-on-top">
<td data-order="2">Rome</td>
</tr>
</tbody>
</table>

Related

How to center the table data elements themselves in the row?

I have a table with the following layout for a time-sheet like application:
<table width="100%" border="1px">
<tr>
<th colspan="11" align="center">Function</th>
</tr>
<tr>
<td align="center">(01)</td> <!-- 7 more TD's after this -->
</tr>
<tr>
<th colspan="11" align="center">Products</th>
</tr>
<tr>
<td align="center">(02)</td> <!-- 10 more TD's after this -->
</tr>
<tr>
<th colspan="11" align="center">Other Codes</th>
</tr>
<tr>
<td align="center">V</td> <!-- 6 more TD's after this -->
</tr>
</table>
Each row has a different amount of table data elements. I'm trying to get the table data elements themselves centered in the entire row, not just the text inside the element itself but I cannot seem to find out how to do this.
Anything I've googled just gives me various things related to the align and valign attributes for the content of the cell itself, rather than centering the cells in the row.
I've tried messing around with the margin and border sizes but it doesn't seem to make any difference as recommended in some of these threads:
https://stackoverflow.com/a/8649701/1189566
How to align td elements in center
and a few other forums but haven't had any success.
If you must use table elements, you could do a table within the table data and get something like this:
http://jsfiddle.net/ykevsws0/
<table width="100%" border="1px">
<tr>
<th colspan="11" align="center">Function</th>
</tr>
<tr>
<td align="center">(01)</td>
<!-- 7 more TD's after this -->
</tr>
<tr>
<th colspan="11" align="center">Products</th>
</tr>
<tr>
<td align="center">(02)</td>
<!-- 10 more TD's after this -->
</tr>
<tr>
<th colspan="11" align="center">Other Codes</th>
</tr>
<tr>
<td align="center">
<table>
<tr>
<td>V</td>
</tr>
<tr>
<td>V</td>
</tr>
<tr>
<td>V</td>
</tr>
<tr>
<td>V</td>
</tr>
<tr>
<td>V</td>
</tr>
</table>
</td>
</tr>
</table>
Another way to do it if for some reason you cannot manipulate the markup is use css to force table data to display:block, like this (might want to specify a css class or something, applying this style to td across the board will cause problems):
http://jsfiddle.net/jagsyguv/
<table width="100%" border="1px">
<tr>
<th colspan="11" align="center">Function</th>
</tr>
<tr>
<td align="center">(01)</td>
<!-- 7 more TD's after this -->
</tr>
<tr>
<th colspan="11" align="center">Products</th>
</tr>
<tr>
<td align="center">(02)</td>
<!-- 10 more TD's after this -->
</tr>
<tr>
<th colspan="11" align="center">Other Codes</th>
</tr>
<tr>
<td>
V
</td>
<td>
V
</td >
<td>
V
</td>
<td>
V
</td>
</tr>
</table>
You need nested table for achieving this result
<table width="100%" border="1px">
<tr>
<th colspan="11" align="center">Function</th>
</tr>
<tr>
<td colspan="11" align="center">
<table border="1px">
<tr>
<td align="center">(01)</td>
<td align="center">(01)</td>
<td align="center">(01)</td>
<td align="center">(01)</td>
<td align="center">(01)</td>
<td align="center">(01)</td>
<td align="center">(01)</td>
<td align="center">(01)</td>
<!-- 7 more TD's after this -->
</tr>
</table>
</td>
</tr>
<tr>
<th colspan="11" align="center">Products</th>
</tr>
<tr>
<td colspan="11" align="center">
<table border="1px">
<tr>
<td align="center">(02)</td>
<td align="center">(02)</td>
<td align="center">(02)</td>
<td align="center">(02)</td>
<td align="center">(02)</td>
<td align="center">(02)</td>
<td align="center">(02)</td>
<td align="center">(02)</td>
<td align="center">(02)</td>
<td align="center">(02)</td>
<td align="center">(02)</td>
<!-- 10 more TD's after this -->
</tr>
</table>
</td>
</tr>
<tr>
<th colspan="11" align="center">Other Codes</th>
</tr>
<tr>
<td colspan="11" align="center">
<table border="1px">
<tr>
<td>V</td>
</tr>
<tr>
<td>V</td>
</tr>
<tr>
<td>V</td>
</tr>
<tr>
<td>V</td>
</tr>
<tr>
<td>V</td>
</tr>
</table>
</td>
</tr>
</table>
Alternatively you could consider using css combined with div tags to generate the table / grid system
You should use a div. A div acts differently than a table, thus you can place it, with some style options, where you want in the page.
You can also build this table with just divs. I prefer using divs rather than tables, because they are easier to handle when you get used to them.
Try searching more info about divs and their usage.
This is a simple example:
<div style="margin:auto; width:60%; padding:10px;">
<table style width="100%" border="1px">
<tr>
<th colspan="11" align="center">Function</th>
</tr>
<tr>
<td align="center">(01)</td> <!-- 7 more TD's after this -->
</tr>
<tr>
<th colspan="11" align="center">Products</th>
</tr>
<tr>
<td align="center">(02)</td> <!-- 10 more TD's after this -->
</tr>
<tr>
<th colspan="11" align="center">Other Codes</th>
</tr>
<tr>
<td align="center">V</td> <!-- 6 more TD's after this -->
</tr>
</table>
</div>
The easiest way to center elements in a <td> cell is the following. (This works for <img>, <video> and most other elements):
<table width="100%" border="0" cellspacing="0" cellpadding="100%">
<tr>
<td>First Element</td>
<td>Second Element</td>
</tr>
</table>

How do I repeat table headers per record on mobile (create a stacked view on mobile)

I'm new to bootstrap. I'm using bootstrap 3. I'm trying to figure out how I can create the following so that it looks like a standard table when not viewing it on a mobile device, and in a more stacked fashion when on mobile.
This is what I'm wanting to do:
You need to your code mention in two different table. & then use css class .hidden-xs & .visible-xs for show & hide in responsive and non-responsive view.
Example following code. also, check it here in jsfiddle
<!--Non-Mobile-->
<table class="table table-bordered table-hover hidden-xs">
<thead>
<tr>
<td>DOMAIN</td>
<td>USER</td>
<td>ACCT</td>
<td>STATUS</td>
<td>ACTIONS</td>
</tr>
</thead>
<tbody>
<tr>
<td>domain1.co</td>
<td>user1</td>
<td>123456</td>
<td>active</td>
<td>edit</td>
</tr>
<tr>
<td>domain2.co</td>
<td>user1</td>
<td>654321</td>
<td>active</td>
<td>edit</td>
</tr>
</tbody>
</table>
<!--Responsive-->
<table class="table table-bordered table-hover visible-xs">
<tr>
<td>DOMAIN</td>
</tr>
<tr>
<td>domain1.co</td>
</tr>
<tr>
<td>USER</td>
</tr>
<tr>
<td>user1</td>
</tr>
<tr>
<td>ACCT</td>
</tr>
<tr>
<td>123456</td>
</tr>
<tr>
<td>STATUS</td>
</tr>
<tr>
<td>active</td>
</tr>
<tr>
<td>ACTIONS</td>
</tr>
<tr>
<td>edit</td>
</tr>
</table>
<table class="table table-bordered table-hover visible-xs">
<tr>
<td>DOMAIN</td>
</tr>
<tr>
<td>domain2.co</td>
</tr>
<tr>
<td>USER</td>
</tr>
<tr>
<td>user1</td>
</tr>
<tr>
<td>ACCT</td>
</tr>
<tr>
<td>654321</td>
</tr>
<tr>
<td>STATUS</td>
</tr>
<tr>
<td>active</td>
</tr>
<tr>
<td>ACTIONS</td>
</tr>
<tr>
<td>edit</td>
</tr>
</table>
</div>

html css code to display multiple tables horizontally or in a single row

I have created a table which loops from a database. Now I need to display this multiple table in a single row. Currently the loop display tables in columns. Please can someone suggest how to write the markup, so that it generates and displays table data in a single row?
For example, if there are 10 tables loops from the database, then there should be 10 cells in a single table row. The table should not contain anything below that row. It's ok for a horizontal scrollbar to display in the event that the table is longer than the screen width.
<table width="100%" class="table table-bordered table-striped">
<table>
<thead>
<tr>
<th colspan="2">First Name</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2">James</td>
</tr>
<tr>
<td>The</td>
<td>Tick</td>
</tr>
</tbody>
</table>
Table inside table is not valid. You have to add table row then add all the tables under each table columns like below.
<table width="100%" class="table table-bordered table-striped">
<tr>
<td>
<table>
<thead>
<tr>
<th colspan="2">First Name</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2">James</td>
</tr>
<tr>
<td>The</td>
<td>Tick</td>
</tr>
</tbody>
</table>
</td>
<td>
Your Second Table Goes here..
</td>
<td>
Your Third Table Goes here and so on.
</td>
</tr>
</table>
If I understand you right, you want the table shown above to be repeated multiple times such and to appear side by side in a single row.
What I would do is add the CSS property display: inline-table (use a class) to the relevant tables.
See the following example:
.tablePanel {
width: 300%;
word-break: nowrap;
border: 1px dotted blue;
}
.tablePanel table {
border: 1px dashed gray;
display: inline-table;
width: 200px;
}
<div class="tablePanel">
<table>
<thead>
<tr>
<th colspan="2">First Name</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2">James</td>
</tr>
<tr>
<td>The</td>
<td>Tick</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th colspan="2">First Name</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2">James</td>
</tr>
<tr>
<td>The</td>
<td>Tick</td>
</tr>
</tbody>
</table> <table>
<thead>
<tr>
<th colspan="2">First Name</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2">James</td>
</tr>
<tr>
<td>The</td>
<td>Tick</td>
</tr>
</tbody>
</table> <table>
<thead>
<tr>
<th colspan="2">First Name</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2">James</td>
</tr>
<tr>
<td>The</td>
<td>Tick</td>
</tr>
</tbody>
</table>
</div>

How to make one <td> span both columns in a two column table?

How can I create a table like the above example in HTML and CSS.
I've tried the following:
<table>
<tr>
<td style="width:50%">TEXT</td>
<td style="width:50%">TEXT</td>
</tr>
<tr>
<td style="width:100%">TEXT</td>
</tr>
but it won't work. Can anyone help?
You should use colspan for your second row. Like this :
<table>
<tr>
<td style="width:50%">TEXT</td>
<td style="width:50%">TEXT</td>
</tr>
<tr>
<td colspan="2" style="width:100%">TEXT</td>
</tr>
...
</table>
For learn -> HTML Colspan
<td>s have a colspan attribute that determine how many columns it should span over. You example has 2 columns to span, so your cleaned up code would look like this:
<table>
<tr>
<td width="50%"></td>
<td width="50%"></td>
</tr>
<tr>
<td width="50%"></td>
<td width="50%"></td>
</tr>
<tr>
<!-- The important part is here -->
<td colspan="2">This will have 100% width by default</td>
</tr>
<tr>
<td width="50%"></td>
<td width="50%"></td>
</tr>
<tr>
<td width="50%"></td>
<td width="50%"></td>
</tr>
</table>
<table border="1">
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
</tr>
<tr>
<td colspan="2">Cell 3 (Two columns)</td>
</tr>
</table>
colspan will help you. Link to more info.

Scrolling HTML Nested Tables

How can I make a table which has sub/nested tables scrollable if given a max height? I do not want the thead to scroll with it. What I'm really looking for is some valid tag that I can wrap my s in, or some other markup that will work just as well as the this:
<table>
<thead>
<tr>
<th>Column1</th>
<th>Column2</th>
</tr>
</thead>
<!-- Insert valid tag here to wrap <tbody>s -->
<tbody>
<tr>
<td>Val1</td>
<td>Val2</td>
</tr>
<tr class="sub-table">
<td colspan="2">
<table>
<thead>
<tr>
<th>Sub Column1</th>
<th>Sub Column2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Sub Val1</td>
<td>Sub Val2</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Val1</td>
<td>Val2</td>
</tr>
<tr class="sub-table">
<td colspan="2">
<table>
<thead>
<tr>
<th>Sub Column1</th>
<th>Sub Column2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Sub Val1</td>
<td>Sub Val2</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
<!-- Insert valid close tag here -->
</table>
If I understand your question correctly, maybe try something like this: http://jsfiddle.net/pPuXL/1/
You need to display the TBODY elements as "block" or "inline-block" in order for overflow to work on it, as in the CSS example I posted.