Angular2- Update UI after deleting

2019-02-18 04:00发布

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

    }
}

3条回答
ら.Afraid
2楼-- · 2019-02-18 04:37

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

}
查看更多
该账号已被封号
3楼-- · 2019-02-18 04:55

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

this.customers = this.customers.slice(index);
查看更多
Emotional °昔
4楼-- · 2019-02-18 05:01

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.

查看更多
登录 后发表回答