基于状态动态创建的垫子图标

发布于 2025-02-10 06:03:07 字数 1717 浏览 0 评论 0原文

在我的应用程序中,我显示上诉列表,其状态(使用垫子图标显示)。这是我的模板的

<div class="approve-detail" fxLayout="column" fxLayoutGap="10px">
      <p>Created: {{absDetail?.reqDate|date:'d. L. y HH:mm'}}</p>
      <ng-container *ngIf="absDetail?.defList">
        <mat-list >
          <mat-list-item class="def-item" *ngFor="let def of absDetail.defList">
            <mat-icon matListIcon *ngIf="def.idReject!=0" class="warn" matTooltip="Rejected">thumb_down</mat-icon >
            <mat-icon matListIcon *ngIf="def.idApprove!=0&&def.idReject==0" class="success" matTooltip="Approved">thumb_up</mat-icon>
            <mat-icon matListIcon *ngIf="def.idReject==0 && def.idApprove==0" matTooltip="Waiting">help</mat-icon>
            <p matLine>{{def.name}}</p>
          </mat-list-item>
        </mat-list>
        <mat-list *ngIf="abs | isDeleteAppealed:absDetail.defList">
          <mat-list-item class="def-item" *ngFor="let def of absDetail.defList">
            <mat-icon matListIcon *ngIf="def.idCancel<0" color="warn" matTooltip="Rejected">thumb_down</mat-icon >
            <mat-icon matListIcon *ngIf="def.idCancel>0" class="success" matTooltip="Approved">thumb_up</mat-icon>
            <mat-icon matListIcon *ngIf="def.idCancel==0" matTooltip="waiting">help</mat-icon>
            <p matLine>{{def.name}}</p>
          </mat-list-item>
        </mat-list>
      </ng-container>
    </div>

工作原理,但是对所有ng-ifs有点脏,还有其他优雅的方法可以做到这一点吗?我在考虑管道,但是我需要为每个元素生成三个动态属性:类,工具提示和正确的图标名称,因此它似乎不是一个更好的解决方案。有什么想法吗?那某种指令呢?

in my app i display list of appeals, with their status (which is displayed using mat icon). Here is piece of my template

<div class="approve-detail" fxLayout="column" fxLayoutGap="10px">
      <p>Created: {{absDetail?.reqDate|date:'d. L. y HH:mm'}}</p>
      <ng-container *ngIf="absDetail?.defList">
        <mat-list >
          <mat-list-item class="def-item" *ngFor="let def of absDetail.defList">
            <mat-icon matListIcon *ngIf="def.idReject!=0" class="warn" matTooltip="Rejected">thumb_down</mat-icon >
            <mat-icon matListIcon *ngIf="def.idApprove!=0&&def.idReject==0" class="success" matTooltip="Approved">thumb_up</mat-icon>
            <mat-icon matListIcon *ngIf="def.idReject==0 && def.idApprove==0" matTooltip="Waiting">help</mat-icon>
            <p matLine>{{def.name}}</p>
          </mat-list-item>
        </mat-list>
        <mat-list *ngIf="abs | isDeleteAppealed:absDetail.defList">
          <mat-list-item class="def-item" *ngFor="let def of absDetail.defList">
            <mat-icon matListIcon *ngIf="def.idCancel<0" color="warn" matTooltip="Rejected">thumb_down</mat-icon >
            <mat-icon matListIcon *ngIf="def.idCancel>0" class="success" matTooltip="Approved">thumb_up</mat-icon>
            <mat-icon matListIcon *ngIf="def.idCancel==0" matTooltip="waiting">help</mat-icon>
            <p matLine>{{def.name}}</p>
          </mat-list-item>
        </mat-list>
      </ng-container>
    </div>

It works, but its kinda dirty with all that ng-ifs, is there any more elegant way to do this? I was thinking about pipe, but i need to generate three dynamic properties for each element: class, tooltip and proper icon name, so it doesnt seems like a better solution. Any ideas? What about some kind of directive?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待天淡蓝洁白时 2025-02-17 06:03:07

您可以做的一个选项是将它们包裹在单独的组件中。使用您的令def 作为@input()参数。设置时,您可以自动更改类/工具提示/图标的值。这样的东西:

abs-icon.component.ts

imports...

@Component({
    selector: 'abs-icon',
    templateUrl: './abs-icon.component.html',
    styleUrls: ['./abs-icon.component.css']
})
export class AbsIconComponent implements OnInit {
    
    public cls: string;
    public tooltip: string;
    public icont: string;

    private _abs: any;

    get abs(): any{
        return this._abs;
    }
    @Input() set abs(value: any) {
        this._abs = value;

        if (_abs.idReject != 0) {
            cls = "warn";
            tooltip = "Rejected";
            icont = "thumb_down"
        }
        else if (.......do the rest){
            /* some code */
        }
    }

    constructor() { }

    ngOnInit(): void { }
}

abs-icon.component.html

<mat-icon matListIcon 
    class="{{cls}}" 
    [matTooltip]="tooltip">

{{icon}}

</mat-icon >

使用它:

<abs-icon [abs]="def"> </abs-icon>

One option you could do is wrap them in a separate component. Use your let def as an @Input() parameter. When it gets set, you can automatically change the value of the class/tooltip/icon. Something like this:

abs-icon.component.ts

imports...

@Component({
    selector: 'abs-icon',
    templateUrl: './abs-icon.component.html',
    styleUrls: ['./abs-icon.component.css']
})
export class AbsIconComponent implements OnInit {
    
    public cls: string;
    public tooltip: string;
    public icont: string;

    private _abs: any;

    get abs(): any{
        return this._abs;
    }
    @Input() set abs(value: any) {
        this._abs = value;

        if (_abs.idReject != 0) {
            cls = "warn";
            tooltip = "Rejected";
            icont = "thumb_down"
        }
        else if (.......do the rest){
            /* some code */
        }
    }

    constructor() { }

    ngOnInit(): void { }
}

abs-icon.component.html

<mat-icon matListIcon 
    class="{{cls}}" 
    [matTooltip]="tooltip">

{{icon}}

</mat-icon >

using it:

<abs-icon [abs]="def"> </abs-icon>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文