Tips on customizing a row in a table

Having a small issue styling a row in my table. Essentially, the table consists of 4 rows.

a) If the data under column Title 5 is below 0, then the entire row should display in red color.

b) If the data under column Title 5 is equal to 17, then the complete row should be in orange color.

c) All other rows should NOT have any specific color.

Most of it is functioning correctly. However, I have encountered an issue:

If a row does not have any assigned color, I still want to ensure the check icon is green.

Here is the code that is currently working:

PLUNKER

<p-panel  class="a-panel-checkIn" header='Compliance Tracking' [toggleable]="true" [collapsed]="isPanelCollapsed" [style]="{ height: '100%' }">
  <p-table [value]="complianceTracking" [columns]="cols" selectionMode="single">
  <ng-template pTemplate="header" let-columns>
  <tr>
    <th *ngFor="let col of columns">
      {{col.header}}
    </th>
  </tr>
</ng-template>
<ng-template pTemplate="body" let-rowData let-columns="columns">
  <tr [ngClass]="rowData.field5 < 0 ? 'a-danger-row' : (rowData.field5 === 17) ? 'a-yellow-row' : 'a-green-row'">
    <td *ngFor="let col of columns" [ngClass]="rowData[col.field] === '' ? 'a-check-icon' : ''">
      {{rowData[col.field]}}
    </td>
  </tr>
  </ng-template>
  </p-table>
</p-panel>

Answer №1

If you're looking to customize the second ng-template in your app.template.html, try substituting the code below:

<ng-template pTemplate="body" let-rowData let-columns="columns">
  <tr [ngClass]="rowData.field5 < 0 ? 'a-danger-row' : (rowData.field5 === 17) ? 'a-yellow-row' : ''">
    <!-- <td *ngFor="let col of columns" [ngClass]="rowData[col.field] === '' ? 'a-check-icon' : ''"> -->
    <td *ngFor="let col of columns" [ngClass]="rowData[col.field] === '' ? ((rowData.field5 > 0 && rowData.field5 !== 17) ? 'a-check-icon a-green-check' : 'a-check-icon' ) : ''">
      {{rowData[col.field]}}
    </td>
  </tr>
</ng-template>

To enhance your styling, add this new rule in your styles.css:

.a-green-check {
   color: #A8CF45;
}

Check out the Plunkr for the live demo.

We hope this modification aligns with your requirements.

Answer №2

Revise your template with the following changes:

<ng-template pTemplate="body" let-rowData let-columns="columns">
  <tr [ngClass]="rowData.field5 < 0 ? 'a-danger-row' : (rowData.field5 === 17) ? 'a-yellow-row' : ''">
    <td *ngFor="let col of columns" [ngClass]="rowData[col.field] === '' ? 'a-check-icon' : ''" class="{{col.field=='field6' && rowData.field5!==17 && rowData.field5>0  ? 'a-green-row':''}}">
      {{rowData[col.field]}}
    </td>
  </tr>
</ng-template>

Result:

https://i.sstatic.net/E5PlD.png

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The ng-select dropdown is experiencing issues on the user interface following an update to the newest versions of Angular 6

Recently, I made the transition of my application from Angular 5 to Angular 6. As part of the update process, I also upgraded ng-select to version 2.4.2, the latest one available on npm/GitHub. However, after the upgrade, the dropdown functionality seems ...

How can TypeScript allow an argument to only accept keys that match another argument?

I'm currently developing a library that deals with linked lists. The current implementation is hardcoded to work with a list node type containing a "next" field that points to the next node of the same type. However, I am looking to make it more flexi ...

Styling Lists: How to Create a Second Line Indent with Counters

There seems to be an issue with the second line indent when using a custom counter. When attempting to use 'list-style-type: decimal', the numbering does not display as desired, for example: ol { counter-reset: ci-counter; list-style- ...

HTML links have been disabled

I have been making updates to the charity website I manage, heroinitiative.org. You can view the revamp progress here: (please note that this is not live code, it is simply for my boss to see the progress and does not need to be kept secret, hence why I a ...

What is the best way to horizontally center an image with a transparent background using CSS?

A search button triggers a processing gif image to appear in the center with a transparent background. The content of this function is described below. Thank you for all responses. .img-loader{ text-align: center; width: 50px; display: block; ma ...

Angular is unable to locate the control without a specified name attribute

This question seems to crop up several times every year, but I'm struggling to make it work. I need to dynamically create a formArray based on elements provided by an array. I've tried various approaches and I'm currently stuck. One of the s ...

Repositioning with Flexbox: shifting the middle element to a new line

I have a flex item that contains three divs. ┌────────────────────────────────────────┐ | WRAPPER | | ┌─────────┬─ ...

Is there a way to adjust paragraph sizes in CSS flexbox so they scale with the font size?

After spending hours trying to figure this out on my own, I finally caved and registered on StackOverflow. Despite Google providing plenty of related questions and examples, I still can't find a solution to my specific problem. The issue at hand is c ...

Moving the sidebar from the app component to a separate component in Angular results in a situation where the sidebar does not maintain full height when the page is scrolled down

Within my Angular application, I have implemented a sidebar as a separate component. Previously, the content of the sidebar was housed within the main app component's HTML page, and it functioned properly by taking up the full height of the page when ...

Chrome is where all the action happens, while Firefox prefers to keep things on the left

After a long break, I returned to designing my website and re-learning CSS. My approach involved creating a WordPress-integrated site with minimal HTML/PHP work, focusing more on utilizing the available CSS classes and IDs provided by a template theme (Bla ...

Focused Filtering in DataGrid Pagination

Seeking to adjust the font size of numerical values (10, 25, and 50 as shown in the screenshot below) within rows per page selection within a pagination section of a DataGrid component. https://i.sstatic.net/PnIDa.png After inspecting each number, it was ...

Guide to creating a PHP loop in the footer of a Magento website

Hey Developers! I have been using the following commands in footer.phtml to retrieve all my cms/blocks in the Magento's footer. <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('home')->toHtml() ...

The CanLoad guard does not permit access to the root route

My original idea was to create a project where the main website, located at "/", would only be loaded lazily after the user logs in. const routes: Routes = [ { path: '', canLoad: [AuthGuard], loadChildren: './home/home.module#HomeModule&a ...

Is there a way to inform TypeScript that an object can only return properties from values found within an array?

I am trying to ensure that the return object from a function in TypeScript only allows keys that correspond to string values present in an array passed as an argument to the function. The returned object should contain a subset of keys from a list of valid ...

Implementing a Typescript hook using useContext along with an uninitialized context object

I am currently attempting to develop a custom hook called useAuth in React 17 with TypeScript. While my solution is somewhat functioning, it requires the following syntax when utilizing the hook methods: const auth = useAuth(); // Do other stuff ...

What is the best way to align a value within CardActions in Material UI?

I'm currently facing an issue with my website design. Here's a snapshot of how it looks: https://i.sstatic.net/UuBJJ.png Additionally, here is the code snippet related to the problem: <CardActions> <Typography style={{ fon ...

Switch up the color of the following-mouse-div in real-time to perfectly complement the color that lies underneath it

I am trying to create a div that changes color based on the complementary color of whatever is underneath the mouse pointer. I want it to follow the mouse and dynamically adjust its color. This functionality is similar to what Gpick does: https://www.you ...

The type 'Observable<Student[]>' is lacking important properties such as id, first_name, last_name, English, and more within the type 'Student'

I have developed two components (table and form) and I am working on transferring data from the form component to the table component when editing. The code that I have written is currently throwing an error. However, when I check the console using console ...

Creating an object efficiently by defining a pattern

As a newcomer to Typescript (and Javascript), I've been experimenting with classes. My goal is to create an object that can be filled with similar entries while maintaining type safety in a concise manner. Here is the code snippet I came up with: le ...

What is the solution to adding values from a counter?

I am trying to create a JavaScript counter: function animateSun() { var $elie = $("#sun"); $({ degree: 0 }).animate({ degree: 360 }, { duration: 190999, easing: 'linear', step: function(val) { now = Math.round ...