4

I have an angular2 app whose backend is in java. I have a list of customers. When i click to remove a customer , the customer is removed but the list does not update. If i manually refresh the page then the list updates. I tried routing to the list component in the subscribe of delete method but that does not work.

list-customers.component.html

<tr [class.warning]="customer.isDefault == 1" *ngFor="let customer of customers | orderBy:['firstName'] | search:searchCustomer.value;let serial = index">
                <td>{{ serial+1 }}</td>
                <td>{{ customer?.firstName+' '+customer?.lastName}}</td>
                <td>{{ customer.email}}</td>
                <td>{{ customer.mobileNumber}}</td>
                <td>{{ customer.streetAddress}}</td>
                <td>{{ customer.priceList?.name}}</td>
                <td><a [routerLink]="['/loggedIn','customer','edit', customer.id ]"><i class="fa fa-edit fa-2x"></i></a></td>
                <td><a (click)="delete(customer)"><i class="fa fa-trash-o fa-2x"></i></a></td>
              </tr>

list-customers.component.ts

    ngOnInit()
    {
        this.refreshCustomersList();
    }

    delete(customer)
    {
        this.userService.delete(customer.id)
            .subscribe(
                success=>
                {
                    var index = this.customers.indexOf(customer, 0);
                    if (index > -1)
                    {
                        this.customers.splice(index, 1);
                    }
                }
            )

    }

    refreshCustomersList()
    {
        this._authHttp.get(
                this.appService.getApiUrl() + "api/customer/list"
            )
            .map(res=>res.json())
            .subscribe(
                successResponse=>
                {
                    this.customers = successResponse.data.customers;
                },
                () => console.log("Request Completed")
            )

    }
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
usmanwalana
  • 1,002
  • 2
  • 14
  • 31

6 Answers6

6

Try calling this.refreshCustomersList(); in your delete function like this:

    delete(customer)
{
    this.userService.delete(customer.id)
        .subscribe(
            success=>
            {
                var index = this.customers.indexOf(customer, 0);
                if (index > -1)
                {
                    this.customers.splice(index, 1);
                    this.refreshCustomersList();
                }
            }
        )

}

This will update the customers array after a customer is removed.

patelarpan
  • 7,188
  • 2
  • 20
  • 25
jhhoff02
  • 1,179
  • 1
  • 17
  • 24
1

you can use JavaScript array filter like this.

    this.userService.delete(customer.id)
    .subscribe(
        success=>
        {

             let newCustomers = this.customers.filter(item => item.id !== customer.id);
             this.customers = newCustomers;
        }
    )
patelarpan
  • 7,188
  • 2
  • 20
  • 25
  • I think this is the most elegant and straightforward solution, although for simplicity's sake, I did: `this.customers = this.customers.filter(item => item.id !== customer.id);`, instead of assigning first to a variable. – Asamoah Sep 19 '19 at 16:54
  • 1
    that's great @Asamoah – patelarpan Sep 21 '19 at 09:46
0

Splice will return the deleted element instead use slice to return the resulted array after deletion, like :-

this.customers = this.customers.slice(index);
Alok Kamboj
  • 1,017
  • 11
  • 12
0

Enter the following code In HTML:

(click)="delete(customer.length-1-i)".
//
deleteFieldValue(index) {
this.customer.splice(index, 1);
this.revervefordispaly();
}

//for reverse:

revervefordispaly(){
this.reversedProductdata.reverse();
}
Abhinav Saxena
  • 1,990
  • 2
  • 24
  • 55
Richa
  • 21
  • 1
0

I have almost same case, I have an array of products. I have to let users to remove product as per their choices. in the end if there is no product in array then I need to show Cancel button instead of the Back button without reloading page.

I got it done by checking for empty array in ngAfterViewChecked() life cycle hook.

products: Product[];
someCondition: boolean;

constructor(private cdr: ChangeDetectorRef) {}
// import { ChangeDetectorRef } from '@angular/core';

ngAfterViewChecked() {
    this.emptyArray();
}

emptyArray() {
    this.someCondition = this.products.length === 0 ? true : false;

    // run change detection explicitly
    this.cdr.detectChanges();
}

removeProduct(id) {
    // your logic for removing product.
}
Sunny Vakil
  • 362
  • 4
  • 7
-1

Instead of getting the whole list from the API again we can set the customers list as follows. It worked for me:

delete(customer)
{
    this.userService.delete(customer.id)
        .subscribe(
            success=>
            {
                var index = this.customers.indexOf(customer, 0);
                if (index > -1)
                {
                    this.customers = this.customers.splice(index, 1);
                }
            }
        )

}