Fixed Table Header with CSS in Internet Explorer - html

I have a CSS Code with
.header-table-container th, .table-1 th, .table-2 th{color: #fff; position:sticky;top:0;}
to fix the Table header in my table. Works fine on Chrome/Firefox. But it also has to work in IE as well. I know sticky is not supported in IE. But position:fixed; top:0; created all the Header Elements at one place.
What can I do here?
Edit:
CSS
<style>
*{padding:0; margin: 0; border: 0; font-size: 1em; line-height: 1.3em; font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; box-sizing: border-box;}
table{width: 100%; background: #999;}
.header-table-container table{background: #076c84;}
.header-table-container th, .table-1 th, .table-2 th{color: #fff; position:sticky;top:0;}
tr.table_row_1{background: #eee;}
tr.table_row_0 td{background: #fff;}
th, td{padding: 0.25em 0.5em;}
.table-1 th{min-width: 10em;}
.table-1 th:nth-of-type(2){min-width: 5em;}
.table-1 th, .table-2 th{background: #076c84;}
.js-enabled .table-1 th{color: #076c84;}
.table-2{margin-top: 2em; width:auto;}
.table-2 tr.table_row_1 td{background: #fff;}
#media only screen and (max-width: 60em){
.table-2{width:100%;}
.table-2 thead{display: none;}
.table-2 tr, .table-2 td, .table-2 tr:before{display: block;}
.table-2 tr{padding-top: 0.5em; background: #076c84;}
.table-2 td:before{content: attr(data-mykey -column)": ";}
.table-2 tr:before{content: attr(data-mykey-row)": "; padding-left: 0.5em; padding-bottom: 0.5em; color:#fff;}
}
</style>
HTML
> <table id="table-2" class="table-2"> <thead> <tr> <th>RDW</th>
> <th>Komm-Nr.</th> <th>E</th> <th>EBNr</th> <th>VBNr</th> <th>MNr.</th>
> <th>BSNr.</th> <th>Personal-Nr.</th> <th>CCJJVkz</th> <th>A</th>
> <th>Pers.Id</th> <th>Nat</th> <th>Plz</th> <th>lfd.Kuv</th>
> <th>Blatt/Obg</th> <th>Blatt/Kuv</th> <th>Info.F</th> <th>akt~Fol</th>
> <th>SeV</th> <th>SeB</th> <th>Bla-VON</th> <th>Bla-BIS</th>
> <th>Kuv.-Id</th> <th>K~EF</th> <th>ZAK</th> <th>GAK</th>
> <th>GruNr</th> <th>AnwK</th> <th>Nkr.</th> <th>Bla-akt</th>
> <th>Seite-VON</th> <th>Seite-BIS</th> <th>Seitenz</th>
> <th>leere~S</th> <th>NOTE-$3</th> <th>Block-$3</th> <th>Displ-$3</th>
> <th>NOTE-$4</th> <th>Block-$4</th> <th>Displ-$4</th> </tr> </thead>
> <tbody>
>
> <tr class="table_row_0" data-mykey-row="Objekt 1"> <td
> data-mykey-column="RDW">0304</td> <td data-mykey-column="Komm-Nr.">
> 00000000003</td> <td data-mykey-column="E">M</td> <td
> data-mykey-column="EBNr">0007185</td> <td
> data-mykey-column="VBNr">0000000</td> <td
> data-mykey-column="MNr.">52050</td> <td
> data-mykey-column="BSNr.">00000</td> <td
> data-mykey-column="Personal-Nr.">00000000000000000000</td> <td
> data-mykey-column="CCJJVkz">2018YIE</td> <td
> data-mykey-column="A">0</td> <td
> data-mykey-column="Pers.Id">0000000</td> <td
> data-mykey-column="Nat">D</td> <td data-mykey-column="Plz">54439</td>
> <td data-mykey-column="lfd.Kuv">0000001</td> <td
> data-mykey-column="Blatt/Obg">000000009</td> <td
> data-mykey-column="Blatt/Kuv">000000009</td> <td
> data-mykey-column="Info.F">VF4</td> <td
> data-mykey-column="akt~Fol">001</td> <td
> data-mykey-column="SeV">001</td> <td data-mykey-column="SeB">001</td>
> <td data-mykey-column="Bla-VON">001</td> <td
> data-mykey-column="Bla-BIS">0000001</td> <td
> data-mykey-column="Kuv.-Id">0000009</td> <td
> data-mykey-column="K~EF">0069013</td> <td
> data-mykey-column="ZAK">1</td> <td data-mykey-column="GAK">00</td> <td
> data-mykey-column="GruNr">001</td> <td
> data-mykey-column="AnwK">000</td> <td
> data-mykey-column="Nkr.">01488</td> <td
> data-mykey-column="Bla-akt">LO41</td> <td
> data-mykey-column="Seite-VON">L201</td> <td
> data-mykey-column="Seite-BIS">0000009</td> <td
> data-mykey-column="Seitenz">000000001</td> <td
> data-mykey-column="leere~S">000000011</td> <td
> data-mykey-column="NOTE-$3">0000011</td> <td
> data-mykey-column="Block-$3">0000007</td> <td
> data-mykey-column="Displ-$3">00010301</td> <td
> data-mykey-column="NOTE-$4">00000008</td> <td
> data-mykey-column="Block-$4">00000350</td> <td
> data-mykey-column="Displ-$4">00010301</td> </tr> ....
Javascript
<script>
var timer = null;
var table = document.getElementById("table-1");
var headerTable = null;
var tableColumns = new Array();
var headerTableColumns = new Array();
if(table){
var heading = table.getElementsByTagName("thead")[0].getElementsByTagName("tr")[0];
if(heading){
document.getElementsByTagName("body")[0].className += "js-enabled";
var columns = heading.getElementsByTagName("th");
var countOfRows = columns.length;
var headerRow = document.createElement("tr");
var headerTableContainer = document.createElement("div");
headerTable = document.createElement("table");
for (var i = 0; i < countOfRows; i++){
var column = document.createElement("th");
column.appendChild(document.createTextNode(columns[i].firstChild.nodeValue));
headerRow.appendChild(column);
tableColumns.push(columns[i]);
headerTableColumns.push(column);
}
headerTable.className = "header-table";
headerTable.appendChild(headerRow);
headerTableContainer.className = "header-table-container";
headerTableContainer.appendChild(headerTable);
document.getElementsByTagName("body")[0].appendChild(headerTableContainer);
updateHeaderTableWidth();
if (window != null && typeof(window) != "undefined"){
if (window.addEventListener) {
window.addEventListener("resize", resetTableWidth, false);
window.addEventListener("scroll", scrolling, false);
} else {
if (window.attachEvent) {
window.attachEvent("onresize", resetTableWidth);
window.attachEvent("onscroll", scrolling);
} else {
window["onresize"] = resetTableWidth;
window["onscroll"] = scrolling;
}
}
}
}
}
/* Method starts a timer to update the width of the columns in the header table
*/
function resetTableWidth(){
if (timer!= null){
window.clearTimeout(timer);
timer = null;
}
timer = window.setTimeout("updateHeaderTableWidth()", 1000);
}
/* Method updates the width of the columns in the header table
*/
function updateHeaderTableWidth(){
if(headerTableColumns.length <= 0 || tableColumns.length <= 0 || headerTableColumns.length != tableColumns.length){
return;
}
var countOfRows = headerTableColumns.length;
for (var i = 0; i < countOfRows; i++){
headerTableColumns[i].style.width = tableColumns[i].offsetWidth+"px";
}
headerTable.style.width = table.offsetWidth+"px";
}
/* Method updates the position of the header table when scrolling left-right
*/
function scrolling(){
headerTable.style.marginLeft = -window.pageXOffset+"px";
if(window.pageYOffset <= 0 && table.offsetTop == 0 ||(window.pageYOffset >= table.offsetTop && window.pageYOffset < table.offsetTop+table.offsetHeight-headerTable.offsetHeight-10)){
headerTable.style.display = "block";
}
else{
headerTable.style.display = "none";
}
}
</script>

Related

Posting a Google Sheet to a Google Site as an HTML table

I have a Google Sheet with about six hundred rows of data. I want to be able to display and filter this spreadsheet as an HTML table and have it displayed to a Google Site. I have found this code online along with the 'Display' HTML which I have used.
However when I publish as a web app, I am only seeing the outline of my table but with no data in the rows. The outline of my table looks correct and contains the search filters I need, however there is no data appearing in the table.
Is there an issue with the code below?
function myFunction() {
}function doGet(e){
var SHEET_ID=e.parameter.sheet_id
var html=HtmlService.createTemplateFromFile('Display')
var ss=SpreadsheetApp.openById('My Google Sheet ID')
var sheet=ss.getSheetByName('Sheet2')
html.data=getRowsData(sheet,sheet.getDataRange(),1)
return html.evaluate().setTitle('Log')
Logger.log(getRowsData(sheet,sheet.getDataRange(),1))
}
function include(filename){
return HtmlService.createHtmlOutputFromFile(filename).getContent()
}
function getRowsData(sheet, range, columnHeadersRowIndex){
columnHeadersRowIndex = columnHeadersRowIndex || range.getRowIndex()-1;
var numColumns = range.getEndColumn() - range.getColumn() + 1;
var headersRange = sheet.getRange(columnHeadersRowIndex, range.getColumn(), 1,
numColumns);
var headers = headersRange.getValues()[0];
return getObjects(range.getValues(),normalizeHeaders(headers));
}
function getObjects(data, keys) {
var objects = [];
for (var i = 0; i < data.length; ++i) {
var object = {};
var hasData = false;
for (var j = 0; j < data[i].length; ++j) {
var cellData = data[i][j];
if (isCellEmpty(cellData)) {
continue;
}
object[keys[j]] = cellData;
hasData = true;
}
if (hasData) {
objects.push(object);
}
}
return objects;
}
function normalizeHeaders(headers) {
var keys = [];
for (var i = 0; i < headers.length; ++i) {
var key = normalizeHeader(headers[i]);
if (key.length > 0) {
keys.push(key);
}
}
return keys;
}
function normalizeHeader(header) {
var key ='';
var upperCase = false;
for (var i = 0; i < header.length; ++i) {
var letter = header[i];
if (letter == '' && key.length > 0) {
upperCase = true;
continue;
}
if (!isAlnum(letter)) {
continue;
}
if (key.length == 0 && isDigit(letter)) {
continue; // first character must be a letter
}
if (upperCase) {
upperCase = false;
key += letter.toUpperCase();
} else {
key += letter.toLowerCase();
}
}
return key;
}
function isAlnum(char) {
return char >= 'A' && char <= 'Z' ||
char >= 'a' && char <= 'z' ||
isDigit(char);
}
function isDigit(char) {
return char >= '0' && char <= '9';
}
function isCellEmpty(cellData) {
return typeof(cellData) == 'string' && cellData == '';
}
Display.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<?!=include('Style')?>
</head>
<body>
<p></p>
<table id="dataTable" border="1" cellpadding="2" cellspacing="0">
<th width="10%">Employee Number</th>
<th width="15%">Name</th>
<th width="15%">Surname</th>
<th width="15%">EmpNo</th>
<th width="10%">Title</th>
<th width="15%">Div</th>
<th width="10%">Office</th>
<th width="10%">Form</th>
<tr>
<td bgcolor="#eb8c00" align="center"><input type="text"
id="EmployeeNumberSearchBox" onkeyup="SearchFunc()"
placeholder="EmployeeNumber..." width="30"></td>
<td bgcolor="#eb8c00" align="center"><input type="text"
id="NameSearchBox"
onkeyup="SearchFunc()" placeholder="Name..." width="35"></td>
<td bgcolor="#eb8c00" align="center"><input type="text" id="SurnameSearchBox"
onkeyup="SearchFunc()" placeholder="Surname..." width="35"></td>
<td bgcolor="#eb8c00" align="center"><input type="text" id="EmpNoSearchBox"
onkeyup="SearchFunc()" placeholder="EmpNo..." width="35"></td>
<td bgcolor="#eb8c00" align="center"><input type="text" id="TitleSearchBox"
onkeyup="SearchFunc()" placeholder="Title..." width="35"></td>
<td bgcolor="#eb8c00" align="center">
<select id="Div" onchange="SearchFunc();">
<option value="">Div...</option>
<option value="DOO">DOO</option>
<option value="PIT">PIT</option>
<option value="FID">FID</option>
<option value="FIS">FIS</option>
<option value="General">General</option>
<option value="Other">Other</option>
<option value="IS">IS</option>
<option value="SAS">SAS</option>
</select>
</td>
<td bgcolor="#eb8c00" align="center">
<select id="Office" onchange="SearchFunc();">
<option value="">Office...</option>
<option value="London">London</option>
<option value="New York">New York</option>
<option value="Berlin">Berlin</option>
<option value="Eindhoven">Eindhoven</option>
<option value="Lille">Lille</option>
<option value="Vienna">Vienna</option>
<option value="Copenhagen">Copenhagen</option>
</select>
</td>
<td bgcolor="#eb8c00" align="center"></td>
</tr>
<? for (var i=1;i<data.length;i++) {?>
<tr style="height:15px;">
<td align="center"><?=data[i]['Employee Number']?> </td>
<td align="left"><?=data[i]['Div']?> </td>
<td align="center"><?=data[i]['EmpNo']?> </td>
<td align="center"><?=data[i]['Name']?> </td>
<td align="center"><?=data[i]['Surname']?> </td>
<td align="center"><?=data[i]['Title']?> </td>
<td align="center"><?=data[i]['Office']?> </td>
<td align="center">
<a href="<?=data[i]['=ARRAYFORMULA(HYPERLINK(K1:K616, "Click here to complete
Access Form"))']?>" target="_blank"><button>Open Form</button>
</a>
</td>
</tr>
<? } ?>
</table>
</body>
</html>
Style.Html
<style>
input[type=text] {
font-size: 12px; /* Increase font-size */
padding: 12px 12px 12px 12px; /* Add some padding */
border: 1px solid #ddd; /* Add a grey border */
margin-bottom: 3px; /* Add some space below the input */
margin-top: 2px;
}
#EmployeeNumberSearchBox, #EmpNoSearchBox, #NameSearchBox,
#SurnameSearchBox, #OfficeSearchBox, #TitleSearchBox {
text-align:center;
width:50%;
}
#DivSearchBox{
text-align:center;
width:85%;
}
#dataTable {
border-collapse: collapse; /* Collapse borders */
width: 100%; /* Full-width */
border: 1px solid #ddd; /* Add a grey border */
font-size: 14px; /* Increase font-size */
table-layout: fixed;
}
#dataTable th{
text-align: center; /* Centre-align text */
background-color: #d04a02;
color: #ffffff
}
#dataTable td {
padding: 3px 5px 2px 10px;
}
#dataTable tr {
/* Add a bottom border to all table rows */
border-bottom: 1px solid #ddd;
}
button {
background-color: #464646;
border:none;
padding: 3px 5px 2px;
text-align: center;
display: inline-block;
font-size: 14px;
font-family: Helvetica Neue;
font-style: Bold;
color: white;
}
button:hover {
background-color: #eb8c00; /* Green */
color: white;
cursor:pointer;
}
select {
border: 0;
width: 100%;
height:35px;
text-align-last:center;
}
</style>
Java.html
<script>
function SearchFunc() {
var EmployeeNumberInput, DivInput, EmpNoInput, NameInput,
SurnameInput, OfficeInput, TitleInput,
EmployeeNumberFilter, DivFilter, EmpNoFilter, NameFilter,
SurnameFilter, OfficeFilter, TitleFilter,
table, tr, i,
EmployeeNumberValue, DivValue, EmpNoValue, NameValue,
SurnameValue, OfficeValue, TitleValue;
EmployeeNumberInput = document.getElementById("Employee Number");
DivInput = document.getElementById("Div");
EmpNoInput = document.getElementById("EmpNo");
NameInput = document.getElementById("Name");
SurnameInput = document.getElementById("Surname");
TitleInput = document.getElementById("Title");
OfficeInput = document.getElementById("Office");
EmployeeNumberFilter = EmployeeNumberInput.value.toUpperCase();
DivFilter = DivInput.value.toUpperCase();
EmpNoFilter = EmpNoInput.value.toUpperCase();
NameFilter = NameInput.value.toUpperCase();
SurnameFilter = SurnameInput.value.toUpperCase();
TitleFilter = TitleInput.value.toUpperCase();
OfficeFilter = OfficeInput.value.toUpperCase();
table = document.getElementById("dataTable");
tr = table.getElementsByTagName("tr");
for (i = 2; i < tr.length; i++) {
EmployeeNumberValue = tr[i].getElementsByTagName("td")[0];
DivValue = tr[i].getElementsByTagName("td")[1];
EmpNoValue = tr[i].getElementsByTagName("td")[2];
NameValue = tr[i].getElementsByTagName("td")[3];
SurnameValue = tr[i].getElementsByTagName("td")[4];
TitleValue = tr[i].getElementsByTagName("td")[5];
OfficeValue = tr[i].getElementsByTagName("td")[6];
if (EmployeeNumberValue) {
if (EmployeeNumberValue.textContent.toUpperCase().indexOf
(EmployeeNumberFilter) > -1
&&
DivValue.textContent.toUpperCase().indexOf(DivFilter) > -1
&&
EmpNoValue.textContent.toUpperCase().indexOf(EmpNoFilter) > -1
&&
NameValue.textContent.toUpperCase().indexOf(NameFilter) >
-1
&&
SurnameValue.textContent.toUpperCase().indexOf(SurnameFilter) > -1
&&
TitleValue.textContent.toUpperCase().indexOf(TitleFilter) > -1)
&&
OfficeValue.textContent.toUpperCase().indexOf(OfficeFilter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
function sortTable() {
var table, rows, switching, i, x, y, shouldSwitch;
table = document.getElementById("dataTable");
switching = true;
/* Make a loop that will continue until
no switching has been done: */
while (switching) {
// Start by saying: no switching is done:
switching = false;
rows = table.rows;
/* Loop through all table rows (except the
first, which contains table headers): */
for (i = 2; i < (rows.length - 1); i++) {
// Start by saying there should be no switching:
shouldSwitch = false;
/* Get the two elements you want to compare,
one from current row and one from the next: */
x = rows[i].getElementsByTagName("TD")[0];
y = rows[i + 1].getElementsByTagName("TD")[0];
// Check if the two rows should switch place:
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
// If so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
}
if (shouldSwitch) {
/* If a switch has been marked, make the switch
and mark that a switch has been done: */
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
}
</script>
Instead of <?= use <?!=
From https://developers.google.com/apps-script/guides/html/templates#force-printing_scriptlets
Force-printing scriptlets
Force-printing scriptlets, which use the syntax <?!= ... ?>, are like printing scriptlets except that they avoid contextual escaping.
Contextual escaping is important if your script allows untrusted user input. By contrast, you’ll need to force-print if your scriptlet’s output intentionally contains HTML or scripts that you want to insert exactly as specified.
As a general rule, use printing scriptlets rather than force-printing scriptlets unless you know that you need to print HTML or JavaScript unchanged.
Related
How to use scriptlets in HTMLOutput in Google Apps Script

Cannot remove padding in table

I am trying to remove the padding from the cells inside my table. I have set it to not have padding in the relevant CSS selectors but not a success.
As you can see, there is padding on all of these cells.
I would like there to not be. I have tried various different padding settings and changing the vertical alignment makes no difference other than to move the text, the padding just goes from all at the bottom to spread between bottom and top.
Below is the code:
'use strict'
let table = document.getElementById("mainTable")
let rows = table.querySelectorAll("tbody tr")
let columns = table.querySelectorAll("#weeks th")
for (let row of rows) {
for (let o = 0; o<columns.length-1; o++) {
let cell = document.createElement("td")
cell.innerHTML='&nbsp'
cell.addEventListener("click", function() {
if (cell.getElementsByTagName("input")[0]) { return } //If cell currently has an input box
//
let oldValue = ""
if (cell.innerHTML !== " ") { //if cell has a saved value
oldValue = cell.innerHTML
}
cell.innerHTML = '<input type="text" class="cellInputs">'
//update input box with old value and focus it
cell.getElementsByTagName("input")[0].focus()
cell.getElementsByTagName("input")[0].value = oldValue
cell.getElementsByTagName("input")[0].addEventListener("keypress", function(e) {
if (e.keyCode === 13) {
cell.innerHTML=cell.getElementsByTagName("input")[0].value
e.preventDefault()
return true
}
})
cell.getElementsByTagName("input")[0].addEventListener("input", function(e) {
console.log(e)
let cellValue = cell.getElementsByTagName("input")[0].value
if (e.data === "." && (cellValue.split('.').length-1 > 1 || cellValue === ".")) {
console.log("stop")
cell.getElementsByTagName("input")[0].value = (cellValue).substring(0, cellValue.length - e.data.length)
}
if (isNaN(e.data) && e.data !==".") {
console.log("Stop")
cell.getElementsByTagName("input")[0].value = (cellValue).substring(0, cellValue.length - e.data.length)
}
//store value inputted into the actual cell
})
cell.getElementsByTagName("input")[0].addEventListener("paste", function(e) {
// clipboardData = e.clipboardData || window.clipboardData;
// pastedData = clipboardData.getData('Text');
let cellValue = cell.getElementsByTagName("input")[0].value
if (cellValue !== "") {
e.preventDefault()
return false
}
if (e.clipboardData.getData('text') === "." && (cellValue.split('.').length-1 > 1 || cellValue === ".")) {
e.preventDefault()
return false
}
if (isNaN(e.clipboardData.getData('text')) && e.clipboardData.getData('text') !==".") {
e.preventDefault()
return false
}
//store value inputted into the actual cell
})
cell.getElementsByTagName("input")[0].addEventListener("focusout", function() {
console.log(document.activeElement)
cell.innerHTML=cell.getElementsByTagName("input")[0].value
})
})
row.appendChild(cell)
}
}
*{
padding: 0;
margin: 0;
font-family: "Trebuchet MS", Times, serif;
box-sizing:border-box;
}
html{
background-color: #35454E;
overflow: hidden;
}
html *{
font-family: "Work Sans", Arial, sans-serif !important;
color: white !important;
}
table{
border-collapse: collapse;
border-spacing: 0px;
color:#35454E;
height:100%;
width:100%;
}
table, th{
border: 2px solid white;
padding:0;
}
th{
vertical-align:top;
font-size: 2.5vw;
}
td{
vertical-align:top;
box-sizing:border-box;
position: relative;
border: 2px solid white;
padding:0;
text-align: center;
font-size: 2.5vw;
padding:0;
}
.cellInputs{
position: absolute;
width:100%;
height:100%;
display: block;
top:0;
left:0;
border:none;
text-align: center;
background-color: #35454E;
word-wrap: break-word;
font-size: 2.5vw;
}
<html>
<head>
<link rel="stylesheet" type="text/css" href="MMS.css">
<title>Money Management</title>
</head>
<body>
<table id="mainTable">
<thead>
<tr>
<th>2019</th>
<th colspan="5">January</th>
</tr>
<tr id="weeks">
<th>&nbsp</th>
<th>31/12/2018</th>
<th>07/01/2019</th>
<th>14/01/2019</th>
<th>21/01/2019</th>
<th>28/01/2019</th>
</tr>
</thead>
<tbody>
<tr>
<th>Balance</th>
</tr>
<tr>
<th>Pay</th>
</tr>
<tr>
<th>&nbsp</th>
</tr>
<tr>
<th>Rent</th>
</tr>
<tr>
<th>Food</th>
</tr>
<tr>
<th>&nbsp</th>
</tr>
<tr>
<th>Total</th>
</tr>
</tbody>
</table>
</body>
<script src="MMS.js"></script>
</html>
Remove height:100% from table .

How to center pagination buttons after table? text-align not working

This seems really simple but I am struggling with CSS.
Basically I have a table and after it I use a pagination system to navigate through the table content. The real table has more information, I just used an example to make it look simpler.
I am having a problem trying to center the buttons for the pagination after the table. The text-align property does not seem to be working and I am not sure why.
How can I center the buttons in the center considering the 100% width.
Thanks.
$(document).ready(function() {
$('#table').after('<div id="nav" class="pagination"></div>');
var rowsShown = 2;
var rowsTotal = $('#table tbody tr').length;
var numPages = rowsTotal / rowsShown;
for (i = 0; i < numPages; i++) {
var pageNum = i + 1;
$('#nav').append('' + pageNum + ' ');
}
$('#table tbody tr').hide();
$('#table tbody tr').slice(0, rowsShown).show();
$('#nav a:first').addClass('active');
$('#nav a').bind('click', function() {
$('#nav a').removeClass('active');
$(this).addClass('active');
var currPage = $(this).attr('rel');
var startItem = currPage * rowsShown;
var endItem = startItem + rowsShown;
$('#table tbody tr').css('opacity', '0.0').hide().slice(startItem, endItem).
css('display', 'table-row').animate({
opacity: 1
}, 300);
});
});
.table {
width: 100%;
}
.pagination {
display: inline-block;
width: 100%;
text-align: center;
}
.pagination a {
color: black;
float: left;
padding: 8px 16px;
text-decoration: none;
transition: background-color .3s;
}
.pagination a.active {
background-color: #4CAF50;
color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table id="table" class="table">
<thead>
<tr>
<th>Name</th>
<th>Item</th>
</tr>
</thead>
<tbody id="tbody">
<tr>
<td>Sarah</td>
<td>3</td>
</tr>
<tr>
<td>Tom</td>
<td>3</td>
</tr>
<tr>
<td>Kate</td>
<td>1</td>
</tr>
<tr>
<td>Michael</td>
<td>1</td>
</tr>
</tbody>
</table>
You can do this.
$(document).ready(function() {
$('#table').after('<div id="nav" class="pagination"></div>');
var rowsShown = 2;
var rowsTotal = $('#table tbody tr').length;
var numPages = rowsTotal / rowsShown;
for (i = 0; i < numPages; i++) {
var pageNum = i + 1;
$('#nav').append('' + pageNum + ' ');
}
$('#table tbody tr').hide();
$('#table tbody tr').slice(0, rowsShown).show();
$('#nav a:first').addClass('active');
$('#nav a').bind('click', function() {
$('#nav a').removeClass('active');
$(this).addClass('active');
var currPage = $(this).attr('rel');
var startItem = currPage * rowsShown;
var endItem = startItem + rowsShown;
$('#table tbody tr').css('opacity', '0.0').hide().slice(startItem, endItem).
css('display', 'table-row').animate({
opacity: 1
}, 300);
});
});
.table {
width: 100%;
}
.pagination {
display: inline-block;
width: 100%;
text-align: center;
display:flex;
justify-content:center;
}
.pagination a {
color: black;
float: left;
padding: 8px 16px;
text-decoration: none;
transition: background-color .3s;
}
.pagination a.active {
background-color: #4CAF50;
color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table id="table" class="table">
<thead>
<tr>
<th>Name</th>
<th>Item</th>
</tr>
</thead>
<tbody id="tbody">
<tr>
<td>Sarah</td>
<td>3</td>
</tr>
<tr>
<td>Tom</td>
<td>3</td>
</tr>
<tr>
<td>Kate</td>
<td>1</td>
</tr>
<tr>
<td>Michael</td>
<td>1</td>
</tr>
</tbody>
</table>

Filter table with multiple columns

Just trying to filter a table but also have it filter the number with and without dashes (working) but also search the name and id as well. Its only searching the one column since the index is [0].
How would I have it search all 3 columns? So if I search number or id or name it would filter. Here is the working code I have so far to search number.
<!DOCTYPE html>
<html>
<head>
<style>
* {
box-sizing: border-box;
}
#myInput {
background-image: url('/css/searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
width: 100%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}
#myTable {
border-collapse: collapse;
width: 100%;
border: 1px solid #ddd;
font-size: 18px;
}
#myTable th, #myTable td {
text-align: left;
padding: 12px;
}
#myTable tr {
border-bottom: 1px solid #ddd;
}
#myTable tr.header, #myTable tr:hover {
background-color: #f1f1f1;
}
</style>
</head>
<body>
<h2>Number search</h2>
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">
<table id="myTable">
<tr class="header">
<th style="width:60%;">Number</th>
<th style="width:60%;">Name</th>
<th style="width:60%;">ID</th>
</tr>
<tr>
<td>905-373-3333</td>
<td>Mike</td>
<td>4563</td>
</tr>
<tr>
<td>905-333-3333</td>
<td>adam</td>
<td>8963</td>
</tr>
<tr>
<td>416-373-3432</td>
<td>Jim</td>
<td>9363</td>
</tr>
</table>
<script>
function myFunction() {
var input, filter, table, tr, td, i, cleanedFilter;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
cleanedFilter = filter.replace("-","");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
cellContent = td.innerHTML.toUpperCase().replace(/-/g,"");
if (cellContent.indexOf(cleanedFilter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
</script>
</body>
</html>
If you want to use a filter for every td available in your rows, you can use the following:
(function(document) {
'use strict';
var LightTableFilter = (function(Arr) {
var _input;
function _onInputEvent(e) {
_input = e.target;
var tables = document.getElementsByClassName(_input.getAttribute('data-table'));
Arr.forEach.call(tables, function(table) {
Arr.forEach.call(table.tBodies, function(tbody) {
Arr.forEach.call(tbody.rows, _filter);
});
});
}
function _filter(row) {
var text = row.textContent.toLowerCase(), val = _input.value.toLowerCase();
row.style.display = text.indexOf(val) === -1 ? 'none' : 'table-row';
}
return {
init: function() {
var inputs = document.getElementsByClassName('light-table-filter');
Arr.forEach.call(inputs, function(input) {
input.oninput = _onInputEvent;
});
}
};
})(Array.prototype);
document.addEventListener('readystatechange', function() {
if (document.readyState === 'complete') {
LightTableFilter.init();
}
});
})(document);
<section class="container">
<input type="search" class="light-table-filter" data-table="order-table" placeholder="Filter">
<table class="order-table table">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Number 2</th>
<th>Number 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Column One</td>
<td>Two</td>
<td>352353</td>
<td>1</td>
</tr>
<tr>
<td>Column Two</td>
<td>Two</td>
<td>4646</td>
<td>2</td>
</tr>
</tbody>
</table>
</section>

How to make table responsive

I have some table and I want to make it responsive when it show in width max 760.
This is the table looks normal :
And I want to make it like this if width of the screen is max 760 :
In that table the last coloumn just appears in end of table not in each row.
Anyone can help me to do that ?
Use this html & css code for make rsponsive table
Check the Snippets is below .
Use this css with media query max-width:760px or any width
.responsive-table {
margin:0px auto;
width:500px;
border: 1px solid #efebeb;
}
.responsive-table img{height:100px; }
.responsive-table th {
display: none;
}
.responsive-table td {
display: block;
border-top: 1px solid #ddd;
}
.responsive-table td:first-child{border:none;}
.responsive-table td:first-child {
padding-top: .5em;
}
.responsive-table td:last-child {
padding-bottom: .5em;
}
.responsive-table td:before {
content: attr(data-th) ": ";
display:block;
font-weight:bold;
}
<table class="responsive-table">
<tbody>
<tr>
<th>
Room
</th>
<th>
Guest
</th>
<th>
Description
</th>
<th>
Rate
</th>
<th>
Book
</th>
</tr>
<tr>
<td data-th="Room">
<img src="http://i45.tinypic.com/t9ffkm.jpg" alt="" />
</td>
<td data-th="Guest">
2 adult
</td>
<td data-th="Description">
Room Only
</td>
<td data-th="Rate">
USD 200
</td>
<td data-th="Book">
<button type="submit"> Book</button>
</td>
</tr>
</tbody>
</table>
I wrote js function to make table responsive. You can set breakpoint and table tag or css class name as parameters.
const makeTableResponsive = (mq, t) => {
var q = window.matchMedia(mq);
if (q.matches) {
let headings = [];
let tables = document.querySelectorAll(t);
if (tables && tables.length > 0) {
tables.forEach(table => {
let thead = table.querySelector("thead");
let headingTags = thead.querySelectorAll("th");
let tbody = table.querySelector("tbody");
let tbodies = tbody.querySelectorAll("tr");
tbody.style.display = "block";
thead.style.display = "none";
headingTags.forEach((th) => {
th.style.display = "block";
headings.push(th.innerText);
});
tbodies.forEach((tr) => {
let trstyle = tr.style;
trstyle.display = "block";
trstyle.border = "1px solid #ccc";
trstyle.marginBottom = "30px";
let tds = tr.querySelectorAll("td");
tds.forEach((td, i) => {
td.style.display = "flex";
td.style.flexDirection = "row-reverse";
td.style.justifyContent = "space-between";
td.innerHTML += `<span class="font-weight-bold text-primary">${headings[i]}</span>`;
});
});
})
}
}
};
makeTableResponsive('(max-width: 992px)', 'table');