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")
            )

    }
}

6 Answers 6

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.

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

Comments

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;
        }
    )

2 Comments

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.
that's great @Asamoah
0

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

this.customers = this.customers.slice(index);

Comments

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();
}

Comments

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.
}

Comments

-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);
                }
            }
        )

}

Comments

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.