如何在使用 angular8 的编辑项中有一些更改时抛出验证消息 [英] How to throw validation message, when there is some change in the edit item using angular8

查看:25
本文介绍了如何在使用 angular8 的编辑项中有一些更改时抛出验证消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用反应形式 formArray 在单击添加和单击编辑时添加数据部分,编辑后我使用删除和保存按钮保存到表格中.所以基本上一次只能做一个手术.所以,当我点击编辑时,添加新按钮将被禁用,保存数据后,我启用添加按钮.因此,现在如果我使用编辑对项目进行了更改,然后单击删除或使用新项目进行编辑,则必须弹出警告消息,说您要删除到目前为止所做的更改.我正在尝试实施,但它仅适用于第一次,从下一次开始,每次单击编辑图标时都会显示警报消息.任何人都可以帮助我让它工作.

演示:演示

表格的 HTML 到我推送数据的位置并显示我添加的内容以及我从后端获取的项目.

<头><tr><th scope="col" *ngFor="let field of w9ListDetails">{{field.displayName}}</th><th scope="col" class="width75">Actions</th></tr></thead><tr *ngFor="let w9 of agentDetailsList?.w9Info ?agentDetailsList.w9Info :temporaryControls"><td *ngFor="w9ListDetails 的let 字段">{{w9[field.param]}}</td><td class="width75"><button type="button" class="btn btn-outline-primary btn-table {{isReadOnly ? 'link-disabled' : ''}}"title="View" (click)="downLoadFiles(w9)"><i class="fas fa-eye"></i></button><button type="button" class="btn btn-outline-primary btn-table ml-1 {{isReadOnly ? 'link-disabled' : ''}}"title="Edit" (click)="editw9(w9, 'editMode')"><i class="fas fa-edit"></i></button></td></tr></tbody>

formArray 的 HTML:

<form formArrayName="w9Info" *ngFor="let item of agentW9InfoForm.controls.w9Info?.controls; let i = index;"><div class="border rounded p-3 position-relative" [formGroupName]="i"><button class="btn btn-link btn-close position-absolute" (click)="deleteW9Details(i,item)"[class.disabled]="isReadOnly"><i class="fas fa-times fa-lg"></i></button><div class="row"><div class="col-6"><div class="form-group"><label for="">税号 <span class="text-danger">*</span></label><input type="text" class="form-control {{w9ReadOnly ? 'link-disabled' : ''}}"placeholder="税号" name="taxId" formControlName="taxId" maxlength="50"autocomplete="new-tx" [ngClass]="{ 'is-invalid': 已提交 && item.controls.taxId.errors }"allowNumberOnly><div *ngIf="已提交 && item.controls.taxId.errors" class="invalid-feedback"><div *ngIf="item.hasError('required', 'taxId')">需要税号</div>

<div class="col-6"><div class="form-group"><label for="">签名日期 <span class="text-danger">*</span></label><input type="text" class="form-control {{w9ReadOnly ? 'link-disabled' : ''}}" placeholder="MM/DD/YYYY"name="signatureDate" formControlName="signatureDate"[ngClass]="{'is-invalid': 已提交 && item.controls.signatureDate.errors }"><div *ngIf="已提交 && item.controls.signatureDate.errors" class="invalid-feedback"><div *ngIf="item.controls.signatureDate.errors.required">需要签名日期</div>

<div class="col-6"><div class="form-group"><label for="">业务类型 <span class="text-danger">*</span></label><select class="custom-select {{w9ReadOnly ? 'link-disabled' : ''}}" name="businessType"formControlName="businessType"[ngClass]="{ 'is-invalid': 已提交 && item.controls.businessType.errors }"><option value=''>选择业务类型</option><option *ngFor="let businessType of detailsS​​electDropDown?.W9BusinessType"[value]='businessType.id'>{{businessType.value}}</option></选择><div *ngIf="已提交 && item.controls.businessType.errors" class="invalid-feedback"><div *ngIf="item.controls.businessType.errors.required">需要业务类型</div>

<div class="row" *ngIf="!hideUpload"><div class="col"><div class="form-group" ><label for="">上传文件</label><div class="form-control" (click)="fileInput.click()" appDragDrop(onFileDropped)="uploadFile($event,item.controls)"><input hidden type="file" #fileInput (change)="uploadFile($event,item.controls)"class="custom-file-input" id="uploadFile" formControlName="fileName"><span>{{ item.controls.fileName.value?.slice(12) }}</span>

<div class="col-6"><button type="button" class="btn btn-outline-primary mt-4" id="" (click)="upload()">上传</button>

<div class="row"><div class="col d-flex justify-content-end align-items-end"><button class="btn btn-primary delbtn" (click)="saveW9Details(item.controls,i)"[class.disabled]="isReadOnly">节省

</表单><div class="row mt-3"><div class="col d-flex justify-content-end align-items-end"><div class="form-group {{addButtonDisable ? 'link-disabled' : ''}}"><button type="button" class="btn btn-outline-primary {{isReadOnly ? 'link-disabled' : ''}}"(click)="addW9Details()"><i class="fas fa-plus"></i>添加新</button>

TS:

deleteW9Details(i) {让错误 = !this.getFormData.invalid &&this.getFormData.dirtyif(!error == true) {this.getFormData.removeAt(i);} 别的 {alert('你确定要删除吗')}}

HTML:

<button class="col-2 pull-left m-b10 m-t10 delbtn" (click)="deleteW9Details(i)"[class.disabled]="isReadOnly">删除<button class="col-2 pull-left m-b10 m-t10 delbtn" (click)="saveW9Details(item.controls)"[class.disabled]="isReadOnly">节省

FormArray 的 TS:

 私有 w9InfoFormInit() {如果 (!this.groupCode && !this.agentCode) {//添加this.agentW9InfoForm = this.FB.group({w9Info: this.FB.array([this.createW9Information()])})} 别的 {//编辑this.agentW9InfoForm = this.FB.group({w9Info:this.FB.array([])})}如果(this.mode == 1){this.agentW9InfoForm.disable()this.isReadOnly = true;}}editw9InfoDetails(w9,mode) {让模式 = 模式如果(编辑模式"){this.hideUpload = true;this.saveUpload = false} 别的 {this.hideUpload = false;this.saveUpload = 真}this.addButtonDisable = true;const control = <FormArray>this.agentW9InfoForm.get('w9Info');control.controls = [];让信息 = [];信息推送(w9)for (const emp of info) {const grp = this.FB.group({taxId: [emp.taxId, Validators.required],签名日期:[emp.signatureDate,[Validators.required]],businessType: [emp.businessType, [Validators.required]],文件名:[emp.fileName],agentW9id:[emp.agentW9id],originalFileName: [emp.originalFileName],agentCode:[emp.agentCode],id:[emp.id]});control.push(grp);}}编辑w9(w9,模式){让错误 = !this.w9InfoDetails.invalid &&this.w9InfoDetails.dirty如果(错误!=真){this.editw9InfoDetails(w9,mode)} 别的 {this.notificationService.activate("Validation Message", "您确定要删除更改吗?", "Yes").then(responseOK => {如果(响应OK){this.editw9InfoDetails(w9,mode)}});}}

表单数组的创建以及行项的删除和编辑,

 public addW9Details() {this.hideUpload = false;this.w9ReadOnly = false;if(this.agentDetailsList && this.agentDetailsList.w9Info) {this.saveUpload = false;} 别的 {this.saveUpload = true;}const control = <FormArray>this.agentW9InfoForm.get('w9Info');if(this.agentDetailsList && this.agentDetailsList.w9Info) {control.controls = [];}control.push(this.createW9Information());this.addButtonDisable = true;}公共 deleteW9Details(i,item) {让错误 = !this.w9InfoDetails.invalid &&this.w9InfoDetails.dirtyif(!error == true) {this.w9InfoDetails.removeAt(i);this.addButtonDisable = false} 别的 {this.notificationService.activate("Validation Message", "您确定要删除更改吗?", "Yes").then(responseOK => {如果(响应OK){this.w9InfoDetails.removeAt(i);this.addButtonDisable = false}});}}public preventW9DetailsEmpty() {如果(!this.w9InfoDetails.length){让组 = this.createW9Information();group.reset();this.agentW9InfoForm.setControl('w9Info', this.FB.array([group]));}}公共获取 w9InfoDetails(): FormArray {return this.agentW9InfoForm.controls['w9Info'];}私有 createW9Information() {返回 this.FB.group({taxId: [null, Validators.required],签名日期:[null,Validators.required],业务类型:[null,Validators.required],原始文件名:[''],agentW9id:0,文档名称: [''],agentCode:parseInt(this.agentbasicInfoForm.controls['agentCode'].value),ID:''});}

保存新添加的行项目/保存编辑项目到网格两个部分代码包含在这里:

 saveW9Details(item, num) {如果(this.w9InfoDetails.invalid){this.notificationService.activate("Validation Message", "请填写税号、签名日期、业务类型", "OK", false).then(responseOK => {如果(响应OK){}});} 别的 {this.addButtonDisable = false;if(this.agentDetailsList && this.agentDetailsList.w9Info) {如果(item.agentW9id.value){for(var i=0;i x.id === parseInt(item.businessType.value));if(this.agentDetailsList.w9Info[i].agentW9id===item.agentW9id.value){this.agentDetailsList.w9Info[i].taxId=item.taxId.value;this.agentDetailsList.w9Info[i].businessType=item.businessType.value;this.agentDetailsList.w9Info[i].signatureDate=item.signatureDate.value;this.agentDetailsList.w9Info[i].agentW9id = item.agentW9id.value;this.agentDetailsList.w9Info[i].businessName = itembusinessName[0].value;this.agentDetailsList.w9Info[i].id = itembusinessName[0].id;//更新=真;this.temporaryControls.push(this.agentDetailsList.w9Info[i])}}}别的 {让 itembusinessName = this.detailsS​​electDropDown.W9BusinessType.filter(x => x.id === parseInt(item.businessType.value));console.log(itembusinessName,"business")让温度={taxId:item.taxId.value,签名日期:item.signatureDate.value,业务类型:item.businessType.value,agentW9id:item.agentW9id.value,业务名称:项目业务名称[0].value,originalFileName:item.fileName.value?item.fileName.value.slice(12):'',id:this.temporaryControls.length}如果(this.agentDetailsList&& this.agentDetailsList.w9Info){this.agentDetailsList.w9Info.push(temp);this.temporaryControls.push(temp)}console.log(this.temporaryControls,"dssdf")}}别的 {让 itembusinessName = this.detailsS​​electDropDown.W9BusinessType.filter(x => x.id === parseInt(item.businessType.value));console.log(itembusinessName,"business")让温度={taxId:item.taxId.value,签名日期:item.signatureDate.value,业务类型:item.businessType.value,agentW9id:item.agentW9id.value,业务名称:项目业务名称[0].value,originalFileName:item.fileName.value?item.fileName.value.slice(12):'',id:this.temporaryControls.length};控制台日志(温度,dsfdsg")this.temporaryControls.push(temp)}console.log(this.temporaryControls,"dssdf")this.w9InfoDetails.removeAt(item);console.log(this.temporaryControls,"temp")}}

解决方案

在编辑方法 editw9() 上,您收到消息您确定要删除吗",因为在第一次编辑和保存后,错误"是从下一次编辑单击开始总是设置为 true,这是因为 this.getFormData.dirty 返回 true.

let error = !this.getFormData.invalid &&this.getFormData.dirty

保存方法调用 this.getFormData.markAsPristine(); 作为最后一行,将重置表单的脏标志

I am using reactive forms formArray to add the section of data on click of add, and on click of edit, after editing i have used delete and save button to save to the table. So basically one operation at a time must be done. So, when i click on edit, Add New button will be disabled and after saving the data, i am enabling the Add button. So, now if i have made changes in the item by using edit, and click to delete or take up a new item to edit, then alert message must throw up saying, you want to delete changes made so far. I am trying to implement but it works only for the first time and from next time it start showing alert message for every click on edit icon. Can anyone help me out to make it work.

DEMO: DEMO

HTML of Table to where i am pushing data and displaying what i have added and what items i am getting from backend.

<table class="table table-hover accordion-table"
        *ngIf="agentDetailsList?.w9Info || temporaryControls.length != 0">
        <thead>
          <tr>
            <th scope="col" *ngFor="let field of w9ListDetails"> {{field.displayName}}
            </th>
            <th scope="col" class="width75">Actions</th>
          </tr>
        </thead>
        <tbody>
          <tr *ngFor="let w9 of agentDetailsList?.w9Info ? agentDetailsList.w9Info : temporaryControls ">
            <td *ngFor="let field of w9ListDetails">
              {{w9[field.param]}}
            </td>
            <td class="width75">
              <button type="button" class="btn btn-outline-primary btn-table {{isReadOnly ? 'link-disabled' : ''}}"
                title="View" (click)="downLoadFiles(w9)"><i class="fas fa-eye"></i></button>
              <button type="button" class="btn btn-outline-primary btn-table ml-1 {{isReadOnly ? 'link-disabled' : ''}}"
                title="Edit" (click)="editw9(w9, 'editMode')"><i class="fas fa-edit"></i></button>
            </td>
          </tr>
        </tbody>
      </table>

HTML of formArray:

<div class="card-body" *ngIf="agentW9InfoForm" [formGroup]='agentW9InfoForm'>
        <form formArrayName="w9Info" *ngFor="let item of agentW9InfoForm.controls.w9Info?.controls; let i = index;">
          <div class="border rounded p-3 position-relative" [formGroupName]="i">
            <button class="btn btn-link btn-close position-absolute" (click)="deleteW9Details(i,item)"
              [class.disabled]="isReadOnly"><i class="fas fa-times fa-lg"></i></button>
            <div class="row">
              <div class="col-6">
                <div class="form-group">
                  <label for="">Tax ID Number <span class="text-danger">*</span></label>
                  <input type="text" class="form-control {{w9ReadOnly ? 'link-disabled' : ''}}"
                    placeholder="Tax ID Number" name="taxId" formControlName="taxId" maxlength="50"
                    autocomplete="new-tx" [ngClass]="{ 'is-invalid': submitted && item.controls.taxId.errors }"
                    allowNumberOnly>
                  <div *ngIf="submitted && item.controls.taxId.errors" class="invalid-feedback">
                    <div *ngIf="item.hasError('required', 'taxId')">Tax Id is required</div>
                  </div>
                </div>
              </div>
              <div class="col-6">
                <div class="form-group">
                  <label for="">Signature Date <span class="text-danger">*</span></label>
                  <input type="text" class="form-control {{w9ReadOnly ? 'link-disabled' : ''}}" placeholder="MM/DD/YYYY"
                    name="signatureDate" formControlName="signatureDate"
                    [ngClass]="{ 'is-invalid': submitted && item.controls.signatureDate.errors }">
                  <div *ngIf="submitted && item.controls.signatureDate.errors" class="invalid-feedback">
                    <div *ngIf="item.controls.signatureDate.errors.required">Signature Date is required</div>
                  </div>
                </div>
              </div>
              <div class="col-6">
                <div class="form-group ">
                  <label for="">Business Type <span class="text-danger">*</span></label>
                  <select class="custom-select {{w9ReadOnly ? 'link-disabled' : ''}}" name="businessType"
                    formControlName="businessType"
                    [ngClass]="{ 'is-invalid': submitted && item.controls.businessType.errors }">
                    <option value=''>Select Business Type </option>
                    <option *ngFor="let businessType of detailsSelectDropDown?.W9BusinessType"
                      [value]='businessType.id'>
                      {{businessType.value}}</option>
                  </select>
                  <div *ngIf="submitted && item.controls.businessType.errors" class="invalid-feedback">
                    <div *ngIf="item.controls.businessType.errors.required">Business Type is required</div>
                  </div>
                </div>
              </div>

            </div>
            <div class="row" *ngIf="!hideUpload">
              <div class="col">
                <div class="form-group" >
                  <label for="">Upload File</label>
                  <div class="form-control" (click)="fileInput.click()" appDragDrop
                    (onFileDropped)="uploadFile($event,item.controls)">
                    <input hidden type="file" #fileInput (change)="uploadFile($event,item.controls)"
                      class="custom-file-input" id="uploadFile" formControlName="fileName">
                      <span>{{ item.controls.fileName.value?.slice(12) }}</span>
                  </div>
                </div>
              </div>
              <div class="col-6">
                <button type="button" class="btn btn-outline-primary mt-4" id="" (click)="upload()">Upload</button>
              </div>
            </div>
            <div class="row">
              <div class="col d-flex justify-content-end align-items-end">
                <button class="btn btn-primary delbtn" (click)="saveW9Details(item.controls,i)"
                  [class.disabled]="isReadOnly">
                  Save
                </button>
              </div>
            </div>
          </div>
        </form>
        <div class="row mt-3">
          <div class="col d-flex justify-content-end align-items-end">
            <div class="form-group {{addButtonDisable ? 'link-disabled' : ''}}">
              <button type="button" class="btn btn-outline-primary {{isReadOnly ? 'link-disabled' : ''}}"
                (click)="addW9Details()"><i class="fas fa-plus"></i> Add New</button>
            </div>
          </div>
        </div>
      </div>

TS:

deleteW9Details(i) {
      let error = !this.getFormData.invalid && this.getFormData.dirty
    if(!error == true) {
      this.getFormData.removeAt(i);

    } else {
      alert('are you sure to delete')

    }
}

HTML:

<button class="col-2 pull-left m-b10 m-t10 delbtn" (click)="deleteW9Details(i)"
          [class.disabled]="isReadOnly">
          Delete
        </button>
         <button class="col-2 pull-left m-b10 m-t10 delbtn" (click)="saveW9Details(item.controls)"
          [class.disabled]="isReadOnly">
          Save
        </button>

TS of FormArray:

  private w9InfoFormInit() {
    if (!this.groupCode && !this.agentCode) {
      // Add
      this.agentW9InfoForm = this.FB.group({
        w9Info: this.FB.array([this.createW9Information()])
      })
    } else {
      //edit
        this.agentW9InfoForm = this.FB.group({
               w9Info:this.FB.array([])
             })
   }
    if (this.mode == 1) {
      this.agentW9InfoForm.disable()
      this.isReadOnly = true;
    }
  }

  editw9InfoDetails(w9,mode) {
    let modes = mode
    if("editMode" ) {
      this.hideUpload = true;
      this.saveUpload = false
    } else {
     this.hideUpload = false;
     this.saveUpload =true
    }
    this.addButtonDisable = true;
    const control = <FormArray>this.agentW9InfoForm.get('w9Info');
    control.controls = []; 
    let info = [];
    info.push(w9)
    for (const emp of info) {
     const grp = this.FB.group({
       taxId: [emp.taxId, Validators.required],
       signatureDate: [emp.signatureDate, [Validators.required]],
       businessType: [emp.businessType, [Validators.required]],
       fileName:[emp.fileName],
       agentW9id:[emp.agentW9id],
       originalFileName: [emp.originalFileName],
       agentCode:[emp.agentCode],
       id:[emp.id]
     });
     control.push(grp);
   }
  }
  editw9(w9,mode) {
    let error = !this.w9InfoDetails.invalid && this.w9InfoDetails.dirty
    if(error != true) {
      this.editw9InfoDetails(w9,mode)

    } else {
      this.notificationService.activate("Validation Message", "Are you sure to delete changes? ", "Yes").then(responseOK => {
        if (responseOK) {
        this.editw9InfoDetails(w9,mode)

        } 
      });
    }

  }

Craeting of form Array along with delete and edit of the row items,

 public addW9Details() {
    this.hideUpload = false;
    this.w9ReadOnly = false;
    if(this.agentDetailsList && this.agentDetailsList.w9Info) {
      this.saveUpload = false;
    } else {
      this.saveUpload = true;
    }
    const control = <FormArray>this.agentW9InfoForm.get('w9Info');
    if(this.agentDetailsList && this.agentDetailsList.w9Info) {
      control.controls = []; 
    } 
    control.push(this.createW9Information());
    this.addButtonDisable = true;

  }

  public deleteW9Details(i,item) {
    let error = !this.w9InfoDetails.invalid && this.w9InfoDetails.dirty
    if(!error == true) {
      this.w9InfoDetails.removeAt(i);
      this.addButtonDisable = false
    } else {
      this.notificationService.activate("Validation Message", "Are you sure to delete changes? ", "Yes").then(responseOK => {
        if (responseOK) {
          this.w9InfoDetails.removeAt(i);
          this.addButtonDisable = false
        } 
      });
    }

  }

  public preventW9DetailsEmpty() {
    if (!this.w9InfoDetails.length) {
      let group = this.createW9Information();
      group.reset();
      this.agentW9InfoForm.setControl('w9Info', this.FB.array([group]));
    }
  }

  public get w9InfoDetails(): FormArray {
    return <FormArray>this.agentW9InfoForm.controls['w9Info'];
  }

  private createW9Information() {
    return this.FB.group({
      taxId: [null, Validators.required],
      signatureDate: [null, Validators.required],
      businessType: [null, Validators.required],
      originalFileName: [''],
      agentW9id:0,
      fileName: [''],
      agentCode:parseInt(this.agentbasicInfoForm.controls['agentCode'].value),
      id:''
    });
  }

Saving of newly added row item/save of edit item to grid both part code contains here:

  saveW9Details(item, num) {

      if(this.w9InfoDetails.invalid) {
        this.notificationService.activate("Validation Message", "Please fill Tax Id Number,Signature Date, Business Type", "OK", false).then(responseOK => {
          if (responseOK) {

          }
        });
      } else {
        this.addButtonDisable = false;
        if(this.agentDetailsList && this.agentDetailsList.w9Info) {
          if(item.agentW9id.value) {
            for(var i=0;i<this.agentDetailsList.w9Info.length;i++){
              let itembusinessName = this.detailsSelectDropDown.W9BusinessType.filter(x => x.id === parseInt(item.businessType.value));
              if(this.agentDetailsList.w9Info[i].agentW9id===item.agentW9id.value){
                this.agentDetailsList.w9Info[i].taxId=item.taxId.value;
                this.agentDetailsList.w9Info[i].businessType=item.businessType.value;
                this.agentDetailsList.w9Info[i].signatureDate=item.signatureDate.value;
                this.agentDetailsList.w9Info[i].agentW9id = item.agentW9id.value;
                this.agentDetailsList.w9Info[i].businessName = itembusinessName[0].value;
                this.agentDetailsList.w9Info[i].id = itembusinessName[0].id;
                // updated=true;
                this.temporaryControls.push(this.agentDetailsList.w9Info[i])
              }
           }
          }  
          else {
            let itembusinessName = this.detailsSelectDropDown.W9BusinessType.filter(x => x.id === parseInt(item.businessType.value));
            console.log(itembusinessName,"business")

            let temp={
              taxId:item.taxId.value,
              signatureDate:item.signatureDate.value,
              businessType: item.businessType.value,
              agentW9id:item.agentW9id.value,
              businessName: itembusinessName[0].value,
             originalFileName:item.fileName.value?item.fileName.value.slice(12):'',
             id: this.temporaryControls.length
            }
            if(this.agentDetailsList&& this.agentDetailsList.w9Info) {
              this.agentDetailsList.w9Info.push(temp);
              this.temporaryControls.push(temp)
            } 
            console.log(this.temporaryControls,"dssdf")
          }
        } 


        else {
          let itembusinessName = this.detailsSelectDropDown.W9BusinessType.filter(x => x.id === parseInt(item.businessType.value));
            console.log(itembusinessName,"business")
            let temp={
              taxId:item.taxId.value,
              signatureDate:item.signatureDate.value,
              businessType: item.businessType.value,
              agentW9id:item.agentW9id.value,
              businessName: itembusinessName[0].value,
             originalFileName:item.fileName.value?item.fileName.value.slice(12):'',
             id: this.temporaryControls.length

            };
            console.log(temp,"dsfdsg")
            this.temporaryControls.push(temp)
        }

        console.log(this.temporaryControls,"dssdf")
       this.w9InfoDetails.removeAt(item);
       console.log(this.temporaryControls,"temp")
      }
        }

解决方案

On edit method editw9(), you are getting message 'are you sure to delete', because after edit and save for the first time, "error" is always getting set to true from next edit click onwards, it is because this.getFormData.dirty is returning true.

let error = !this.getFormData.invalid && this.getFormData.dirty

On save method call this.getFormData.markAsPristine(); as last line, which will reset dirty flag of the form

这篇关于如何在使用 angular8 的编辑项中有一些更改时抛出验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆