首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular 6-分页不适用于数据表

Angular 6-分页不适用于数据表
EN

Stack Overflow用户
提问于 2019-08-24 18:56:36
回答 1查看 2.9K关注 0票数 0

我正在从服务器请求数据,并能够成功地在数据表中显示它,因为某些原因我不得不使用"table mat- table“而不是html中的"mat-table”,在此之后,分页功能停止工作。早些时候,当我使用"mat-table“元素时,它工作得很好。此外,使用"table“标签的原因是为了动态隐藏没有数据的列,这是我之前无法做到的。如果有办法解决这个问题,请让我知道。提前感谢!

代码语言:javascript
复制
public array: any = [];//declaring an empty array

import { MatTableDataSource, MatSlideToggleChange } from '@angular/material';
import {MatPaginator,  MatSort} from '@angular/material';
constructor(private router: Router, private Auth: AuthService ) { }

  dataSource = new MatTableDataSource(this.data);

  displayedColumns: string[] = this.array;//passing array to displayedColumns

  @ViewChild(MatPaginator) paginator: MatPaginator;
  @ViewChild(MatSort) sort: MatSort;

ngAfterViewInit() {
  this.dataSource.paginator = this.paginator;
  this.dataSource.sort = this.sort;
 }

getRecords(event){
  event.preventDefault();
  const target = event.target;



this.someService.getDetails()
 .subscribe(
   data =>{

     this.Response=data.data,
     this.dataSource.data = this.Response,
     console.log("fetched data :",this.Response);
     for (var item of Object.keys(this.Response[0])){
      this.array.push(item);// PUSHING THE FETCHED COLUMNS TO ARRAY
    }
   }
 );this.resetArray();

 }
代码语言:javascript
复制
<div class="mat-elevation-z8">
    <mat-paginator #paginator
    [length]="dataSource.data.length"
    [pageIndex]="0"
    [pageSize]="50"
    [pageSizeOptions]="[25, 50, 100, 250]">
</mat-paginator>
 <mat-table [dataSource]="dataSource">
   <ng-container matColumnDef="userId">
     <mat-header-cell *matHeaderCellDef>User Id</mat-header-cell>
     <mat-cell *matCellDef= "let element">{{element.userId}}</mat-cell>
   </ng-container>
   <ng-container matColumnDef="title">
      <mat-header-cell *matHeaderCellDef>Title</mat-header-cell>
      <mat-cell *matCellDef= "let element">{{element.title}}</mat-cell>
    </ng-container>
    <ng-container matColumnDef="id">
        <mat-header-cell *matHeaderCellDef>ID</mat-header-cell>
        <mat-cell *matCellDef= "let element">{{element.id}}</mat-cell>
      </ng-container>
      <ng-container matColumnDef="completed">
          <mat-header-cell *matHeaderCellDef>Completed</mat-header-cell>
          <mat-cell *matCellDef= "let element">{{element.completed}}</mat-cell>
        </ng-container>
        <ng-container matColumnDef="blank">
            <mat-header-cell *matHeaderCellDef>Blank</mat-header-cell>
            <mat-cell *matCellDef= "let element"></mat-cell>
          </ng-container>
   <mat-header-row *matHeaderRowDef="displayedColumns" style="background-color:#bcd8f1;border-color: red;"></mat-header-row>
   <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
 </mat-table>

</div>

数据表使用动态列和分页显示记录,但不是只显示新数据,数据表列附加了以前加载的列,然后显示。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-05 15:23:52

从你的stackblizt

代码语言:javascript
复制
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
  /*
    These variables are not needed
  */
  // public data: any = [];  
  // public array: any = [];

  dataSource = new MatTableDataSource([]);
  displayedColumns: string[] = [];
  // View child require 2 PARAMS
  @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
  @ViewChild(MatSort, { static: true }) sort: MatSort;

  constructor(private service: Service) { }


  ngOnInit() { 
    this.loadData();
  }

  showTable() {
    // I dont know what you really want to do here.
    if(this.dataSource.data.length === 0){
      this.loadData();
    }
  }

  ngAfterViewInit() {
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
  }

  loadData() {
    this.service.getAuthRecords1().subscribe(
      data => {
          this.dataSource.data = data as any,
          console.log("dummy data :", data);
        if (this.displayedColumns.length === 0){
          this.displayedColumns = Object.keys(data[0]);
        }
      });
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57637301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档