ng2-bootstrap分页和bootstrap表集成错误 [英] ng2-bootstrap pagination and bootstrap table integration error

查看:88
本文介绍了ng2-bootstrap分页和bootstrap表集成错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试集成 ng2-bootstrap分页组件和引导表.

I'm trying to integrate ng2-bootstrap pagination component and bootstrap table.

我有一个简单的引导表,其中装有ngFor指令:

I have a simple bootstrap table that is loaded with ngFor directive:

<tr>
    <th *ngFor="#col of cols">{{col.header}}
</tr>
</thead>
<tbody>
    <tr *ngFor="#row of rows">
        <td *ngFor="#col of cols">{{row[col.field]}}</td>
    </tr>
</tbody>

rows的内容由分页组件确定: 我有一个名为data的数组,其中包含该表的一堆条目.数组长度用于确定分页分量的totalItems:

The rows content is determined by the pagination component: I have an array called data that contains a bunch of entries for the table. The array length is used to determined the totalItems of the pagination component:

<pagination class="pagination-sm"
            [(ngModel)]="page"
            [totalItems]="data.length"
            [itemsPerPage]="itemsPerPage"
            [maxSize]="maxSize"
            [boundaryLinks]="true"
            (pageChanged)="onPageChange($event)">
</pagination>

表的rows变量仅包含当前页面的条目.这是通过使用分页组件提供的pageChange事件来完成的:

The table's rows variable contains only the entries of the current page. This is done by using the pageChange event provided by the pagination component:

onPageChange(page:any) {
        let start = (page.page - 1) * page.itemsPerPage;
        let end = page.itemsPerPage > -1 ? (start + page.itemsPerPage) : this.data.length;
        this.rows = this.data.slice(start, end);
    }

到目前为止很好... 问题是可以从data数组中删除条目. 如果分页栏指向最后一页,然后从data数组中删除条目,则控制台中将显示以下错误:

So far so good... The problem is that entries can be removed from the data array. If the pagination bar is pointing to the last page and then entries are removed from the data array, the below error is shown in console:

Expression 'rows in App@9:8' has changed after it was checked.

可以在此处找到一个实时示例: http://plnkr.co/edit/5zxamBiWISBpEmXYP5UK?p=preview 只需点击last按钮,然后点击cut data.

A live example can be found here: http://plnkr.co/edit/5zxamBiWISBpEmXYP5UK?p=preview Just click the last button and then cut data.

有什么建议吗?

推荐答案

作为一种解决方法,我决定对cutData函数进行一些更改. 在每次切割数据之前,当前页面都将重置为1:

As a workaround, I decided to change the cutData function a little bit. Before each data cut, the current page is reset to 1:

cutData(){
  this.page = 1;
  this.onPageChange({page: this.page, itemsPerPage: this.itemsPerPage})
  this.data = this.data.slice(0, this.data.length/2);
}

现在一切似乎都按预期进行.

Now everything seems to work as expected.

另一个选项(如此处类似问题)是在rows更改后手动触发组件的更改检测:

Another option (as mentioned here for a similar issue) is to manually trigger change detection for the component after rows change:

constructor(private cd: ChangeDetectorRef) {}

(...)

onPageChange(page:any) {
    let start = (page.page - 1) * page.itemsPerPage;
    let end = page.itemsPerPage > -1 ? (start + page.itemsPerPage) : this.data.length;
    this.rows = this.data.slice(start, end);
    this.cd.detectChanges();
}

这篇关于ng2-bootstrap分页和bootstrap表集成错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆