0

I have one button New which will open model popup window

<lightning-button label="New" slot="actions" onclick={openModel}></lightning-button>

On click of button openModel function will get call from js which ic calling loaddata function.

t_Fields =[];

 openModal() {
    this.loadData();
}

loadData(){
    for(var key in this.tData){
        this.t_Fields = [...this.t_Fields, {value:this.tabDetails[key], 
 label:this.tData[key]}];
    }
}

tData is capturing values in connectedcall back when page loads and that I need to show in one of the input box when i will click on new.

But whenever I am closing form...same values are getting added like when I am clicking on New button first time values are

red
Orange
Blue

when i am clicking on cancel or close button and again clicking on New button then values showing like and so on

red
Orange
Blue
red
Orange
Blue

and so on..

Please help, how can I resolve it?

1 Answer 1

0

You have used Spread operator in Javascript.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

This line: [...this.t_Fields, {value:this.tabDetails[key], label:this.tData[key]}];

Combines existing this.t_Fields with new this.t_Fields.

So on modal close or cancel, always reset the value of this.t_Fields to [] and other variables in your component as applicable, until you want to retain the values.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .