I want to make the search input field of my data table sticky.
"iDisplayLength": 1000,
"bLengthChange": true,
"bFilter": true,
"bInfo": true,
"bSort": true,
"order": [[ 0, "desc" ]],
dom: "Bfrtip",
The data table header is already sticky, but not the search field.
For the sticky the data table, I used:
thead {
position: sticky;
top: 0px;
background: white;
That works, but not for the search field. "scrollX": true/false doesn't work.
You can apply a style to the DataTable global search box using the dom option.
Example:
dom: 'B<"sticky-wrapper"f>rtip'
Each of the Bfrtip values is explained in the linked documentation - but for our specific needs, the f is for the filter box.
This is wrapped in a class called sticky-wrapper using <"sticky-wrapper"f>.
The style (in my test example) is based on the one used in the question:
.sticky-wrapper {
position: sticky;
top: 0px;
background: white;
}
Demo:
$(document).ready(function() {
var table = $('#example').DataTable({
dom: 'B<"sticky-wrapper"f>rtip'
});
});
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Demo</title>
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.css">
<link rel="stylesheet" type="text/css" href="https://datatables.net/media/css/site-examples.css">
<style>
.sticky-wrapper {
position: sticky;
top: 0px;
background: white;
}
</style>
</head>
<body>
<div style="margin: 20px;">
<table id="example" class="display dataTable cell-border" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office in Country</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
</tr>
<tr>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>$170,750</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>Junior "Technical" Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009/01/12</td>
<td>$86,000</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>Senior Javascript Developer</td>
<td>Edinburgh</td>
<td>22</td>
<td>2012/03/29</td>
<td>$433,060</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>33</td>
<td>2008/11/28</td>
<td>$162,700</td>
</tr>
<tr>
<td>Brielle Williamson</td>
<td>Integration Specialist</td>
<td>New York</td>
<td>61</td>
<td>2012/12/02</td>
<td>$372,000</td>
</tr>
<tr>
<td>Herrod Chandler</td>
<td>Sales Assistant</td>
<td>San Francisco</td>
<td>59</td>
<td>2012/08/06</td>
<td>$137,500</td>
</tr>
<tr>
<td>Rhona Davidson</td>
<td>Integration Specialist</td>
<td>Tokyo</td>
<td>55</td>
<td>2010/10/14</td>
<td>$327,900</td>
</tr>
<tr>
<td>Jonas Alexander</td>
<td>Developer</td>
<td>San Francisco</td>
<td>30</td>
<td>2010/07/14</td>
<td>$86,500</td>
</tr>
<tr>
<td>Shad Decker</td>
<td>Regional Director</td>
<td>Edinburgh</td>
<td>51</td>
<td>2008/11/13</td>
<td>$183,000</td>
</tr>
<tr>
<td>Michael Bruce</td>
<td>Javascript Developer</td>
<td>Singapore</td>
<td>29</td>
<td>2011/06/27</td>
<td>$183,000</td>
</tr>
<tr>
<td>Donna Snider</td>
<td>Customer Support</td>
<td>New York</td>
<td>27</td>
<td>2011/01/25</td>
<td>$112,000</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
</table>
</div>
</body>
</html>
You can adjust the style to meet your specific needs.
I am using bootstrap-table and I would like to color certain columns red or green based on the value that they are having.
I am having the following table:
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
<link href="https://unpkg.com/bootstrap-table#1.18.3/dist/bootstrap-table.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>
<script src="https://unpkg.com/bootstrap-table#1.18.3/dist/bootstrap-table.min.js"></script>
<h2>Products</h2>
<div class="table-responsive">
<table id="table" data-toggle="table" data-height="460" data-page-size="10" data-pagination="true" class="table table-striped table-hover">
<thead>
<tr>
<th data-field="trx_date" scope="col">Transaction Date</th>
<th data-field="order_type" scope="col">Buy/Sell</th>
<th data-field="total_trx" scope="col">Total Transaction</th>
<th data-field="SecInfo" scope="col">Details</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">8/18/2016</th>
<td>Sell</td>
<td>$95,001,513.81</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td>Purchase</td>
<td>$95,001,513.81</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td>Purchase</td>
<td>$95,001,513.81</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td>Purchase</td>
<td>$95,001,513.81</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td>Sell</td>
<td>$95,001,513.81</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td>Sell</td>
<td>$95,001,513.81</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td>Purchase</td>
<td>$95,001,513.81</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td>Purchase</td>
<td>$95,001,513.81</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td>Sell</td>
<td>$95,001,513.81</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td>Sell</td>
<td>$95,001,513.81</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td>Sell</td>
<td>$95,001,513.81</td>
<td>Details</td>
</tr>
</tbody>
</table>
</div>
Is there a way to style each row like the following:
I tried to set a data-element on my tr-tag however, as I have many tables that need to get styled ist there an easier way with css?
I appreciate your replies!
Is this What you need?
The reason for using class instead of manually telling to check 'nth' cell is because, May be in future the order column in table or the number of column in the table may vary, so its recommenced to use a selector to implement this
$('.trans').each(function() {
if (parseFloat($(this).text().replace(/\$|,/g, '')) <= 100000000.00) {
$(this).parent().css({
'background-color': '#89e289'
})
} else {
$(this).parent().css({
'background-color': '#f08080'
})
}
});
//If you want to check Sell/Product cell do:
$('.sales').each(function(){
if($(this).text() == 'Sell'){
$(this).parent().css({'background-color' : '#89e289'})
}
else{
$(this).parent().css({'background-color' : '#f08080'})
}
});
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
<link href="https://unpkg.com/bootstrap-table#1.18.3/dist/bootstrap-table.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>
<script src="https://unpkg.com/bootstrap-table#1.18.3/dist/bootstrap-table.min.js"></script>
<h2>Products</h2>
<div class="table-responsive">
<table id="table" data-toggle="table" data-height="460" data-page-size="10" data-pagination="true" class="table table-striped table-hover">
<thead>
<tr>
<th data-field="trx_date" scope="col">Transaction Date</th>
<th data-field="order_type" scope="col">Buy/Sell</th>
<th data-field="total_trx" scope="col">Total Transaction</th>
<th data-field="SecInfo" scope="col">Details</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">8/18/2016</th>
<td class="sales">Sell</td>
<td class="trans">$195,001,513.81</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td class="sales">Purchase</td>
<td class="trans">$95,001,513.81</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td class="sales">Purchase</td>
<td class="trans">$95,001,513.81</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td class="sales">Purchase</td>
<td class="trans">$95,001,513.81</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td class="sales">Sell</td>
<td class="trans">$195,001,513.81</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td class="sales">Sell</td>
<td class="trans">$195,001,513.81</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td class="sales">Purchase</td>
<td class="trans">$95,001,513.81</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td class="sales">Purchase</td>
<td class="trans">$95,001,513.81</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td class="sales">Sell</td>
<td class="trans">$195,001,513.81</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td class="sales">Sell</td>
<td class="trans">$195,001,513.81</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td class="sales">Sell</td>
<td class="trans">$195,001,513.81</td>
<td>Details</td>
</tr>
</tbody>
</table>
</div>
With your current HTML, you could use something like this to get the value of the appropriate cell, reformat the string into a float, and change the background color based on this value.
$('#table tr').each(function () {
let value = this.children[2].innerHTML;
// omit dollar signs and commas with replace and return a float
value = parseFloat(value.replace(/\$|,/g, ''));
// compare values and adjust styling
value > 90000000.50 ? this.style.backgroundColor = 'lightgreen' : '';
value < 10000000.75 ? this.style.backgroundColor = 'salmon' : '';
});
Unfortunately you cannot set base color or styles of any elements in your HTML, depending on the content they have with CSS. This is something you must look over with javascript.
Why this? Because CSS is a frontend styling "modifier", that edits property of HTML without looking at its content and what there is written. It simply applies styles. With JavaScript however, you can edit the content by taking it, analyzing it, and changing its style.
I took your code and edited a little the content of it to modify and show you how you can deal with this in javascript. You can do that in more efficient ways but I went for simplicity and the DOM to make you understand at best
for (let i = 0; i < 11; i++) {
let price = document.getElementsByClassName('price')[i].innerText;
console.log(price)
if (price <= 100){
document.getElementsByClassName('price')[i].style.backgroundColor = 'red'
} else if (price > 100) {
document.getElementsByClassName('price')[i].style.backgroundColor = 'blue'
}
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>
<h2>Products</h2>
<div class="table-responsive">
<table id="table" data-toggle="table" data-height="460" data-page-size="10" data-pagination="true" class="table table-striped table-hover">
<thead>
<tr>
<th data-field="trx_date" scope="col">Transaction Date</th>
<th data-field="order_type" scope="col">Buy/Sell</th>
<th data-field="total_trx" scope="col">Total Transaction</th>
<th data-field="SecInfo" scope="col">Details</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">8/18/2016</th>
<td>Sell</td>
<td class="price">80</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td>Purchase</td>
<td class="price">90</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td>Purchase</td>
<td class="price">700</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td>Purchase</td>
<td class="price">900</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td>Sell</td>
<td class="price">500</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td>Sell</td>
<td class="price">200</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td>Purchase</td>
<td class="price">300</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td>Purchase</td>
<td class="price">100</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td>Sell</td>
<td class="price">100</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td>Sell</td>
<td class="price">100</td>
<td>Details</td>
</tr>
<tr>
<th scope="row">8/18/2016</th>
<td>Sell</td>
<td class="price">100</td>
<td>Details</td>
</tr>
</tbody>
</table>
</div>
I just want to apply table-stripped class to my 3 colums. Totally I have 5 colums, but 3 of them needs to be stripped. Any idea for that?
For example, I only want to apply table stripped to selected area below:
You can override first td element with style="background-color: white;"
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<table class="table table-condensed table-striped">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td style="background-color: white;">John</td>
<td>Doe</td>
<td>john#example.com</td>
</tr>
<tr>
<td>Mary</td>
<td>Moe</td>
<td>mary#example.com</td>
</tr>
<tr>
<td style="background-color: white;">July</td>
<td>Dooley</td>
<td>july#example.com</td>
</tr>
</tbody>
</table>
</div>
I'm aware of table-responsive class but it's not working here and it's breaking the UI.
My code looks like this:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<table id="home-table">
<tr>
<td class='home-cell'>
<table class="table table-bordered">
<caption>Overview</caption>
<thead>
<tr>
<th>Device ID</th>
<th>Last Reading</th>
</tr>
</thead>
<tbody>
<tr>
<td>Demo 1</td>
<td>Reading: R1</td>
</tr>
</tbody>
</table>
</td>
<td class='home-cell'>
<table class="table table-bordered">
<caption>Alarms and Schedules</caption>
<thead>
<tr>
<th>Device ID</th>
<th>Details</th>
</tr>
</thead>
<tbody>
<tr>
<td>Dev 1</td>
<td>Scheduled 10:00 AM OFF</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
How can I achieve separate responsive horizontal scrollbars in both the tables inside the main table?
you can do it like this
<style>
.home-cell.table-responsive {
width: 150px;
overflow: auto !important;
display: inline-block;
}
table .table-bordered {
width: 190px;
min-width: 270px;
}
</style>
<table id="home-table">
<tbody>
<tr>
<td class="home-cell table-responsive">
<table class="table table-bordered">
<caption>Overview</caption>
<thead>
<tr>
<th>Device ID</th>
<th>Last Reading</th>
</tr>
</thead>
<tbody>
<tr>
<td>Demo 1</td>
<td>Reading: R1</td>
</tr>
</tbody>
</table>
</td>
<td class="home-cell table-responsive">
<table class="table table-bordered">
<caption>Alarms and Schedules</caption>
<thead>
<tr>
<th>Device ID</th>
<th>Details</th>
</tr>
</thead>
<tbody>
<tr>
<td>Dev 1</td>
<td>Scheduled 10:00 AM OFF</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
Try this, This will work
<div class="container">
<div class="row">
<div class="col-xs-12">
<div class="table-responsive">
<table class="table table-bordered">
<th colspan="3">Outer Table</th>
<tr>
<td>This is row one, column 1</td>
<td>This is row one, column 2</td>
<td>
<table class="table table-bordered">
<th colspan="3">Inner Table</th>
<tr>
<td>This is row one, column 1</td>
<td>This is row one, column 2</td>
<td>This is row one, column 3</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
https://jsfiddle.net/DTcHh/38830/
I have a responsive-table within a td with a colspan. Instead of the responsive-table limiting the width and creating a scroll bar for the inner table, it stretches the table to be too large for the screen. Is there any css that can be added to make this work other than setting an explicit width on the responsive-table div?
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div style="width: 50px">
<table class="table table-striped table-bordered">
<tbody>
<tr>
<td>Column 1</td>
<td>Column 2</td>
</tr>
<tr>
<td colspan="2">
<div class="table-responsive">
<table class="table table-bordered">
<tbody>
<tr>
<td>Test1</td>
<td>Test2</td>
<td>Test3</td>
<td>Test4</td>
<td>Test5</td>
<td>Test6</td>
<td>Test7</td>
<td>Test8</td>
<td>Test9</td>
<td>Test10</td>
<td>Test11</td>
<td>Test12</td>
<td>Test13</td>
<td>Test14</td>
<td>Test15</td>
<td>Test16</td>
<td>Test17</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
I am expecting it to look like this, without needing to set an explicit width on the div inside the td.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div style="width: 50px">
<table class="table table-striped table-bordered">
<tbody>
<tr>
<td>Column 1</td>
<td>Column 2</td>
</tr>
<tr>
<td colspan="2">
<div class="table-responsive" style="width: 50px">
<table class="table table-bordered">
<tbody>
<tr>
<td>Test1</td>
<td>Test2</td>
<td>Test3</td>
<td>Test4</td>
<td>Test5</td>
<td>Test6</td>
<td>Test7</td>
<td>Test8</td>
<td>Test9</td>
<td>Test10</td>
<td>Test11</td>
<td>Test12</td>
<td>Test13</td>
<td>Test14</td>
<td>Test15</td>
<td>Test16</td>
<td>Test17</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
Try this. Im not sure if you need it to be 50px wide if so just change style="width: 100%" to style="width: 50px%".
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div class="table-responsive" style="width: 100%">
<table class="table table-striped table-bordered">
<tbody>
<tr>
<th scope="row">1</th>
<td>Test1</td>
<td>Test2</td>
<td>Test3</td>
<td>Test4</td>
<td>Test5</td>
<td>Test6</td>
<td>Test7</td>
<td>Test8</td>
<td>Test9</td>
<td>Test10</td>
<td>Test11</td>
<td>Test12</td>
<td>Test13</td>
<td>Test14</td>
<td>Test15</td>
<td>Test16</td>
<td>Test17</td>
</tr>
</tbody>
</table>
</div>