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?
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>
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.
I'm trying to export a painted html table completely dynamically into a csv file. I tried to implement several Scripts, functions.etc but I have not obtained the desired result. If anyone can help me, I'd be very grateful.
The information in the table is loading from a Request in "jsonData", It is important to clarify that I do not require to know the "Headers", only the information inside the table body:
ts Code:
jsonData:any;
_object = Object;
"jsonData" looks like:
[{
Subject_ID: "ACCT101",
First_Available_Date: "01/01/01",
....
},
{
Subject_ID: "510862185-X",
First_Available_Date: "06/04/19"..}....
]
HTML
<table>
<thead>
<tr>
<th *ngFor="let header of _object.keys(jsonData[0]); let i = index">{{header}}</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let row of jsonData; let i = index">
<td *ngFor="let objKey of _object.keys(row); let j = index">{{ row[objKey] }} </td>
</tr>
</tbody>
</table>
<button class="btn btn-submit btn-sm float-right" type="submit" (click)="export()">
Submit
</button>
Please try below code. this will extract data from table and download as csv.
<!DOCTYPE html>
<html>
<head>
<style>
td, table,th{
border: solid 1px black;
}
</style>
</head>
<body>
<table id="data_table">
<thead>
<tr>
<th>Subject_ID</th>
<th>First_Available_Date</th>
</tr>
</thead>
<tbody>
<tr>
<td>ACCT101</td>
<td>01/01/01</td>
</tr>
<tr>
<td>510862185-X</td>
<td>06/04/19</td>
</tr>
<tr>
<td>New data</td>
<td>New data 1</td>
</tr>
</tbody>
</table>
<script>
function exportit() {
var csv='';
table=document.getElementById("data_table");
tr=table.children[0].children[0];
for(i=0;i<tr.children.length;i++)
{
csv+=tr.children[i].innerText+",";
}
csv=csv.substring(0,csv.length-1)+"\n";
tbody=table.children[1];
for(i=0;i<tbody.children.length;i++)
{
for(j=0;j<tbody.children[i].children.length;j++)
{
csv+=tbody.children[i].children[j].innerText+",";
}
csv=csv.substring(0,csv.length-1)+"\n";
}
csv=csv.substring(0,csv.length-1)+"\n";
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);
hiddenElement.target = '_blank';
hiddenElement.download = 'data.csv';
hiddenElement.click();
}
</script><br>
<button onclick="exportit()">export</button>
</body>
</html>
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>
Link
Heres the website containing the full html rendered Link
Here's my code:
<template name="activeOrderTable"><table class="table">
<thead>
<tr>
<th>Commodity</th>
<th>Quantity</th>
<th>Price (High Quality Metal)</th>
<th>Order Type</th>
<th>Merchant</th>
<th>Location</th>
<th>Date Added</th>
</tr>
</thead>
<tbody>
{{#each activeOrders}}
<tr>
<td>{{prettifyCommodity commodity}}</td>
<td>{{quantity}}</td>
<td>{{price}}</td>
<td>{{prettifyOrderType orderType}}</td>
<td>{{merchant}}</td>
<td>{{location}}</td>
<td>{{prettifyDate createdAt}}</td>
<td><button type="button" class="btn btn-danger btn-xs" data-id="{{_id}}"><span class="glyphicon glyphicon-remove-sign"></span></button></td>
</tr>
{{else}}
<h3>You have no active orders</h3>
{{/each}}
</tbody>
</table>
I would like to center the You have no active orders
I've tried class text-center but its not working.
Your H3 element is inside the table so the browser is trying to 'fix it'.
You should wrap your H3 tag in <tr><td colspan="6"><td></tr> tags and then apply class="text-center" to H3
Just add a text-center class on your h3 tag directly, not on it's parent divs like this:
{{else}}
<h3 class="text-center">You have no active orders</h3>
{{/each}}
Here's a jsfiddle with the above codes: http://jsfiddle.net/AndrewL32/65sf2f66/26/
You must try applying vanilla CSS to the above code.
table.table h3 {
text-align:center;
}