0

I have created a sample angular app where I have taken TableComponent as my main page and also I have a popup in the TableComponent.

When I click on the OpenPopup button in my main page I get a Popup window with two tables.

The first table has a list of data rows and my second table is empty. When I transfer the rows from my first table to second table and click on save button ,the data rows from second table is displayed on the main page table.

But my issue is when I am opening the popup for the second time , the second table is not showing the earlier selected rows and is empty.

Please access my sample app here..

can anybody please suggest me what I am missing here...?

3
  • May be you have cleared you alue while open the popup Commented Oct 26, 2018 at 11:30
  • not I havent cleared.. Commented Oct 26, 2018 at 11:33
  • Please see my revised answer with solution. Commented Oct 26, 2018 at 13:21

1 Answer 1

1

ELEMENT_DATA is a CONST outside of the scope of class OpenPopup, this is why when you complete the splice this.checkedData.splice(index,1) you are essentially mutating or modifying the data in the ELEMENT_DATA const and it is maintaining the state.

class OpenPopup gets instantiated everytime you open the popup and because your are doing the following you are re-initializing the checkedDataSource as [] when the popup opens.

 checkedData = [];
 checkedDataSource = new MatTableDataSource<Element>(this.checkedData);

Revision:

You will need to replicate ELEMENT_DATA for checkedData.

  • Adding the following to your stackblitz example works.

    const ELEMENT_DATA: Element[] = [
    { position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H' },
    { position: 2, name: 'Helium', weight: 4.0026, symbol: 'He' },
    { position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li' },
    { position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be' },
    { position: 5, name: 'Boron', weight: 10.811, symbol: 'B' }
    ];
    
    const CHECKED_DATA: Element[] = [];
    

Then in your OpenPopup class

checkedData = Object.assign(CHECKED_DATA);
Sign up to request clarification or add additional context in comments.

4 Comments

but i am facing one more issue... the second time when i open the popup window and just click on close icon ,now the data in my main table i.e in parent page is again getting deleted. Its retaining the previous state only if I click on save button..
Please check this link stackblitz.com/edit/…....
here I have tried to resolve that issue as you said me to do it for the popup second table..but I am unable to accomplish
Please raise a new question for this issue (as this one has been resolved/answered) and I or another member of SO will take a look.

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.