Search code examples
javascripttypescriptangular-materialangular7angular-components

How to apply red color font to specific column for dynamic data?


I work on angular 7 app I need to apply red color font to data content body to `columnname='onlineurl'.

but I don't know how to do that?

I show data dynamically meaning no fixed column or data and code below working without any issue.

My main thing I need is if columnname='onlineurl' make font color to content body data red.

my code as below data source on ts as below:

this._displayreport.GetReportDetailsPaging(this.searchData).subscribe((data: any[]) => {  

       this.reportdetailslist = data;  

       this.headerCols = Object.keys(data[0]);  

       data.forEach((item) =>{  
         let values = Object.keys(item).map((key)=> item[key])

         this.contentBody.push(values);  


  });  


     });

to get header without data I do as below:

<thead style="width: max-content">    
         <tr>    
           <th  *ngFor="let coln of headerCols">    

               {{coln}}    

           </th> 
 </tr>    


         </tr>   
</thead> 

to get content data without header I do

<tr *ngFor="let rep of contentBody">    

                <td *ngFor="let r1 of rep"><span>{{r1}}</span></td>    

                <td
</tr> 
Result Returned 
ReportId   onlineurl          reportdate
1222       localhost:5000/    12-12-2018
1255       localhost:7000/    12-01-2019
1230       localhost:9000/    12-12-2020

Solution

  • You can try adding class conditionally with ngClass like so:

    <td *ngFor="let r1 of rep">
        <span [ngClass]="{'text-red': r1.columnname=='onlineurl'}">{{r1}}</span>
    </td>
    

    And then use CSS to apply color:

    .text-red{
        color:red;
    }