How to change table 'td' color based on key change in foreach loop - css

I am trying to set color when my $key value is changed. I mean I am generating a table when my $key is changing. Now I want to set a different bg-color when the $key is changing and a new table created. Now the <th colspan="5">{{$key}}</th> is changing and creating table for each $key I need to set color that $key is changing.
#foreach($lists as $key => $ser)
<table class="table table-striped table-bordered table-hover" id="sample_3">
<thead>
<tr>
<th colspan="5">{{$key}}</th>
</tr>
<tr>
<th> Destination</th>
<th> Services</th>
<th> Status</th>
<th> Time</th>
</tr>
</thead>
<tbody>
#foreach($ser as $s)
<tr>
<td style="background: rgb(176,224,230);"> TE 17 <br/>{{$s->sp}}</td>
<td> {{$s->dd}}</td>
<td> {{$s->ss}}</td>
<td> {{$s->dt}}</td>
</tr>
#endforeach
</tbody>
</table>
#endforeach

You can use a variable to store the value of $key in each loop. Then you can check to see if the $key is same as the previous one and change the color accordingly.
#php($prevvalue='')
#foreach($ser as $s)
<tr>
<td style="background: {{$s->key == $prevvalue ? 'oldcolor','newcolor'}}"> TE 17 <br/>{{$s->sp}}</td>
<td> {{$s->dd}}</td>
<td> {{$s->ss}}</td>
<td> {{$s->dt}}</td>
</tr>
#php($prevvalue = $s->key)
#endforeach

You can first add class from blade to group elements having similar value :
#php($prev = null)
#php($counter = 1)
#foreach($ser as $s)
#php
$counter = $s->key == $prev || is_null($prev) ? $counter : $counter + 1;
#endphp
<tr>
<td class="td_color_{{ $counter }}"> TE 17 <br/>{{$s->sp}}</td>
<td> {{$s->dd}}</td>
<td> {{$s->ss}}</td>
<td> {{$s->dt}}</td>
</tr>
#php($prev = $s->key)
#endforeach
This will give you following for the first column in each <tr> :
<td class="td_color_1">...</td>
<td class="td_color_1">...</td>
<td class="td_color_2">...</td>
<td class="td_color_2">...</td>
<td class="td_color_3">...</td>
<td class="td_color_4">...</td>
<td class="td_color_5">...</td>
Then in your css you have colors for these :
<style type="text/css">
td.td_color_1 { background: red; }
td.td_color_2 { background: blue; }
td.td_color_3 { background: green; }
td.td_color_4 { background: yellow; }
td.td_color_5 { background: pink; }
</style>

Related

CSS td conditional background color setting

Suppose I have the following angular table
<table>
<tr *ngFor="let company of investTable; let i = index">
<td>
{{company.name}}
</td>
<td>
{{company.price}}
</td>
<td>
{{company.profit}}
</td>
</tr>
</table>
How can I make a neat solution to have table cells have a background color depending on the cell value ? lets say if company profit is positive, make it green.
in css
td.highlight {
background-color: green;
}
in html :-
<table>
<tr *ngFor="let company of investTable; let i = index">
<td>
{{company.price}}
</td>
<td [ngClass]="{'highlight': company.profit > 0}">
{{company.profit}}
</td>
</tr>
</table>
You can add this to your html :
<td [style.positive]="company.profit > 0">
and this to your css :
td.positive{
background-color: green;
}
Look into ngStyle and ngClass. Simplest use case would be to directly bind to the style property.
<table>
<tr *ngFor="let company of investTable; let i = index">
<td>
{{company.name}}
</td>
<td>
{{company.price}}
</td>
<td [style.background-color]="company?.profit > 5 ? 'green' : 'red'">
{{company.profit}}
</td>
</tr>
</table>

highlighting cell when mouse hovers over it

I want the table cells to highlight/change background colour when the mouse hovers over them.
Here is the declaration of the styles
.cells{
border-style:inset;
border-width:1px;
border-color:#06F;
background-color:#D6D6D6;
font-style: italic;
}
.cells td:hover{
background-color: #FF0000;
Here is the creation of the table:
<table class="table">
<tr>
<td></td>
<th colspan="5" class="specialCell">Cost Per Person</td>
</tr>
<tr>
<th class="specialCell">Party Size</th>
<th class="headers"><?php echo $titles[0] ?></th>
<th class="headers"><?php echo $titles[1] ?></th>
<th class="headers"><?php echo $titles[2] ?></th>
<th class="headers"><?php echo $titles[3] ?></th>
<th class="headers"><?php echo $titles[4] ?></th>
</tr>
<?php
for ($y=0; $y<$rows_needed; $y++){
echo '<tr>';
echo '<th class="headers">'.$column1[$y].'</th>';
for ($i=0; $i<5; $i++){
$newValue = $column1[$y] * $titles[$i];
echo '<td class="cells">'.$newValue.'</td>'; //THIS IS WHERE THE CLASS IS CALLED
}
echo '</tr>';
}
?>
</table>
This however does not cause the table cell to change background colour when hovered over. Any ideas?
In your case .cells is the <td>. So your CSS should be:
.cells:hover{
background-color: #FF0000;
}
have you tried to add this style:
.headers:hover{
background-color: #FF0000;
}
Please try this snippet
.headers:hover {
background-color: tomato;
cursor: pointer;
}
<table class="table">
<tr>
<td></td>
<th colspan="5" class="specialCell">Cost Per Person</td>
</tr>
<tr>
<th class="specialCell">Party Size</th>
<th class="headers">A</th>
<th class="headers">B</th>
<th class="headers">C</th>
<th class="headers">D</th>
</tr>
</table>

Is there anyway to reorder of row <tr> in table by css?

I have a table with 3 row like below. How can I change the order of row by CSS?
Example:
Name: A B C
Age: 1 2 3
Country: US CA CN
And I want it become:
Country: US CA CN
Name: A B C
AGE: 1 2 3
<table class="details">
<tbody>
<tr class="name">
<td class="label"><label for="name">Fullname</label></td>
<td class="value name-wrapper">name</td>
</tr>
<tr class="Age">
<td class="label"><label for="age">Age</label></td>
<td class="value age-wrapper">26</td></tr>
<tr class="Country">
<td class="label"><label for="country">Country</label></td>
<td class="value country-wrapper">US</td></tr>
</tbody>
</table>
You can use flex-direction: column-reverse on parent elements to reverse the order of their children. Use flex-direction: row-reverse for rows.
tbody { display: flex; flex-direction: column-reverse; }
<table class="details">
<tbody>
<tr class="name">
<td class="label"><label for="name">Fullname</label></td>
<td class="value name-wrapper"></td>
</tr>
<tr class="Age">
<td class="label"><label for="age">Age</label></td>
<td class="value age-wrapper"></td>
</tr>
<tr class="Country">
<td class="label"><label for="country">Country</label></td>
<td class="value country-wrapper"></td>
</tr>
</tbody>
</table>
I think the only way to do this is to consider some visual hack by using transform. Make sure border-spacing is set to 0 to avoid complex calculation and be aware that this work fine if all the tr have the same height.
tr {
/*we move all the tr down by one row*/
transform:translateY(100%);
}
tr.country {
/*we move the country tr up by two rows*/
transform:translateY(-200%);
}
table {
border-spacing:0;
}
td {
padding:5px;
}
<table class="details">
<tbody>
<tr class="name">
<td class="label"><label for="name">Fullname</label></td>
<td class="value name-wrapper">name</td>
</tr>
<tr class="age">
<td class="label"><label for="age">Age</label></td>
<td class="value age-wrapper">26</td></tr>
<tr class="country">
<td class="label"><label for="country">Country</label></td>
<td class="value country-wrapper">US</td></tr>
</tbody>
</table>
You can do this with flex and order and position them however you want. Disclaimer, with this solution tr won't have display: table-row-group so you'll have to write additional css for positioning your cells:
tbody {
display:flex;
flex-direction: column;
}
tr:nth-child(2) {
order: -1;
}
tr:nth-child(1) {
order: 1;
}
<table class="details">
<tbody>
<tr class="name">
<td class="label"><label for="name">Fullname</label></td>
<td class="value name-wrapper">name</td>
</tr>
<tr class="Age">
<td class="label"><label for="age">Age</label></td>
<td class="value age-wrapper">26</td></tr>
<tr class="Country">
<td class="label"><label for="country">Country</label></td>
<td class="value country-wrapper">US</td></tr>
</tbody>
</table>
Updated added updated HTML.

Is it possible to fix <footer> in bootstraptable?

i found the showFooter Property. but i do not get how to customize the Content for that.
i only need to fill three of total 8 in ...
so table Looks basically like this:
<div class="table-responsive">
<table class="table table-bordered" id="tblKontoauszug">
<thead>
<tr>
<th data-sortable="#sortable" data-sorter="dateSorter">Buchungsdat.</th>
<th data-sortable="#sortable">Belegnr.</th>
<th data-sortable="#sortable">BA</th>
<th data-sortable="#sortable" data-sorter="betragSorter">Betrag</th>
<th data-sortable="#sortable">Buchungstext</th>
<th data-sortable="#sortable">Gegenkontoart</th>
<th data-sortable="#sortable">Gegenkonto</th>
<th data-sortable="#sortable">Bezeichnung</th>
</tr>
<tr class="info text-bold">
<td>
#if ( Model.Zeilen.Count() > 0 )
{
<span>#Model.Zeilen.Min(b => b.Buchungsdatum).ToShortDateString()</span>
}
</td>
<td colspan="2">Anfangsbestand</td>
<td class="text-right">#Model.Anfangsbestand.ToString("N")</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</thead>
<tfoot>
<tr class="info text-bold">
<td>
#if ( Model.Zeilen.Count() > 0 )
{
<span>#Model.Zeilen.Max( b => b.Buchungsdatum ).ToShortDateString()</span>
}
</td>
<td colspan="2">Endbestand</td>
<td class="text-right #( Model.Endbestand < 0 ? "negative" : "")">#Model.Endbestand.ToString( "N" )</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tfoot>
<tbody>
#foreach ( var zeile in Model.Zeilen )
{
<tr>
<td>#zeile.Buchungsdatum.ToShortDateString()</td>
<td>#zeile.Belegnummer</td>
<td title="#zeile.BelegartText">#zeile.Belegart</td>
<td class="text-right #( zeile.Betrag < 0 ? "negative" : "")">#zeile.Betrag.ToString("N")</td>
<td>#zeile.Buchungstext</td>
<td>#zeile.Gegenkontoart</td>
<td>#zeile.Gegenkonto</td>
<td>#zeile.Bezeichnung</td>
</tr>
}
</tbody>
</table>
</div>
You can customize content in the footer by adding the data-footer-formatter attribute to the column you want to add the footer to and setting that attribute equal to a defined javascript function. Here's an example:
HTML:
<table data-toggle="table" data-show-footer="true" data-footer-style="footerStyle" data-show-columns="true">
<thead>
<tr>
<th data-sortable="true" data-footer-formatter="totalText">Name</th>
<th data-sortable="true" data-footer-formatter="carsSum"># Cars</th>
</tr>
</thead>
<tbody>
<tr>
<td>Johnny</td>
<td>2</td>
</tr>
<tr>
<td>Zack</td>
<td>3</td>
</tr>
<tr>
<td>Timmy</td>
<td>2</td>
</tr>
</tbody>
</table>
Javascript:
function totalText(data) {
return 'TOTAL';
}
function carsSum(data) {
return '7';
}
function footerStyle(value, row, index) {
return {
css: { "font-weight": "bold" }
};
}
Notice I'm not using <tfoot> at all here. That is because <tfoot> won't work with the show columns dropdown list but this will (if I were to use the data-show-columns="true" attribute on the <table> element as I did in the example above). You can also style the footer using the data-footer-style attribute. In my example, I'm making all text in the footer bold.
You can see the code in action here: https://jsfiddle.net/3mou92px/9/

I want to page break in pdf reporting from mvc3 razor view to pdf

I am working in mvc3 razor view. I create pdf report from mvc3 razor view below is my controller code.
public ActionResult PReqDocPrint()
{
List<PReqPrintModel> Rec = new List<PReqPrintModel> { };
Rec = PReqPrint("", "", "");
return this.ViewPdf("Purchase Requisition", "PReqDocPrint", Rec);
}
I get data from database through stored procedure and return to controller
and my view code is given below.
#model IEnumerable<CBS.Models.PReqPrintModel>
#{
Layout = "~/Views/Shared/_LayoutForPrints.cshtml";
}
<table border="1" >
<tr>
<td>
<h2 style="text-align:center; font-family:Times New Roman;">Purchase Requisition</h2>
</td>
</tr>
</table>
<div style="color:Black;border-width:0px 1px 1px 0px;font-size:8px; font-weight:normal; page-break-before:always;">
#{
var OddColor = "#aaaaff";
var evenColor = "#EEFFFF";
var Odd = OddColor;
var eve = evenColor;
}
<table style="font-family:Times New Roman; font-size:small">
#foreach (var doc in Model.GroupBy(d => d.PReqNo))
{
foreach (var pre in doc)
{
<tr style="border:1;">
<td>
Purchase Requisition: #Html.Encode(pre.PReqNo)
</td>
<td>
Purchase Date: #String.Format("{0:dd/MM/yyyy}", #Html.Encode(pre.PReqDate.Date))
</td>
</tr>
<tr style="border:1;">
<td>
Store Location: #Html.Encode(pre.StName)
</td>
<td>
Department: #Html.Encode(pre.DeptName)
</td>
</tr>
<tr style="border:1;">
<td>
Remarks: #Html.Encode(pre.RefRemarks)
</td>
<td></td>
</tr>
}
<tr >
<td colspan="2">
<table border="1" style="color:Black;border-width:0px 1px 1px 0px;font-size:8px;font-family:Helvetica;font-weight:normal;">
<tr style="background-color:#7070a0; font-weight:bold; color:Black; border:1;" >
<td align="left">
Code
</td>
<td align="left">
Description
</td>
<td align="left">
Unit
</td>
<td align="left">
Part No
</td>
<td align="left">
Lot No
</td>
<td align="left">
Item Remarks
</td>
<td align="right">
Quantity
</td>
<td align="right">
Amount
</td>
</tr>
#foreach (var item in doc)
{
<tr bgcolor="#Odd">
<td align="left">
#Html.Encode(item.ItemCode)
</td>
<td align="left">
#Html.Encode(item.ItemDesc)
</td>
<td align="left">
#Html.Encode(item.Units)
</td>
<td align="left">
#Html.Encode(item.PartNo)
</td>
<td align="left">
#Html.Encode(item.LotNo)
</td>
<td align="left">
#Html.Encode(item.Remarks)
</td>
<td align="right">
#Html.Encode(item.QtyIN)
</td>
<td align="right">
#Html.Encode(item.Qty)
</td>
</tr>
}
if (Odd == OddColor)
{
Odd = evenColor;
}
else
{
Odd = OddColor;
}
}
</table>
</td>
</tr>
}
</table>
</div>
<div class="break"></div>
Below is my PDF result image which return from view
Here I want to start new PDF page When second Purchase Requisition no 2014030001 start. Any one please help me.
i already try inline CSS for page break or table page-break-before:always; and also try a separate div tag with page-break-before:always; but nothing obtain my required result.
Also it not apply separate style sheet formats on PDF view.

Resources