【问题标题】:Pagination on nested lists in Angular2Angular2中嵌套列表的分页
【发布时间】:2018-08-01 02:47:18
【问题描述】:

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

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”元素分页。如果 department 为空(因为它在当前显示的页面上有零个元素),那么它将收到 display:none

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

我找到的最接近的东西是:Pagination of nested objects in angular js,其中他解释了如何从 scratch 编写自定义寻呼机,但同样,似乎没有任何方法可以计算显示多少个LI,每页只显示10个。

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

【问题讨论】:

    标签: angular pagination nested-loops ngfor


    【解决方案1】:

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

    因此,我改为扁平化并重建我的数组,并重新编写 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>
    

    【讨论】:

    • 您遇到了与我目前遇到的完全相同的问题(AngularJS 除外)。感谢您实际跟进并说出您最终做了什么!我可能也不得不走扁平化数据的路线,这将是一个巨大的重构。
    【解决方案2】:

    Eliezer,“关键”是将变量“count”和“activePage”添加到您的部门。您可以使用类似的地图

    //supouse you have a an array "departments" that is 
    //an object with title,description and list -this last is an array too
    this.department=department.map(d=>{
       title:d.title,
       description:d.description,
       count:d.list.length,
       activePage:0,
       list:d.list
    });
    

    那么你可以使用 split 只显示十个人

    <div *ngFor="let department of myJSON">
        <h2>{{department.title}}</h2>
        <p>{{department.description}}</p>
        <ul class="list">
            <li *ngFor="let person of 
                    department.list.slice(department.activePage,10*(department.activePage))">
                {{person.name}}
                {{person.phone}}
            </li>
        </ul>
        <button (click)="department.activePage++">more</button>
        <button (click)="department.activePage--">less</button>
    </div>
    

    【讨论】:

    • 感谢您的回复,Eliseo,但这里的代码将显示每个部门 10 个
    • 这根本不是我需要的。大多数分页组件都可以做到这一点。我想要的结果是每页显示 10 个
    • ,但如果
    • 在一个类别内,则按部门将它们组合在一起。
  • 那么恐怕你必须使用数组来创建一个 pages[] 数组,每个都有你想要显示的元素。这个元素应该有 type,title,description,personName,personPhone(类型 sirve 以显示或 title 和 description 或 personName 和 personPhone)。然后你的 *ngFor="let data of pages[paginaActual]"
  • 这并不能解决任何问题。它只是将所有工作推送到 App.component.ts 而不是 App.component.html
  • 【解决方案3】:

    我能够通过创建一个新组件来解决这个问题。这样前端分页的实现更容易,嵌套的 NgFor 也最小化了。

    这是我想说的示例代码:

    父组件:

    <div *ngFor="let res of result">
        <p>{{res.name}}</p>
    </div>
    
    <app-child [holder]="res"></app-child>
    

    子组件

    <div *ngFor = "let res1 of holder">
        <p>{{res1.middleName}}</p>
    </div>
    

    注意:在子组件中应用分页逻辑。

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签