angular - Angular2 中嵌套列表的分页

标签 angular pagination nested-loops ngfor

我有一个正在尝试分页的嵌套项目列表,但我能找到的大多数分页实现似乎不适用于嵌套列表。

app.component.html:

<div *ngFor="let department of myJSON">
    <h2>{{department.title}}</h2>
    <p>{{department.description}}</p>
    <ul class="list">
        <li *ngFor="let person of department.list">
            {{person.name}}
            {{person.phone}}
        </li>
    </ul>
</div>

为了澄清,我在此页面上只需要一个分页元素,并且所有部门都使用它来对每 10 个“li”元素进行分页。如果部门为空(因为它在当前显示的页面上有零个元素),那么它将收到display:none

我尝试过 NgxPaginationModule,但它似乎不支持识别当前 ngFor 之外的元素。

我发现的最接近的是:Pagination of nested objects in angular js ,其中他解释了如何从头开始编写自定义寻呼机,但同样,似乎没有任何方法可以计算显示的 LI 数量,并且每页仅显示 10 个。

任何帮助或指导将不胜感激!

最佳答案

最后,我无法找到任何不迫使我从头开始构建完整分页的解决方案。

因此,我改为展平并重建我的数组,并重新编写 app.component.html删除所有嵌套循环,并使用标准 ngx-pagination .

这是我使用的 HTML 的最终版本:

app.component.html:

//itemList = current page of items.
//i = index
//person = item on page
//If a person is the first one on the page(index=0) or his department ID
//is different from the previous person's department ID, then print 
//the .deparment-section block.

<ng-template ngFor let-person let-i="index" let-itemList="ngForOf" [ngForOf]="personList | paginate: { itemsPerPage: 10, currentPage: p }">
    <div class="department-section" *ngIf="  person.department.id && (i == 0 || person.department.id != itemList[i-1].department.id)">
        <h2>{{departmentList[person.department.id].name}}</h2>
        <p>{{departmentList[person.department.id].description}}</p>                
    </div>
    <div class="person">
        {{person.name}}
        {{person.phone}}
    </div>
</ng-template>

关于angular - Angular2 中嵌套列表的分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48907300/

相关文章:

Angular 构建错误 "..has no exported member ' ɵReflectorReader'。”

angular - ngx-bootstrap ~ 错误类型错误 : undefined is not observable

angular - 类型 observable<{}> 不可分配给任何类型 :Property length is missing in type{} in Typescript

php - 调用未定义的函数 cell()

algorithm - 找出一组中的哪些数字组合加起来等于给定的总数

c++ - python 中等效的 C++ 嵌套打印

javascript - 动画化动态 Angular 组件的移除?

php - 分页语法错误

php - PDO 中是​​否有 mysql_result($result, $i, 'ColumnName' ) 的等效项

function - 这个 postgres 函数是否具有成本效益还是仍然需要清理