I have a table with html as follows:
<div id="cart">
<table>
<thead>
<tr>
<th>ID</th>
<th>Product Name</th>
<th >Price</th>
<th>Qty</th>
<th >Options</th>
<th >Value</th>
</tr>
</thead>
<tbody>
<tr>
<td class="item-pid">5</td>
<td class="item-name"></td>
<td class="item-price"></td>
<td class="item-quantity">1</td>
<td class="options">color</td>
<td class="item-total">$23.52</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<th>Sub-Total</td>
<td class="third">5</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<th>Shipping Option</td>
<td class="third">EMS</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<th>Grand Total</td>
<td class="third">100</td>
</tr>
<CAPTION ALIGN="bottom">
This is the table's bottom caption
</CAPTION>
</tbody>
</table>
</div>
I need the last three row columns Sub-total, shipping option and grand total to be as a standalone single column with all td 's prior to it removed. currently i am adding:
<td><td><td><td>
to align it to right. All prior cells are displaying blank. I want all those cells removed and the last 3 cells aligned to right.
How is that possible?
I have a Fiddle Demo here..
Help Requested. Thanks in advance.
You can change those 3 rows code into this:
<tr>
<td colspan="5" style="text-align:right;">Sub-Total</td>
<td class="third">5</td>
</tr>
<tr>
<td colspan="5" style="text-align:right;">Shipping Option</td>
<td class="third">EMS</td>
</tr>
<tr>
<td colspan="5" style="text-align:right;">Grand Total</td>
<td class="third">100</td>
</tr>
You can use colspan to merge the colums and text-align:right for the text to align right.
Check out this Fiddle..
Related
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>
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 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.
i always manage to stuff up tables. i want to make one like the top image but im getting the bottom image with:
<table>
<tr>
<td>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</td>
<td rowspan="3"></td>
</tr>
</table>
can someone please correct my work. i know its lazy but ive already tried like 6 combinations and cant get the right one and its becoming very frustrating. is there some kind of free tool available to format tables to prevent me having this issue in future?
<table border="1">
<tr>
<td width="40"> </td>
<td width="40"> </td>
<td width="80" rowspan="3"> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<!--<td> </td>-->
</tr>
<tr>
<td> </td>
<td> </td>
<!--<td> </td>-->
</tr>
</table>
Fiddle
You have invalid HTML. You cannot have <tr> as children of <td>.
Try this instead...
<table>
<tr>
<td></td>
<td></td>
<td rowspan="3"></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
I must create this table, but colspan and rowspan make my brain crazy. Please help.
Jsfiddle blank for experiments, - http://jsfiddle.net/3pbuT/2/
Fairly straight-forward..... Your'e confusion is the number of rows you had. There are only 2 rows in that table.
DEMO HERE
<table>
<tr>
<td rowspan="2"></td>
<td rowspan="2"></td>
<td colspan="4"></td>
<td rowspan="2"></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
Try this ... if you have dreamweaver tool you can do this very easily....
<table width="200" border="1">
<tr>
<td rowspan="2"> </td>
<td rowspan="2"> </td>
<td colspan="4"> </td>
<td rowspan="2"> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
The easiest way is Dreamweaver, but it doesn't take much to deal with colspan and rowspan, I just did this with very little thinking, and I used jsfiddle just to make sure it was correct.
Enjoy.
<table>
<tr>
<td rowspan="2"></td>
<td rowspan="2"></td>
<td colspan="4"></td>
<td rowspan="2"></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
<table>
<thead>
<tr>
<th rowspan="2">город 1</th>
<th rowspan="2">город 2</th>
<th colspan="4">город 3</th>
<th rowspan="2">город 4</th>
</tr>
<tr>
<th>город 5</th>
<th>город 6</th>
<th>город 7</th>
<th>город 8</th>
</tr>
</thead>
</table>
Something like this:
<table>
<tr>
<td rowspan="2"> </td>
<td rowspan="2"> </td>
<td colspan="4"> </td>
<td rowspan="2"> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
http://jsfiddle.net/3pbuT/9/
<html>
<head>
</head>
<body>
<table border="1">
<tr>
<td rowspan="2">one</td>
<td rowspan="2">Two</td>
<td colspan="4">Im big!</td>
<td rowspan="2">Last</td>
</tr>
<tr>
<td rowspan="2">one</td>
<td rowspan="2">Two</td>
<td>Part 1</td>
<td>Part 2</td>
</tr>
</table>
</body>
</html>
Here you go..
<table border="1">
<tr>
<td rowspan="2"></td>
<td rowspan="2"></td>
<td colspan="4"></td>
<td rowspan="2"></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
colspan combines columns, rowspan combines rows. So you look at how many rows are there at maximum and how many columns there at maximum.
In your case you have 7 columns at maximum and 2 rows at maximum:
<table border="1">
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td>e</td>
<td>f</td>
<td>g</td>
</tr>
<tr>
<td>h</td>
<td>i</td>
<td>j</td>
<td>k</td>
<td>l</td>
<td>m</td>
<td>n</td>
</tr>
</table>
Then you combine columns / rows:
<table border="1" style="padding:5px;border-spacing:10px">
<tr>
<td rowspan="2">a (former a)</td>
<td rowspan="2">b (former b)</td>
<td colspan="4">c (former c)</td>
<td rowspan="2">d (former g)</td>
</tr>
<tr>
<td>e</td>
<td>f</td>
<td>g</td>
<td>h</td>
</tr>
</table>
<html>
<head>
<style type='text/css'>
table {
border-spacing:0;
}
td {
border:1px solid grey;
}
</style>
</head>
<body>
<table>
<tr>
<td rowspan='2'>1 col, 2 rows</td>
<td rowspan='2'>1 col, 2 rows</td>
<td colspan='4'>4 col, 1 row</td>
<td rowspan='2'>1 col, 2 rows</td>
</tr>
<tr>
<td>1 col, 1 row</td>
<td>1 col, 1 row</td>
<td>1 col, 1 row</td>
<td>1 col, 1 row</td>
</tr>
</table>
</body>
</html>
EDIT - I'd recommend against WYSIWYG editors, because you won't learn how to do it yourself. Learning will make a few headaches, sure, but then you KNOW. Give a man a fish...