Table with fixed header make problems - html

I was trying to set up a table with a fixed header which works fine on all tables except the admin table.
showntable:
<div class="table-responsive">
<table class="table table-bordered" id="showntable">
<thead>
<tr>
<th> Business Unit</th>
<th> Product Group</th>
<th> Device</th>
<th> Serial number</th>
<th> Location</th>
<th> Available?</th>
<th> Condition</th>
<th> Calibration Date</th>
<th> Info</th>
<th> Book</th>
</tr>
</thead>
<tbody id="contents">
<?php echo fill_table($connect); ?>
</tbody>
</table>
</div>
bookedtable:
<div class="table-responsive">
<table class="table table-bordered" id="bookedtable">
<thead>
<tr>
<th>Type</th>
<th>Serial Number</th>
<th>From</th>
<th>Until</th>
<th>Edit Comment</th>
<th>Edit Booking</th>
</tr>
</thead>
<tbody>
<?php echo fill_book($connect); ?>
</tbody>
</table>
</div>
admintable:
<div class="table-responsive">
<table class="table table-bordered" id="admintable">
<thead>
<tr>
<th>Business Unit</th>
<th>Product Group</th>
<th>Device</th>
<th>Serial Number</th>
<th>Condition</th>
<th>Calibration Date</th>
<th>Comment</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<?php echo fill_admin($connect); ?>
</tbody>
</table>
</div>
CSS:
thead {
display: table;
width: calc(100% - 17px); /* -17px because of the scrollbar width */
}
tbody {
display: block;
max-height: 55vh;
overflow-y: scroll;
}
#showntable th, #showntable td {
width: 10%;
overflow-x:scroll;
}
#bookedtable th, #bookedtable td {
width: 10%;
overflow-x:scroll;
}
#admintable th, #admintable td {
width:10%;
overflow-x:scroll;
}
th {
text-align: center;
}
td {
text-align: center;
}
The #showntable consists of 10 columns, the #bookedtable of 6 and the #admintable of 8 columns. Somehow the width of 10%
for the th and td of the former two seem to work and take up the entire width but the admin table only seems to broaden
when I put the width as 1% for some reason. The thead is correct but the td seems to make problems.
How can I make the admin table the same size?
Also when I resize the browser window to be smaller the columns don't stay the same size. Do you know how to fix these issues?
Cheers
Edit:
Js Fiddle: http://jsfiddle.net/d7crasvh/23/
Its looks fine in Firefox but in IE and Edge it has some issues... :(
Instead of the admin table being wonky as described in the problem it's now the shown table that looks wrong. also the tables are all not aligned, the header and the body i mean

Related

HTML table - making a single column responsive

How do I go about making the 'description' column responsive? As you can see the bootstrap description is pushing my site. I would like the description go for a specific width, and the text to go behind the 'language' and have just one horizontal scroll bar for all of the description column.
I'm using bootstrap btw so the code doesn't contain a css but maybe it helps you anyway :)
<div className="table-responsive-lg">
<table className="table table-hover text-nowrap">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Description</th>
<th scope="col">Language</th>
<th scope="col">Date Created</th>
<th scope="col">File Size (kb)</th>
<th scope="col">Open Issues</th>
</tr>
</thead>
<tbody>{data.map(item => (
<tr key={item.id}>
<td><a href={item.html_url}>{item.name}</a></td>
<td>{item.description}</td>
<td>{item.language}</td>
<td>{item.created_at.slice(0,10)}</td>
<td>{item.size}</td>
<td>{item.open_issues}</td>
</tr>
))}</tbody>
</table>
</div>
You can define a class with some CSS as below:
.fixed-width {
width: 400px;
display: block;
overflow-x: auto;
}
then add class fixed-width to all <th> and <td> tags in your "description" column.
Use this CSS.
.table {border-collapse:collapse; table-layout:fixed; width:310px;}
.table td {width:100px; /*add as per your requirements*/ word-wrap:break-word;}
Using Child Pseudo Or Unique Id OR Class.
Set as per screen required. Using #media.
I modified the previous answer a bit and add the .fixed-width class only on the column containing the description
.fixed-width {
max-width: 400px;
display: block;
overflow-x: scroll;
}
<div className="table-responsive-lg">
<table className="table table-hover text-nowrap">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Description</th>
<th scope="col">Language</th>
<th scope="col">Date Created</th>
<th scope="col">File Size (kb)</th>
<th scope="col">Open Issues</th>
</tr>
</thead>
<tbody>
<tr key="1">
<td>Google</td>
<td class="fixed-width">some looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong description here</td>
<td>English</td>
<td>19/01/2021</td>
<td>38</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
You can define your css Like this:
tbody tr td:nth-child(2) {
width: 245px;
overflow-x: scroll;
max-width: 245px;
}

Centering span in table body

I am trying to center span in tbody, but I am not sure how to do that and I don't know if this structure is valid if I don't include a tr and td elements.
html
<table class="table">
<thead>
<tr>
<th>Rank</th>
<th>
<div>Name</div>
</th>
<th>
<div>Age</div>
</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<div><span>You haven’t added any data yet</span></div>
</tbody>
</table>
css
tbody div {
display: table-row;
}
tbody div span{
display: block;
text-align: center;
width: 100%;
}
Use colspan:
<table class="table">
<thead>
<tr>
<th>Rank</th>
<th>
<div>Name</div>
</th>
<th>
<div>Age</div>
</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="3">You haven’t added any data yet</td>
</tr>
</tbody>
</table>
By convention you should include tr and th as well within tbody
You can use colspan to merge columns and text-align:center style to center text.
<table class="table" style="width:100%">
<thead>
<tr>
<th>Rank</th>
<th>
<div>Name</div>
</th>
<th>
<div>Age</div>
</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="4" style="text-align: center;">You haven’t added any data yet</td>
</tr>
</tbody>
</table>

How to make a button expand the length of an html table header?

I am trying to get a button that I put in a table <th> to expand the full width and height of the header column, even when a table cell in the same column is larger than the text. If I do, width 100% in css, then the button will just match the cell size, and then the title will extend beyond the button and header.
I am using the button so when the user click on it the column will sort.
This is my code just trying to do with width:
<th><button onclick="tableColumnSort(this)" style="width:100%">#descriptor.Name</button></th>
And this is how it looks without using width:
<th><button onclick="tableColumnSort(this)">#descriptor.Name</button></th>
If you set an element to width: 100% or height: 100%, it's relative to its parent element. So, if you're setting your button width: 100%, and you want it to be 100% of your header width, set a width (that's not in percentage) on the header and it'll respect it. Like this:
HTML:
<table>
<thead>
<tr>
<th>
<button>shortHeader</button>
</th>
<th>
<button>veryLongLongHeader</button>
</th>
<th>
<button>header</button>
</th>
</tr>
</thead>
<tbody>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</tbody>
</table><table>
<thead>
<tr>
<th>
<button>shortHeader</button>
</th>
<th>
<button>veryLongLongHeader</button>
</th>
<th>
<button>header</button>
</th>
</tr>
</thead>
<tbody>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
CSS:
button {
width: 100%;
}
th {
width: 100px;
}
table, th, td {
border: 1px solid #000;
}
Check out this fiddle.
Change css of the clicked th with your js, with a low gray background (#ccc it's good) and change font-size a bit, 1 or 2px. TL;DR Change the frontend to show a more elegant th with the users interactions
<table>
<thead>
<tr>
<th onclick="tableColumnSort(this); changeTH(this);">
shortHeader
</th>
<th onclick="tableColumnSort(this); changeTH(this);">
veryLongLongHeader
</th>
<th onclick="tableColumnSort(this); changeTH(this);">
header
</th>
</tr>
</thead>
<tbody>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</tbody>
</table><table>
<thead>
<tr>
<th onclick="tableColumnSort(this); changeTH(this);">
shortHeader
</th>
<th onclick="tableColumnSort(this); changeTH(this);">
veryLongLongHeader
</th>
<th onclick="tableColumnSort(this); changeTH(this);">
header
</th>
</tr>
</thead>
<tbody>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
On JS:
function changeTH(container){
var ths = document.getElementsByTagName('th');
var totalThs = ths.length;
//reset all ths before set your new th style
for (i = 0; i < totalThs; i++){
ths[i].style.background = '#fff';
ths[i].style.fontSize = 'inherit';
}
container.style.background = '#ccc';
container.style.fontSize = '12px'; //or other size you prefer
}
The better way to do this is add all listeners inside a js file, it's more elegant, but my example works fine.
Make your buttons display: block.
table, th, td { border-collapse: collapse; border: 1px solid #333; }
th button { display: block; width: 100%; }
<table>
<thead>
<tr>
<th><button>DELETE</button></th>
<th><button>PARTNO</button></th>
<th><button>DLT</button></th>
<th><button>TCOUNT</button></th>
<th><button>TRANS</button></th>
</tr>
</thead>
<tbody>
<tr><td></td><td>3</td><td>20170315</td><td>1</td><td>R</td></tr>
<tr><td></td><td>3</td><td>20170315</td><td>1</td><td>R</td></tr>
<tr><td></td><td>3</td><td>20170315</td><td>1</td><td>R</td></tr>
<tr><td></td><td>3</td><td>20170315</td><td>1</td><td>R</td></tr>
</tbody>
</table>

Bootstrap nested inner table with colspan

For this Bootstrap styled table, I want the three columns (Sub1, Sub2, Sub3) widths to grow and align with their corresponding columns in the nested table.
http://jsfiddle.net/jamesholcomb/r55Zc/
Instead of using a nested <table>, simply use the rowspan attribute on the first column. Borders (and some padding) can be removed with some creative CSS (example):
CSS
th { text-align: center; }
tbody td {
border-width: 0 !important;
padding-top:0 !important;
}
tbody tr th ~ td {
border-top-width: 1px !important;
padding-top:8px !important;
}
tbody tr td:first-of-type {
border-left-width: 1px !important;
}
HTML
<div class="row">
<div class="span*">
<table class="table table-bordered">
<thead>
<tr>
<th></th>
<th colspan="3">Col1</th>
</tr>
<tr>
<th></th>
<th>Sub1</th>
<th>Sub2</th>
<th>Sub3</th>
</tr>
</thead>
<tbody>
<tr>
<th rowspan="3">Row1</th>
<td>[-01234543333545]</td>
<td>[4567]</td>
<td>[1]</td>
</tr>
<tr>
<td>[1]</td>
<td>[456.789]</td>
<td>[2]</td>
</tr>
<tr>
<td>[0]</td>
<td>[1]</td>
<td>[0000789.0123]</td>
</tr>
</tbody>
</table>
</div>
</div>
If you want the entire row to highlight correctly, I suggest you tweak your table as others have suggested. However, instead of creating multiple trs and giving the td a rowspan of 3 (which causes row highlighting problems), just list the data in the td as an unordered list and make some adjustments to the css.
Table:
<div class="row">
<div class="span*">
<table class="table table-bordered">
<thead>
<tr>
<th></th>
<th colspan="3">Col1</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>Sub1</td>
<td>Sub2</td>
<td>Sub3</td>
</tr>
<tr>
<td>Row1</td>
<td><ul><li>[-01234543333545]</li><li>[1]</li><li>[0]</li></ul></td>
<td><ul><li>[4567]</li><li>[456.789]</li><li>[1]</li></ul></td>
<td><ul><li>[1]</li><li>[2]</li><li>[0000789.0123]</li></ul></td>
</tr>
</tbody>
</table>
</div>
</div>
CSS:
td > ul {
list-style-type:none;
margin:0;
}
Here is the updated fiddle forked from #Mr.Alien's suggestions.

Fixed column width with horizontal scrolling doesn't show when the column widths larger than container

I am trying to have a table with fixed column widths and horizontal scrolling when the width of the columns is greater than the containing block.
The only time the fixed column widths work is when the sum of column widths < containing block (i.e. no scroll)
Otherwise, the fixed column widths seem to get ignored. Anyone know how to do this? here's my html and css.
<div class="scroll-content-grid21">
<div class="ExtraScrollableContainerDiv">
<table class="regular" style="width:1440px">
<tr>
<th>Item #</th>
<th>Description</th>
<th>Rate</th>
<th>Qty</th>
<th>Price</th>
<th>Amount</th>
<th>Prev Qty</th>
<th>Prev Amt</th>
etc. more columns
</tr>
<%
for (int i = 0; i < this.Model.BusinessObject.Items.Count; i++)
{
%>
<tr>
<td style="width:80px"><%: this.Model.BusinessObject.Items[i].SnapshotReferenceNumber %></td>
<td style="width:240px"><%: this.Model.BusinessObject.Items[i].SnapshotShortDescription%></td>
<td style="width:80px"><%: this.Model.BusinessObject.Items[i].SnapshotUnitRate%></td>
<td style="width:80px"><%: this.Model.BusinessObject.Items[i].SnapshotQuantity%></td>
<td style="width:80px"><%: this.Model.BusinessObject.Items[i].SnapshotUnitOfMeasureId%></td>
<td style="width:80px"><%: this.Model.BusinessObject.Items[i].SnapshotAmount%></td>
<td style="width:80px"><%: this.Model.BusinessObject.Items[i].PreviousToDateQuantity%></td>
<td style="width:80px"><%: this.Model.BusinessObject.Items[i].PreviousToDateAmount%></td>
etc. more columns
</tr>
</table>
div.scroll-content-grid21
{
overflow : auto;
width: 1072px; /* notice, smaller than the table width */
height: 500px;
}
table.regular
{
table-layout:fixed;
margin-top: 0.1em;
margin-bottom: 0.1em;
border-collapse: collapse;
text-align: left;
}
The first thing I can see is that you need to give width to your table header cells (th) as well. try this to see if it helps. Also have you given {overflow-x:auto} to td and th ?
Here's a sample for anyone else who needs it:
<html>
<head>
<style>
div.outer
{
width : 500px;
overflow : auto;
}
table
{
table-layout:fixed;
width: 100%; /* same as containing div */
}
th, td
{
border: solid 1px #ccc;
}
</style>
</head>
<body>
<div class="outer">
<div class="scrollable">
<table>
<thead>
<th style="width:50px">One</th>
<th style="width:300px">Two</th>
<th style="width:200px">Three</th>
<th style="width:200px">Four</th>
<th style="width:200px">Five</th>
</thead>
<tbody>
<tr>
<td>001</td>
<td>My really long description here</td>
<td>10.0 units</td>
<td>$100.00 dollars</td>
<td>$1000.00 total</td>
</tr>
<tr>
<td>002</td>
<td>This is number 2</td>
<td>5 units</td>
<td>$5.00 dollars</td>
<td>$25.00 total</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>