Angular 中如何从 json 中获取数据分页

Alf*_*rdo 1 javascript arrays httpclient angular

我是新手,我有一个 HTTPclient 服务,它从一个 JSON 文件中获取所有数据,有 1000 个寄存器,我如何进行分页,例如,显示前 10 个并对其进行分页

这是我的服务

@Injectable({
  providedIn: 'root'
})
export class ProductsService {

  constructor(private http:HttpClient ) { 
  }

  getPorducts(): Observable<Product[]> {

    return this.http.get<Product[]>('assets/data/DATA.json');

  }
}
Run Code Online (Sandbox Code Playgroud)

我只是在我的家庭组件上使用它

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styles: []
})
export class HomeComponent implements OnInit {

  public products: any[] = [];


  constructor(private _Products: ProductsService) { }

  public lastKey: any[] = [];

  ngOnInit() {
    this._Products.getPorducts().subscribe(data => {
      if (data) {
        this.products = data;
      }
    });


  }
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到呢?提前致谢

Abo*_*zlR 5

让我们首先从服务中读取数据(在服务中我们从 json 文件中读取数据

export class AppComponent implements OnInit {

 public products: any[] = [];

 curPage: number;
 pageSize: number;

 constructor(private _Products: ProductService) { }

 ngOnInit(): void {
   this._Products.getJSON().subscribe(response => {
     this.products = response.items;
   });

   this.curPage = 1;
   this.pageSize = 10; // any page size you want 
 }

 numberOfPages() {
   return Math.ceil(this.products.length / this.pageSize);
 };
}
Run Code Online (Sandbox Code Playgroud)

这里我们添加了两个变量,curPage可以pageSize根据需要进行更新。(单击)将引导用户到所需的页面。您可以根据需要更新分页控件的外观。

最后在你的html中:

 <table class="table table-sm table-bordered">
     <thead>
         <tr>
             <th>Id</th>
             <th>Name</th>
         </tr>
     </thead>
     <tbody>
         <tr *ngFor="let item of products | slice: (curPage * pageSize) - pageSize :curPage * pageSize">
             <td>{{item.id}}</td>
             <td>{{item.name}}</td>
         </tr>
     </tbody>
 </table>
 <p class="pagination">
     <button [disabled]="curPage == 1" (click)="curPage = curPage - 1">PREV</button>
     <span>Page {{curPage}} of {{ numberOfPages() }}</span>
     <button [disabled]="curPage >= list.length/pageSize" (click)="curPage = curPage + 1">NEXT</button>
 </p>
Run Code Online (Sandbox Code Playgroud)

堆栈闪电战在这里

另一种方法是使用 NPM 包ngx-pagination

第 1 步:通过终端运行以下命令

npm install ngx-pagination --save
Run Code Online (Sandbox Code Playgroud)

步骤2:从服务中读取数据

export class AppComponent implements OnInit {

 public products: any[] = [];
 constructor(private _Products: ProductService) { }

 ngOnInit(): void {
   this._Products.getJSON().subscribe(response => {
     this.products = response.items;
   });
 }

}
Run Code Online (Sandbox Code Playgroud)

步骤 3:在 app.module.ts 中导入依赖项

import { NgxPaginationModule } from 'ngx-pagination';
@NgModule({
   declarations: [
       AppComponent
   ],
   imports: [
       BrowserModule,
       NgxPaginationModule --> this line
   ],
   providers: [],
   bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

现在让我们看一下 app.module.ts 中导入了 ngx-pagination 模块的代码

第 4 步:从 app.component.html 更新视图

<table class="table table-sm table-bordered">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let item of products  | paginate:{itemsPerPage: 5, currentPage:p}">
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
        </tr>
    </tbody>
</table>
<pagination-controls (pageChange)="p=$event"></pagination-controls>
Run Code Online (Sandbox Code Playgroud)

第 5 步:运行应用程序 使用 npm start 在终端上运行应用程序

堆栈闪电战在这里