简体   繁体   English

如果没有使用 Angular8 在输入字段中进行编辑,如何禁用保存按钮

[英]How to disable Save button, if there is no edit made in input field using Angular8

I am using Angular8 application and in this i have added inline edit using ngModel, but in this i have to keep Save button disabled, if there is no change in the input fields, if there is any change among 3 fields then save button should be enabled.我正在使用 Angular8 应用程序,在此我使用 ngModel 添加了内联编辑,但在此我必须禁用保存按钮,如果输入字段没有变化,如果 3 个字段之间有任何变化,那么保存按钮应该是启用。

HTML: HTML:

  <ng-container *ngFor="let contact of yearManagement">
      <tr>
        <td></td>
        <td class="text-capitalize">
          <ng-container *ngIf="editableRow !== contact.id; else newDeb">
            {{contact.policyTypeName}}
          </ng-container>
          <ng-template #newDeb>
            <input [(ngModel)]="contact.policyTypeName" />
          </ng-template>
        </td>
        <td>
          <ng-container *ngIf="editableRow !== contact.id; else newDeb1">
            {{contact.quotes}}
          </ng-container>
          <ng-template #newDeb1>
            <input [(ngModel)]="contact.quotes" (ngModelChange)="onModelChange(oldVal, $event); oldVal = $event;"/>
          </ng-template>
        </td>
        <td>
          <ng-container *ngIf="editableRow !== contact.id; else newDeb2">
            {{contact.policyCT}}
          </ng-container>
          <ng-template #newDeb2>
            <input [(ngModel)]="contact.policyCT" />
          </ng-template>
        </td>
        <td>
          <ng-container *ngIf="editableRow !== contact.id; else newDeb3">
            {{contact.writtenPremium}}
          </ng-container>
          <ng-template #newDeb3>
            <input [(ngModel)]="contact.writtenPremium" />
          </ng-template>
        </td>
        <td class="width125">
          <ng-container *ngIf="editableRow == contact.id;">
            <button class="btn btn-outline-primary btn-table" title="close"
            (click)="onCloseEvent(contact)"
           >close</button>
            <button class="btn btn-outline-primary btn-table" title="close" (click)="onSave(contact)" [disabled]="disableSubmit()"
              >save</button>
          </ng-container>
          <ng-container *ngIf="editableRow !== contact.id;">

            <button class="btn btn-outline-primary btn-table ml-1" title="Edit"
          (click)="[editableRow = contact.id,onEditEvent(contact)]">Edit</button>
          </ng-container>
          <button class="btn btn-outline-primary btn-table ml-1" title="Delete"
                      (click)="deleteCommitment(contact)">Delete</button>
        </td>
      </tr>
    </ng-container>

TS: TS:

 public disableSubmit() {
    let disabled = true;
    const keys = Object.keys();
    keys.forEach(key => {
      if () {
        disabled = false;
        return;
      }
    });
    return disabled;
  }
  

DEMO 演示

I tried to understand the issue and made a few modifications, it did work as expected.我试图理解这个问题并做了一些修改,它确实按预期工作。

First : define a boolean variable called disabled首先:定义一个名为 disabled 的布尔变量

selectedRow: any;
disable: boolean;
public onEditEvent(event) {
  this.disable = true;
  this.selectedRow = { ...event };
  this.editCommitment = event;
 }

The first time when the edit is clicked, it will be true, and the save button will be disabled.第一次点击edit时为true,并禁用保存按钮。

Second change the onChangeModel function, check if the value in the field is changed, if yes, re-activate the save button otherwise it will be disabled.第二次更改onChangeModel函数,检查字段中的值是否改变,如果改变,重新激活保存按钮,否则将被禁用。

oldValue: string;
onModelChange(oldval, event) {
 if (event) {
    this.disable = false;
    console.log(oldval, event);
    if (this.oldValue != event) {
    }
    this.oldValue = event;
  }
 }

lastly return the valuable disable in the getter function最后在 getter 函数中返回有价值的禁用

public get disableSubmit() {
  return this.disable;
}

Now call this getter function on the save button现在在保存按钮上调用这个 getter 函数

       <button
          class="btn btn-outline-primary btn-table"
          title="close"
          (click)="onSave(contact)"
          [disabled]="disableSubmit"
        >
          save
        </button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM