Given the following table/code, I'd like to add or adjust three items. I don't know Jquery well enough as I just learn programming within two months and this code was refer from other places and I made some adjustment for it. Anyone who could help me I will be very appreciate, I have try this expand and collapse function for two weeks but I still cannot get the result that I want so I post here to ask for help.
Here are the three items:
For the parent that do not have child will not show '+' when I click on the hide all, for example in my fiddle, the capital share and capital contribution does not have child below it but it still appear '+'. Please see the image attached
[Current Result & Expected Result]
When I show all, I want to show '-' for the parent or child that able to collapse as a group. Please see the image attached
[Expected Output Result]
After I click Hide All, when I click expand the Fixed Assets, It should only appear Building and Computer, not including the child of Building and Computer. When I click expand on the building, then it only expand the accum. dprn building. Please see the image attached [Expected Result]
Here is the table I'm using:
$('.collapse').on('click', function () {
//console.log($(this).attr('data-depth'));
var findChildren = function (tr) {
var depth = tr.data('depth');
return tr.nextUntil($('tr').filter(function () {
return $(this).data('depth') <= depth;
}));
};
var children = findChildren($(this));
if ($(children).is(':visible')) {
$(this).addClass("closed");
$(children).hide();
} else {
$(this).removeClass("closed");
$(children).show();
var children = findChildren($(".closed"));
$(children).hide();
}
});
$('#show_all').on('click', function () {
$("#mytable tr.collapse").removeClass("closed").show();
});
$('#hide_all').on('click', function () {
$("#mytable tr.collapse:not([data-depth='0'])").hide();
$("#mytable tr.collapse.level0").addClass("closed");
});
table td,
th {
border: 1px solid #eee;
font-family: Arial;
font-size: 16px;
}
.level0 td:first-child {
padding-left: 10px;
}
.level1 td:first-child {
padding-left: 40px;
}
.level2 td:first-child {
padding-left: 70px;
}
.level3 td:first-child {
padding-left: 100px;
}
.level4 td:first-child {
padding-left: 130px;
}
.level5 td:first-child {
padding-left: 160px;
}
.closed td:first-child::before {
content: "+";
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<button id="show_all"><p>Show all</p></button>
<button id="hide_all"><p>Hide all</p></button>
<table class="table table-bordered" id="mytable" style="border-collapse: collapse">
<thead>
<tr>
<th colspan="5">Name</th>
<th>DR</th>
<th>CR</th>
</tr>
<tr data-depth="0" class="collapse level0">
<td colspan="5">CAPITAL SHARE</td>
<td>10,000</td>
<td>0.00</td>
</tr>
<tr data-depth="0" class="collapse level0">
<td colspan="5">CAPITAL CONTRIBUTION </td>
<td>200,000</td>
<td>12,000</td>
</tr>
<tr data-depth="0" class="collapse level0">
<td colspan="5">FIXED ASSETS</td>
<td>200,320</td>
<td>0.00</td>
</tr>
<tr data-depth="1" class="collapse level1">
<td colspan="5">BUILDING</td>
<td>3,222,000</td>
<td>0.00</td>
</tr>
<tr data-depth="2" class="collapse level2">
<td colspan="5">ACCUM. DPN-BUILDING</td>
<td>0.00</td>
<td>0.00</td>
</tr>
<tr data-depth="1" class="collapse level1">
<td colspan="5">COMPUTER</td>
<td>320,000</td>
<td>0.00</td>
</tr>
<tr data-depth="2" class="collapse level2">
<td colspan="5">ACCUM. DPN-COMPUTER</td>
<td>0.00</td>
<td>0.00</td>
</tr>
<tr data-depth="3" class="collapse level3">
<td colspan="5">ACCUM. DPN-COMPUTER</td>
<td>0.00</td>
<td>0.00</td>
</tr>
<tr data-depth="3" class="collapse level3">
<td colspan="5">ACCUM. DPN-COMPUTER</td>
<td>0.00</td>
<td>0.00</td>
</tr>
<tr data-depth="0" class="collapse level0">
<td colspan="5">INVESTMENT</td>
<td>0.00</td>
<td>0.00</td>
</tr>
<tr data-depth="1" class="collapse level1">
<td colspan="5">INVESTMENT IN ABC COMPANY</td>
<td>0.00</td>
<td>0.00</td>
</tr>
</thead>
</table>
You are missing one thing here. When you are adding a ".closed" class on collapse you are adding it to all the "#mytable tr.collapse.level0". Instead, write an (if statement) and only add ".closed" class to those elements who have a sibling whose class contains ".level1".
$("#hide_all").on("click", function() {
$("#mytable tr.collapse:not([data-depth='0'])").hide();
let a = document.querySelectorAll("#mytable tr.collapse.level0");
for (const iterator of a) {
if (iterator.nextSibling.nextSibling) {
if (iterator.nextSibling.nextSibling.className.includes("level1")) {
$(iterator).addClass("closed");
}
}
}
})
At the end, I finally found a plugins of treegrid that is more easily for me to use and understand. Anyone who need to use treegrid or treeview of table can use the plugins.
TreeGrid Plugins. If anyone want to use my above sample also can, but the disadvantage is the page will die when load data >300rows.
Related
How to set table row background-color with css pseudo class :has() only when button clicked and class "group-process" added to each row in tbody?
When I click button with id "group_processing_button" displays block with class "group-processing". Also when I click this button only rows which have class i[class$="fa-check-square"] must have background-color: #9e9; I mean this css style must work:
.table tbody tr[class$="group-process"]:has(td.no):has(i[class$="fa-check-square"]) {
background-color: #9e9;
}
Only when block with class "group-processing" displayed by button.
When I click and display off the block with class "group-processing", rows which have class i[class$="fa-check-square"] must have background-color: transparent, this style must not work:
.table tbody tr[class$="group-process"]:has(td.no):has(i[class$="fa-check-square"]) {
background-color: #9e9;
}
Thank You for help!
var $groupProcessingContainer = jQuery('.group-processing');
var $groupProcessingTable = jQuery('.table-order');
jQuery('#group_processing_button').click(function() {
var $rows = $groupProcessingTable.find('tbody tr');
$rows.each(function() {
var $row = jQuery(this);
if ($row.hasClass('group-process')) {
$row.removeClass('group-process');
} else {
$row.addClass('group-process');
}
});
$groupProcessingContainer.slideToggle();
});
tr td.no .group-check {
display: none;
}
tr.group-process td.no .group-check {
display: inline;
}
.table tbody tr[class$="group-process"]:has(td.no):has(i[class$="fa-check-square"]) {
background-color: #9e9;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<button type="button" class="btn btn-sm btn-info" id="group_processing_button">Group processing</button>
<div class="group-processing" style="display: none;">Block for filtering</div>
<table class="iblock table table-striped table-bordered table-hover table-order">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Supplier</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td class="no">1.<i class="fa group-check fa-check-square"></i></td>
<td>
item_name
</td>
<td>Supplier_name</td>
</tr>
<tr class="even">
<td class="no">1.<i class="fa fa-square-o group-check"></i></td>
<td>
item_name
</td>
<td>Supplier_name</td>
</tr>
</tbody>
</table>
I am trying to auto increment column with header ID, using reactjs bootstrap5.
The table loads data from a database.
<table className="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Tick</th>
<th>Percent</th>
<th>Ratio</th>
<th>Broke</th>
<th />
</tr>
</thead>
<tbody>
{ this.props.sett_list.map(dca_sett_list_item => (
<tr key={sett_list_item.id}>
<td>{sett_list_item.id}</td>
<td>{sett_list_item.ticker}</td>
<td>{sett_list_item.percent_down}</td>
<td>{sett_list_item.pe_ratio}</td>
<td><button className='btn btn-primary btn-sm'>BROKE</button></td>
<td><button onClick={this.props.deleteSettings_T.bind(this, sett_list_item.id)}className='btn btn-danger btn-sm'>Delete</button></td>
</tr>
)) }
</tbody>
</table>
I found an answer which directs towards using css:
table {
counter-reset: tableCount;
}
.counterCell:before {
content: counter(tableCount);
counter-increment: tableCount;
}
Do I make a custom.css and add the above code to get the functionality? Or is there a better way to do this in bootstrap 5?
I have a table. The only CSS I am adding to the standard Bootstrap library is the following to implement the sticky header:
.table-sticky th {
background: #fff;
position: sticky;
top: -1px;
z-index: 990;
}
<div class="table-responsive p-0 mt-2">
<table class="table table-sm table-striped table-hover table-sticky">
<thead>
<tr>
<th>#</th>
<th>Header</th>
<th>Header</th>
<th>Header</th>
<th>Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>1,001</td>
<td>Lorem</td>
<td>ipsum</td>
<td>dolor</td>
<td>sit</td>
</tr>
<tr>
<td>1,002</td>
<td>amet</td>
<td>consectetur</td>
<td>adipiscing</td>
<td>elit</td>
</tr>
</tbody>
</table>
</div>
The sticky header worked until I wrapped the table in the div.table-responsive. How do I allow these to co-exist?
There's no way to co-exist a position: sticky inside a .table-responsive without re-implementing the latter.
My solution ended up to be using .table-responsive-sm to refuse this trade-off when a responsive table is certainly not needed.
In this case you need to code it yourself. Following JS will do the job:
var element = document.getElementById("fixed-thead");
var parentElement = element.parentElement;
window.addEventListener('scroll', () => {
var coordinates = parentElement.getBoundingClientRect();
if (coordinates.y < 0) {
element.style.transform = 'translate3d(0, ' + (-coordinates.y) + 'px, 0)';
} else {
element.style.transform = 'translate3d(0,0,0)';
}
});
And in your table mark thead with id="fixed-thead" like this:
<thead id="fixed-thead">
I use translate3d to enable hardware acceleration to make it smoother. If user uses wheel to scroll scroll event is triggering very ofter I recommend using debouncing of scroll events.
In case your table has column with fixed with you can use setting position: fixed and top: 0 instead of translate3d. But in case of a variable length translate3d is preferred.
In case you need to apply this code to multiple pages on page use following code:
function applyFixedHeader(element) {
var parentElement = element.parentElement;
window.addEventListener('scroll', () => {
var coordinates = parentElement.getBoundingClientRect();
if (coordinates.y < 0) {
element.style.transform = 'translate3d(0, ' + (-coordinates.y) + 'px, 0)';
} else {
element.style.transform = 'translate3d(0,0,0)';
}
});
}
var elements = document.querySelectorAll("{yourSelector, e.g. `.table-fixed` for the tables containing class `table-fixed`}");
elements.forEach(element => applyFixedHeader(element));
without any additionnal CSS, you can fix table head by removing table-responsive from the div and adding class="bg-white sticky-top border-bottom" class to table tag. Like this
<div class="table">
<table class="table">
<thead>
<tr>
<th class="bg-white sticky-top border-bottom">FirstName</th>
<th class="bg-white sticky-top border-bottom">LastName</th>
<th class="bg-white sticky-top border-bottom">Class</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Cooker</td>
<td>A1</td>
</tr>
<tr>
<td>David</td>
<td>Jeferson</td>
<td>A2</td>
</tr>
...
</table>
</div>
On my website, I have a class to change font and text size named class='q'. the class will not work even though I copy and pasted this from the home page as It is menu bar for all pages. All other classes work but not this one, Why?
th.q {
font-family: avenir;
font-size: 25px;
}
<BODY>
<TABLE class='a' border='0' width='100%'>
<TR height='20%'>
<TH class='q'><a class="hover">About</a></TH>
<TH class='q'><a>Products</a></TH>
<TH class='q'><a>Pricing</a></TH>
<TH class='q'><a>Contact</a></TH>
</TR>
</TABLE>
(Referring to your answer:)
Unfortunately, giving multiple elements the same id invalidates your HTML. If the #font selector was enough to fix the problem, you should be able to give the <table> an id instead and select its <th> descendants:
#navigation th {
font-size: 25px;
}
<TABLE id='navigation' class='a' border='0' width='100%'>
<TR height='20%'>
<TH><a class="hover">About</a></TH>
<TH><a>Products</a></TH>
<TH><a>Pricing</a></TH>
<TH><a>Contact</a></TH>
</TR>
</TABLE>
Also, the border, width, and height attributes should be in CSS, not HTML:
#navigation th {
font-size: 25px;
}
#navigation {
border: none;
width: 100%;
}
#navigation tr {
height: 20%;
}
<TABLE id='navigation' class='a'>
<TR>
<TH><a class="hover">About</a></TH>
<TH><a>Products</a></TH>
<TH><a>Pricing</a></TH>
<TH><a>Contact</a></TH>
</TR>
</TABLE>
Of course, you shouldn't be using tables for layout, anyway.
Since nothing else works I have used Ids to fix it.
HTML
<BODY>
<TABLE class='a' border='0' width='100%'>
<TR height='20%'>
<TH id='font'><a class="hover">About</a></TH>
<TH id='font'><a>Products</a></TH>
<TH id='font'><a>Pricing</a></TH>
<TH id='font'><a>Contact</a></TH>
</TR>
</TABLE>
CSS
#font {
font-family: avenir;
font-size: 25px;
}
I have looked through all the answers posted but I cant seem to get this code to work correctly. I am trying to get the individual cell to change color when I hover over it, but I don't have access to the .css with the service we are using. I am being forced to drop an HTML code box that I can paste my code into specific to the element I am changing, but not the entire .css file...just that element.
Here is my code. Any help in getting the background to change to #ff0000 and the Text to change to #000000 when I roll over the cell would be greatly appreciated.
(It is ultimately my intent to add a >a href for each of the cells as well, but I am trying to do this one step at a time. The >a href will (I hope) add the selected cell to a shopping cart.)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
<title></title>
<style type="text/css">
body {
background: #000;
}
#wrap {
margin: 0 auto; /* margin 0 auto will center that box in your document */
width: 780px; /*size of your box*/
background: #000;
text-align: center; /* everything will be written in that box will be centered horizontally*/
}
</style>
<div id="wrap">
<table width="780">
<tr>
<td align="center">
<table border=1>
<tbody>
<!-- Results table headers -->
<tr>
<th>Messages Per Month</th>
<th>1 Month Pricing</th>
<th>3 Month Pricing</th>
<th>12 Month Pricing</th>
</tr>
<tr>
<td>500</td>
<td>$14.95/Month</td>
<td>$12.95/Month</td>
<td>$9.95/Month</td>
</tr>
<tr>
<td>1,000</td>
<td>$24.95/Month</td>
<td>$20.95/Month</td>
<td>$17.95/Month</td>
</tr>
<tr>
<td>1,500</td>
<td>$37.95/Month</td>
<td>$31.95/Month</td>
<td>$26.95/Month</td>
</tr>
<tr>
<td>2,000</td>
<td>$49.95/Month</td>
<td>$41.95/Month</td>
<td>$35.95/Month</td>
</tr>
<tr>
<td>2,500</td>
<td>$62.95/Month</td>
<td>$52.95/Month</td>
<td>$44.95/Month</td>
</tr>
<tr>
<td>5,000</td>
<td>$119.95/Month</td>
<td>Not Available</td>
<td>Not Available</td>
</tr>
<tr>
<td>7,500</td>
<td>$179.95/Month</td>
<td>Not Available</td>
<td>Not Available</td>
</tr>
<tr>
<td>10,000</td>
<td>$219.95/Month</td>
<td>Not Available</td>
<td>Not Available</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</div>
In CSS:
td:hover {
background-color: #ff0000;
color: #000000;
}
Or you can use JavaScript/jQuery:
$(document).ready(function() {
$("td").hover(
function() {
$(this).css('background-color', '#ff0000');
$(this).css('color', '#000000');
},
function() {
$(this).css('background-color', 'none');
$(this).css('color', 'black'); // or whatever your original color was
}
);
});
Wrap the cell data with a div (class="cell_hvr") and a link around the content.
.cell_hvr {
padding: 2px;
width: 100%;
height: 100%;
}
.cell_hvr a {
display: block;
text-decoration: none;
}
.cell_hvr a:hover {
background-color: red;
}
<div class="cell_hvr"> (Your content) </div>
You can do this by giving each cell a class
<td class="a">..</td>
and then styling it
<style>
td.a { background-color:#ff0000; }
td.a:hover { background-color:#000000; }
</style>
CSS: td:hover, th:hover { background:#ff0000; color:#000; }
I was looking for the JavaScript version of the answer to this question. But, I am not using JQuery..
I have
oCell = newRow.insertCell();
oCell.style.background = tintTest(myCounts[i]);
myCounts is just an array holding an Int..
but now i just wanted to add the hover background color to my current oCell before smashing it in line and moving on with the loop......
So I found the code below as the only checked answer in a stream of 'can't be done'.. just not sure if this would even help me.
var css = 'table td:hover{ background-color: #00ff00 }';
var style = document.createElement('style');
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
document.getElementsByTagName('head')[0].appendChild(style);
EDIT!!! I just found out my issue (I hope the code snip helps with yours still) I tried to add the hover in CSS and it just isn't supported at all.