0

i have edit and close button in same row, if there is any edit made to the input field then it must show an alert message, if no change is made it must retain its old value. My issue here is, i click on edit and do some change to input, when i click on close, i am able to retain new value, but when i click on close, it must revert to old value

TS:

 public onEditEvent(event) {
    this.editCommitment = event;
  }

  public onCloseEvent(event){
    if(event.policyCT == this.editCommitment.policyCT && event.quotes == this.editCommitment.quotes && event.writtenPremium == this.editCommitment.writtenPremium) {
      event.writtenPremium = this.editCommitment.writtenPremium;
      event.policyCT = this.editCommitment.policyCT;
      event.quotes = this.editCommitment.quotes 
      this.editableRow = 0;
    } else {
      alert('change')
    }
  }

Demo

3
  • Thanks for response @TimothyAlexisVass, here my issue is that, i click on edit and do some change to input, when i click on close, i am able to retain new value, but when i click on close, it must revert to old value Commented Jul 29, 2021 at 16:46
  • Please update your question instead ;) I need to go, perhaps someone else can help you. Commented Jul 29, 2021 at 16:50
  • Generically, one approach is to make a deep copy of the item before entering into 'edit mode'. The edit mode uses the copy. When you save the edits, copy that deep copy back to the source. When you cancel, do nothing. Commented Jul 29, 2021 at 17:22

1 Answer 1

1

By default ng-model will update the model. You need to maintain old value manually when user clicks on edit button. Like below:-

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

Apply that when user click on close button.

  public onCloseEvent(event) {
    event.writtenPremium = this.selectedRow.writtenPremium;
    event.policyCT = this.selectedRow.policyCT;
    event.quotes = this.selectedRow.quotes;
    this.editableRow = 0;
  }

And on Save click you dont have to do anything as model is already updated.

Working Demo:- https://stackblitz.com/edit/angular-turxyo?file=src%2Fapp%2Fapp.component.ts

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for response, will test and update you
Thanks it is working fine, i have voted and accepted answer
i need one help in this
@Bhrungarajni add a new question with problem statement, will try to help.
stackoverflow.com/questions/69983942/… Please help me in this, i have created one new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.