【问题标题】:How to make a function in angular 7如何在 Angular 7 中创建函数
【发布时间】:2019-01-22 21:35:14
【问题描述】:

我有一个 6 列的垫子表。在第 5 列中,它显示作业的状态,即已完成、正在运行或待处理。 我创建了两个按钮,即停止和重新运行。 我想编写功能,如果作业完成,则应禁用停止按钮并应启用重新运行,这意味着用户应该能够重新运行已完成的作业以及作业是否正在执行或挂起(等待执行)然后应该启用停止按钮并禁用重新运行,这意味着用户应该能够停止正在运行的作业。 有人可以帮我吗?

我创建了一个示例stackblitz here.

HTML 代码:

<div class="main-content">
<mat-toolbar>
    <mat-progress-bar
        mode="indeterminate"
        class="mat-progress-bar"
        color ="primary"
    >
    </mat-progress-bar>
    &nbsp;&nbsp;
    <button
    mat-icon-button
    (click)="stop_exec_job()"
    matTooltip="Stop Executing the Job"
>
    <!-- Edit icon for row -->
    <i class="material-icons" style="color:red"> stop </i>
</button>

</mat-toolbar>

<div class="card">
    <div class="card-header">
        <h5 class="title">Job Execution Stats</h5>
    </div>

    <mat-table [dataSource]="jobExecutionStat">
        <!-- Id Column -->
        <ng-container matColumnDef="id">
            <mat-header-cell *matHeaderCellDef> ID </mat-header-cell>
            <mat-cell *matCellDef="let element">{{ element.id }} </mat-cell>
        </ng-container>

        <ng-container matColumnDef="exec_date">
            <mat-header-cell *matHeaderCellDef>
                Execution Date
            </mat-header-cell>
            <mat-cell *matCellDef="let element"
                >{{ element.exec_date }}
            </mat-cell>
        </ng-container>

        <!-- Weight Column -->
        <ng-container matColumnDef="curr_time_period">
            <mat-header-cell *matHeaderCellDef>
                Current Time Period
            </mat-header-cell>
            <mat-cell *matCellDef="let element"
                >{{ element.curr_time_period }}
            </mat-cell>
        </ng-container>

        <!-- Symbol Column -->
        <ng-container matColumnDef="prev_time_period">
            <mat-header-cell *matHeaderCellDef>
                Previous Time Period
            </mat-header-cell>
            <mat-cell *matCellDef="let element"
                >{{ element.prev_time_period }}
            </mat-cell>
        </ng-container>

        <ng-container matColumnDef="status">
            <mat-header-cell *matHeaderCellDef> Status </mat-header-cell>
            <mat-cell *matCellDef="let element"
                >{{ element.status }}
            </mat-cell>
        </ng-container>

        <ng-container matColumnDef="actions">
            <mat-header-cell *matHeaderCellDef> </mat-header-cell>
            <mat-cell *matCellDef="let element; let index = index">
                <button
                    mat-icon-button
                    (click)="stop_exec_job()"
                    matTooltip="Stop Executing the Job"
                >
                    <!-- Edit icon for row -->
                    <i class="material-icons" style="color:red"> stop </i>
                </button>
                <!-- Delete icon for row -->
                <button
                    class="stop_btn"
                    mat-icon-button
                    color="#b71c1c"
                    (click)="re_run_job()"
                    matTooltip="Re-Run the Job"
                >
                    <i class="material-icons" style="color:green">
                        cached
                    </i>
                </button>
            </mat-cell>
        </ng-container>

        <mat-header-row
            *matHeaderRowDef="jobExecStatDisplayedColumns"
        ></mat-header-row>
        <mat-row *matRowDef="let row; columns: jobExecStatDisplayedColumns">
        </mat-row>
    </mat-table>
</div>

打字稿代码:

import { Component, OnInit } from "@angular/core";
import { MatTableDataSource } from "@angular/material";
import { GlobalAppSateService } from '../../services/globalAppSate.service';
import { DataService } from '../../services/data.service';
import { SnakBarComponent } from '../custom-components/snak-bar/snak- 
bar.component';
@Component({
selector: "app-job-execution-screen",
templateUrl: "./job-execution-screen.component.html",
styleUrls: ["./job-execution-screen.component.scss"]
})
export class JobExecutionScreenComponent implements OnInit {
stop_btn: boolean = true;
re_run_btn: boolean = true;


jobExecStatDisplayedColumns = [
    "id",
    "exec_date",
    "prev_time_period",
    "curr_time_period",
    "status",
    "actions"
];

jobExecutionStat = new MatTableDataSource<Element>(ELEMENT_DATA);
constructor(private dataService: DataService, public globalAppSateService: 
GlobalAppSateService, private snakbar: SnakBarComponent ) {

}
ngOnInit() {
    const project = JSON.parse(this.dataService.getObject("project"));
    if (project != null) {
        this.globalAppSateService.onMessage(project);
    }
}


stop_exec_job() {

}
re_run_job() {

}
}
const ELEMENT_DATA: Element[] = [
{
    id: 1,
    exec_date: "17-01-2016",
    prev_time_period: "2016-04,2016-05,2016-06",
    curr_time_period: "2016-08",
    status: "Completed"
},
{
    id: 2,
    exec_date: "17-01-2017",
    prev_time_period: "2017-04,2017-05,2017-06",
    curr_time_period: "2017-08",
    status: "Running"
},
{
    id: 3,
    exec_date: "27-07-2017",
    prev_time_period: "2017-45,2017-46,2017-47",
    curr_time_period: "2018-01,2018-02",
    status: "Pending"
},
{
    id: 4,
    exec_date: "17-10-2018",
    prev_time_period: "2017-30,2017-31,2017-32",
    curr_time_period: "2018-01,2018-02",
    status: "Completed"
},
{
    id: 5,
    exec_date: "21-01-2018",
    prev_time_period: "2016-01,2016-02,2016-03,2016-04",
    curr_time_period: "2016-52",
    status: "Pending"
},
{
    id: 6,
    exec_date: "17-01-2018",
    prev_time_period: "2017-31,2017-32,2017-33,2017-34",
    curr_time_period: "2017-52",
    status: "Running"
}
];
export interface Element {
id: number;
exec_date: string;
prev_time_period: string;
curr_time_period: string;
status: string;
}

出于某种原因,停止和缓存是图标,在我的视觉工作室中我可以看到图标,但在 stackblitz 中我无法将它们转换为图标。 仅供参考,红色停止是停止,绿色缓存是重新运行

【问题讨论】:

  • 您的问题到底是什么?图标不显示或按钮不工作或状态不持久?
  • 按钮不起作用。我创建了一个函数 stop_exec_job 但我不知道如何编写一个函数,如果状态为 Completed 则应启用重新运行按钮。不显示图标是为了让 SOF 用户在查看 stackblitz 时不要感到困惑

标签: javascript angular typescript


【解决方案1】:

使用disabled 禁用按钮或使用*ngIf 将其从DOM 中完全删除。

<button mat-icon-button [disabled]="isRunning" (click)="stop_exec_job()" matTooltip="Stop Executing the Job"></button>

<button mat-icon-button [disabled]="!isRunning" (click)="stop_exec_job()" matTooltip="Stop Executing the Job"></button>

根据执行的操作更新状态。一定要设置正确的初始状态。

isRunning: boolean = false;

stop_exec_job() {
  this.isRunning = false;
}

re_run_job() {
  this.isRunning = true;
}

【讨论】:

  • 我想要这个东西,但基于一个条件。如果我的表的状态列已完成,则应启用 stop_btn,否则如果其挂起则应禁用
  • @saad - 您的 stackblitz 与您问题中的代码完全不同。如果您想要一个用于禁用按钮的状态,您可以执行以下操作。您可以根据需要进一步定制它,概念保持不变。
  • 我想要这样的东西,但是你的代码所做的是,如果我点击它,停止按钮就会消失,如果我点击重新运行,那么重新运行按钮就会消失
  • @saad - 见我的第一行Use disabled to disable the button or *ngIf to remove it from the DOM completely. 如果按钮不再可见,您可能正在使用*ngIf
  • 不,我正在使用您更新的答案。即禁用而不是 *ngIf
【解决方案2】:

如果您想禁用表格中的按钮,您必须使用disabled 属性并检查您的元素状态。

[disabled]="element.status === 'Completed'" // The button will be disabled if status is Completed
[disabled]="element.status === 'Running'" // Disabled if status is Running

考虑到您的标准,您的停止按钮将如下所示:

<button
    [disabled]="element.status === 'Completed'"
    mat-icon-button
    (click)="stop_exec_job()"
    matTooltip="Stop Executing the Job"
>
    <!-- Edit icon for row -->
    <i class="material-icons" style="color:red"> stop </i>
</button>

你的重新运行按钮会是这样的:

 <button 
       [disabled]="element.status === 'Running'"
       class="stop_btn"
       mat-icon-button
       color="#b71c1c"
       (click)="re_run_job()"
       matTooltip="Re-Run the Job"
          >
           <i class="material-icons" style="color:green">
                            cached
           </i>
       </button>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 2022-08-16
    • 2019-04-14
    相关资源
    最近更新 更多