代码之家  ›  专栏  ›  技术社区  ›  Lynob

角度数据表:如何从数组中读取数据?

  •  2
  • Lynob  · 技术社区  · 6 年前

    我在用 Angular Datatables

    用户.ts

      ngOnInit() {
        this.dtOptions = {
          pagingType: 'full_numbers',
          pageLength: 7,
          deferRender: true,
          retrieve: true
        };
        if (localStorage.getItem('data') !== null) {
          const data = JSON.parse(localStorage.getItem('data'));
          this.item = data.body.response;
          if (Array.isArray(this.item.domains) || this.item.domains.length) {
            for (let i = 0; i < this.item.domains.length; i++) {
              this.users.getUsers(this.item.domains[i].id).subscribe((res: any) => {
                this.domain_users = res.response.items;
                // the api returns arrays and so I had to iterate over them, and not all of them
                // the first array is empty
                for (let j = 0; j < this.domain_users.length; j++) {
                  if (this.domain_users[j].user !== undefined) {
                    this.user.push(this.domain_users[j].user);
    
                  }
                }
              }, error => {
                console.log('getUsers error, probably session expired on the server ' + error);
              });
            }
          }
        }
      }
      ngAfterViewInit(): void {
        this.dtTrigger.next();
      }
      ngOnDestroy(): void {
        // Do not forget to unsubscribe the event
        this.dtTrigger.unsubscribe();
      }
      rerender(): void {
        this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => {
          // Destroy the table first
          dtInstance.destroy();
          // Call the dtTrigger to rerender again
          this.dtTrigger.next();
        });
      }
    

    html格式

    <table id="detailed-resource-optria" datatable [dtOptions]="dtOptions" [dtTrigger]="dtTrigger" class="compact row-border hover cell-border"
        data-page-length='10'>
        <thead>
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
            </tr>
        </thead>
        <tbody>
            <tr *ngFor="let item of user">
                <td>{{item.profile?.fname}}</td>
                <td>{{item.profile?.lname}}</td>
    
            </tr>
        </tbody>
    </table>
    

    this.user 没有经过 dtOptions ,但我不知道如何修复它,我尝试从数组中提供数据,作为源,这是行不通的。

    2 回复  |  直到 5 年前
        1
  •  2
  •   derelict    6 年前

    你试过跑步吗 this.dtTrigger.next(); 在将所有数据推送到this.user后手动执行?这里建议:l-lin.github.io/angular datatables/#/basic/angular way

    (应操作人员的要求,将评论提升为应答)

    我猜这只是时间问题——因为异步查询直到 ngAfterViewInit

        2
  •  2
  •   Eliseo    6 年前

    如果需要使用大量可观察的,请使用forkJoin

    //define an array of observables
    let request:Observable[]=[]
    if (Array.isArray(this.item.domains) || this.item.domains.length) {
        for (let i = 0; i < this.item.domains.length; i++){
             //just fill the array of observables
             request.push(this.users.getUsers(this.item.domains[i].id))      
        }
        forkJoin(request).subscribe((response:any[])=>{
             //in response[0], we have the response of getUsers(this.item.domain[0].id)
             //in response[1], we have the response of getUsers(this.item.domain[1].id)
             ....
             for (let res in response)
             { 
                 //You can use concat and filter
                 let users=Res.response.items
                     .filter(us=>us.user!=undefined)
                     .map(us=>us.user);
                 this.user.concat(users);
             }
        })
    }