Angular 6: 在*ngFor中使用ngx-pagination对formArray进行分页,但视图中的控件没有改变。
创始人
2024-10-16 06:00:45
0

在Angular 6中使用ngx-pagination对formArray进行分页时,可以按照以下步骤进行设置:

  1. 首先,确保已经安装了ngx-pagination模块,可以使用以下命令进行安装:
npm install ngx-pagination --save
  1. 在你的组件中引入ngx-pagination模块:
import { Component } from '@angular/core';
import { FormBuilder, FormGroup, FormArray } from '@angular/forms';
import { PaginationInstance } from 'ngx-pagination';

@Component({
  selector: 'app-my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.css']
})
export class MyComponent {
  myForm: FormGroup;
  p: number = 1; // 当前页码
  itemsPerPage: number = 10; // 每页显示的条目数

  constructor(private formBuilder: FormBuilder) {
    this.myForm = this.formBuilder.group({
      myArray: this.formBuilder.array([])
    });
  }

  get myArray(): FormArray {
    return this.myForm.get('myArray') as FormArray;
  }

  // 添加一个控件到formArray
  addControl() {
    this.myArray.push(this.formBuilder.control(''));
  }

  // 删除一个控件
  removeControl(index: number) {
    this.myArray.removeAt(index);
  }

  // 获取当前页的数据
  get currentPageItems() {
    const startIndex = (this.p - 1) * this.itemsPerPage;
    const endIndex = startIndex + this.itemsPerPage;
    return this.myArray.controls.slice(startIndex, endIndex);
  }

  // 在视图中初始化控件
  initControls() {
    for (let i = 0; i < 100; i++) {
      this.addControl();
    }
  }

  // 分页配置
  config: PaginationInstance = {
    id: 'custom',
    itemsPerPage: 10,
    currentPage: 1
  };
}
  1. 在模板文件中使用ngx-pagination指令来实现分页:

在上述代码中,通过使用currentPageItems属性来获取当前页的数据,然后在ngFor指令中使用它来循环创建控件。注意,我们还实现了initControls方法来初始化一些控件,这只是一个示例,你可以根据你的需求进行更改。

希望这个解决方法能够帮助到你!

相关内容

热门资讯

第一分钟辅助!wepoker破... 第一分钟辅助!wepoker破解器,菠萝辅助器免费版的特点,经验教程(有挂规律)1、玩家可以在菠萝辅...
5分钟辅助!约局吧德州真的有透... 5分钟辅助!约局吧德州真的有透视挂吗,we-poker软件,妙计教程(今日头条)约局吧德州真的有透视...
第九分钟辅助!wpk插件,hh... 第九分钟辅助!wpk插件,hh poker辅助有用吗,资料教程(确实有挂)1、首先打开hh poke...
第三分钟辅助!wepoker透... 第三分钟辅助!wepoker透视脚本苹果版,pokemmo辅助器,教程书教程(确实有挂)1、完成we...
第三分钟辅助!哈糖大菠萝能开挂... 第三分钟辅助!哈糖大菠萝能开挂吗,aapoker脚本,法门教程(有人有挂)1、超多福利:超高返利,海...
两分钟辅助!wepoker游戏... 两分钟辅助!wepoker游戏的安装教程,wepoker透视苹果系统,法子教程(确实有挂)1、这是跨...
七分钟辅助!德扑之心免费透视,... 七分钟辅助!德扑之心免费透视,wepoker私人辅助器,方案教程(有挂教学)1、德扑之心免费透视免费...
三分钟辅助!hhpoker可以... 三分钟辅助!hhpoker可以开挂吗,wepoker私人局透视教程,操作教程(有挂讲解)1、wepo...
4分钟辅助!wepoker辅助... 4分钟辅助!wepoker辅助工具,hhpoker透视方法,教程书教程(有挂教程)1、起透看视 we...
9分钟辅助!wepoker怎么... 9分钟辅助!wepoker怎么获得好牌,pokerworld破解版下载,练习教程(有挂分析)1、不需...