0

I am working on angular project when I am adding new row in table I am getting proper data in my service but in table it is showing 2 times.

Above is my smart table code.

    <ng2-smart-table [settings]="settings" [source]="source"  (createConfirm)="onCreateConfirm($event)">
    </ng2-smart-table>

Following is the function on save.

 add: {
      addButtonContent: '<i class="nb-plus"></i>',
      createButtonContent: '<i class="nb-checkmark"></i>',
      cancelButtonContent: '<i class="nb-close"></i>',
      confirmCreate: true,
    },

    onCreateConfirm(event): void {
if (window.confirm('Are you sure you want to create?')) {
      event.newData['name'] += ' + added in code';
      event.confirm.resolve(event.newData);
      CoursesService.addNewCourse(event.newData);

    } else {
      event.confirm.reject();
    }
  }    
      }

The addNewCourse function in service is as follow,

static addNewCourse(data)
{
        this.coursedata.push(data);
    console.log(this.coursedata);
}

In coursedata I am getting data only one time while in table it is showing 2 times

any help?

INDRAJITH EKANAYAKE
  • 3,894
  • 11
  • 41
  • 63
Faisal Amdani
  • 109
  • 14
  • So on initial load in your table are no double entrys? If you add new data, this data is duplicate in your table? If so then `event.confirm.resolve(event.newData)` adds data to the table and your CoursesService adds new data to your table. – SplitterAlex Feb 09 '18 at 11:21
  • @SplitterAlex then what should I do – Faisal Amdani Feb 09 '18 at 11:49
  • I haven't used ng2-smart-table before, but according the documantation you can set the `mode` property from default `inline` to `external`. Description: "Determines how to react on action links (Add, Edit, Delete). 'external' - just trigger the events (LINK HERE). 'inline' - process internally, show forms/inputs/etc" So if you set the setting to `external` only your changes from your dedicated service will apply. – SplitterAlex Feb 09 '18 at 15:43
  • @SplitterAlex mode should be in inline and it worked for me i was pushing two times the data so it was displaying 2 times so I have given ans to it – Faisal Amdani Feb 10 '18 at 09:24

1 Answers1

0

I was pushing the data again in the addNewCourse() function so i have to remove it. Because event.confirm.resolve(event.newData); is pushing the data so the addNewCourse() sholud be as follow

static addNewCourse(data)
{
    console.log(this.coursedata);
}
Faisal Amdani
  • 109
  • 14