如何在Angular中为mat-row分配不同的颜色?

huangapple go评论76阅读模式
英文:

How to assign different colour to mat-row in angular?

问题

以下是翻译好的内容:

在这里的情况是我想根据一些逻辑(例如列表的 orderId)为 mat 表的每一行分配颜色。对于 orderId <= 1000,颜色应为红色;对于 1000 < orderId <= 2000,颜色应为绿色;对于 orderId > 2000,颜色应为黄色。我尝试了以下方法,但无法实现期望的输出:

<mat-row *matRowDef="let row; columns: displayedColumns"
         (click)="highlightedRows.push(row)"
         [style.background]="highlightedRows.indexOf(row) != -1 ? backgroundColor : ''">
</mat-row>

将不胜感激地接受任何帮助。

英文:

The case here is I want to assign colour to each row of mat table based on some logic (for e.g orderId of the list) for orderId &lt;= 1000 colour should be red, for 1000 &lt; orderId &lt;= 2000, colour should be green and orderId &gt; 2000, colour should be yellow. I tried this but unable to achieve the desired output:

&lt;mat-row *matRowDef=&quot;let row; columns: displayedColumns&quot; 
         (click)=&quot;highlightedRows.push(row)&quot; 
         [style.background]=&quot;highlightedRows.indexOf(row) != -1 ? backgroundColor : &#39;&#39;&quot;&gt;
&lt;/mat-row&gt;

Any help will be appreciated.

答案1

得分: 1

&lt;mat-row *matRowDef=&quot;let row; columns: displayedColumns; let i = index&quot;         
         [style.background]=&quot;getBackgroundColour(i)&quot;&gt;
&lt;/mat-row&gt;
getBackgroundColour(i) {
    if(i &lt;= 1000) {
        return &#39;red&#39;
    }
    if(i &lt;= 2000) {
        return &#39;green&#39;
    }
    if(i &gt; 2000) {
        return &#39;yellow&#39;
    }
}
英文:
&lt;mat-row *matRowDef=&quot;let row; columns: displayedColumns; let i = index&quot;         
         [style.background]=&quot;getBackgroundColour(i)&quot;&gt;
&lt;/mat-row&gt;
getBackgroundColour(i) {
    if(i &lt;= 1000) {
        return &#39;red&#39;
    }
    if(i &lt;= 2000) {
        return &#39;green&#39;
    }
    if(i &gt; 2000) {
        return &#39;yellow&#39;
    }
}

答案2

得分: 0

Call a function in HTML:

&lt;mat-row *matRowDef="let row; columns: displayedColumns" (click)="highlightedRows.push(row)" [style.background]="setBgColor(row.orderId)"&gt;&lt;/mat-row&gt;

And in your TypeScript, you can add a function:

setBgColor(orderId: number){
  if(orderId <= 1000){
    return "#ff0000";
  }
  else if(orderId <= 2000 && orderId > 1000){
    return "#008000";
  }
  return "#ffff00";
}
英文:

Call a function in html

&lt;mat-row *matRowDef=&quot;let row; columns: displayedColumns&quot; (click)=&quot;highlightedRows.push(row)&quot; [style.background]=&quot;setBgColor(row.orderId)&quot;&gt;&lt;/mat-row&gt;

and in your ts you can add a function

  setBgColor(orderId: number){
    if(orderId &lt;= 1000){
      return &quot;#ff0000&quot;;
    }
    else if(orderId  &lt;= 2000 &amp;&amp; orderId &gt; 1000){
      return &quot;#008000&quot;;
    }
    return &quot;#ffff00&quot;;
  }

答案3

得分: 0

你可以使用ngClass来实现这个。这将需要创建带有所需颜色的CSS类。

然后在此之后,您需要执行以下操作:

<mat-row *matRowDef="let row; columns: displayedColumns" (click)="highlightedRows.push(row)" 
[ngClass]="{'redClass': row.orderId <= 1000, 'greenClass': row.orderId > 1000 && row.orderId <= 2000, 'yellowClass': row.orderId > 2000}">

来源:https://stackoverflow.com/questions/44821875/multiple-conditions-in-ngclass-angular-4

英文:

You can use ngClass for this. This will require to make css classes with the required color.

Then after this you need to do:

&lt;mat-row *matRowDef=&quot;let row; columns: displayedColumns&quot; (click)=&quot;highlightedRows.push(row)&quot; 
[ngClass]=&quot;{&#39;redClass&#39;: row.orderId &lt;= 1000, &#39;greenClass&#39;: row.orderId &gt; 1000 &amp;&amp; row.orderId &lt;= 2000, &#39;yellowClass&#39;: row.orderId &gt; 2000}&quot;&gt;

Source: https://stackoverflow.com/questions/44821875/multiple-conditions-in-ngclass-angular-4

huangapple
  • 本文由 发表于 2020年1月6日 15:45:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608386.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定