I am developing a website using ASP.NET c# using razor view engine. I am using a for loop to display rows from a database and display it in a html table. each row contains a variable called "requestStatus". the request status is either "approved", "rejected" or Pending. Is there a way i can change the bg color of the table row based on the requeststatus , so for example if the requeststatus is "pending" set the table row to yellow, if the request status is "approved" set table row bgcolor to green ?
any help would be much great !
the code i use display the table is below
<fieldset>
<legend>Your Leave Requests</legend>
<table border="1" width="100%">
<tr bgcolor="grey">
<th>Description</th>
<th>Leave Type</th>
<th>Start Date</th>
<th>End Date</th>
<th>Total days leave requested</th>
<th>Request Status</th>
</tr>
@foreach(var rows2 in rows1){
<tr>
<th>@rows2.description</th>
<th>@rows2.leaveType</th>
<th>@rows2.startDate.ToString("dd-MMMM-yyyy")</th>
<th>@rows2.endDate.ToString("dd-MMMM-yyyy")</th>
<th>@rows2.totalDays</th>
<th>@rows2.requestStatus</th>
</tr>
}
</table>
</fieldset>
In HTML, table background color is defined using Cascading Style Sheets (CSS). Specifically, you use the background-color property to define background color. You can apply this property against the whole table, a row, or a single cell.
Explanation: The background color of the table is set by the bgcolor="color" attribute to the <table> tag. The background color of a table row is set by using the <tr> tag. The background color of a cell in a table is set by using either in <td> or <th> tag.
Just use the requestStatus as you class name and assign styles as appropiate:
<style type="text/css">
.grey {
background-color:grey;
}
.approved {
background-color:green;
}
.rejected {
background-color:red;
}
.pending {
background-color:lime;
}
</style>
<fieldset>
<legend>Your Leave Requests</legend>
<table border="1" width="100%">
<tr class="grey">
<th>Description</th>
<th>Leave Type</th>
<th>Start Date</th>
<th>End Date</th>
<th>Total days leave requested</th>
<th>Request Status</th>
</tr>
@foreach (var rows2 in rows1)
{
<tr class="@rows2.requestStatus">
<td>@rows2.description</th>
<td>@rows2.leaveType</th>
<td>@rows2.startDate.ToString("dd-MMMM-yyyy")</th>
<td>@rows2.endDate.ToString("dd-MMMM-yyyy")</th>
<td>@rows2.totalDays</th>
<td>@rows2.requestStatus</th>
</tr>
}
</table>
</fieldset>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With