如何使控件正常工作以及在另一个分页中导航时 [英] How to make controls to works correctly and when navigate in another paginate

查看:16
本文介绍了如何使控件正常工作以及在另一个分页中导航时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟着这个post 以使用滑块.当我转到下一页时,控制器出现问题.在首页上,控制器工作得很好,当我转到第二页时,重复相同的值.因此,如果在第 1 页上第一行中的产品 1 处于活动状态,即使在第二页上,第一行中的产品也处于活动状态.但实际上该产品在第二页上并不活跃.同样的事情在其他页面上重复.第一行的每一页都处于活动状态.我认为问题出在我的代码 ts 中.演示请按照我下面的代码:

 populateFormGPS() {this.ws.getAllGpss().subscribe(全球定位系统 =>{this.gpss = gpssconsole.log(gpss)//所有值都到了让控件 = {'gps_id': new FormControl('', Validators.required)};for (让 i = 0; i < this.gpss.length; i++) {控件['gps_actived-' + i] = new FormControl(this.gpss[i].gps_actived === 1, Validators.required)}this.acitveGPSForm = new FormGroup(controls);this.patchForm();})}补丁形式(){this.acitveGPSForm.patchValue({gps_id: this.gpss.map(x => x.gps_id),});}

HTML 代码:

<div class="col s12 m2"><label class="col s12 label-control" style="padding: 0px">页面上的行数</label><select class="form-control input-sm" [(ngModel)]="rowsOnPage"><option [ngValue]="5">5</option><option [ngValue]="10">10</option><option [ngValue]="20">20</option></选择>

<div class="col s12 m4"><label class="col s12 label-control" style="padding: 0">排序方式</label><div class="col s6" style="padding: 0"><select class="form-control input-sm" [(ngModel)]="sortBy">………………</选择>

<div class="col s6" style="padding: 0"><select class="form-control input-sm" [(ngModel)]="sortOrder">………………</选择>

<div class="panel panel-default"><table class="bordered table-bordered" [mfData]="gpss | dataFilter : filterQuery" #mf="mfDataTable" [mfRowsOnPage]="rowsOnPage"[(mfSortBy)]="sortBy" [(mfSortOrder)]="sortOrder"><头><tr><第>序列号</th><th>IMEI </th>SIM号<th>价格</th></tr></thead><tr *ngFor="let item of mf.data; let i=index"><td>{{item.gps_serial}}</td><td>{{item.gps_imei}}</td><td>{{item.gps_sim_iccid}}</td><td>{{item.unit_price}} 全部</td><td><form [formGroup]="acitveGPSForm" class="col s12" *ngIf="auths.findPermission('gpsactivate')"><section class="example-section" ><mat-slide-toggle formControlName="gps_actived-{{i}}" class="example-margin" [checked]="item.gps_actived ===1" #elem (click)="onActivegps(item.gps_id), item.gps_actived, elem)" ></mat-slide-toggle></节></表单><td>………………</td></tr></tbody><脚><td colspan="5"><mfBootstrapPaginator [rowsOnPageSet]="[5,10,20]"></mfBootstrapPaginator></td></tfoot>

请问有什么办法可以解决这个问题吗?

谢谢

解决方案

这是因为您的控件名称基于本地索引而不是 Id.因此,当您的行被过滤以显示每页 n 结果时,您的行 n 将具有索引 0,因此它将重用与实际第 0 行相同的表单控件.

app.component.ts中,替换

controls['active-'+i] = ...

controls['active-'+this.homeboxsp[i].homeboxpackage_id] = ...

然后在 app.component.html 上替换

formControlName="active-{{i}}"

formControlName="active-{{item.homeboxpackage_id}}"

修改后的stackblitz

I followed this post to use the slider. I have a problem with the controller when I go to the next page. On the front page the controller works very well, when I go to the second page, the same values are repeated. So if on page 1 the product 1 in the first line is active, even on the second page, the product in the first line is active. But really the product is not active on the second page. The same thing is repeated on the other pages. Every page the first line has active. I think the problem is in my code ts. DEMO Please follow my code below:

  populateFormGPS() {
    this.ws.getAllGpss().subscribe(
      gpss => {
        this.gpss = gpss
        console.log(gpss)// all value arrive ok
        let controls = {
          'gps_id': new FormControl('', Validators.required)
        };

        for (let i = 0; i < this.gpss.length; i++) {
          controls['gps_actived-' + i] = new FormControl(this.gpss[i].gps_actived === 1, Validators.required)
        }
        this.acitveGPSForm = new FormGroup(controls);
        this.patchForm();
      }
    )
  }

  patchForm() {
    this.acitveGPSForm.patchValue({
      gps_id: this.gpss.map(x => x.gps_id),
    });
  }

Html code:

<div class="row">
  <div class="col s12 m2">
    <label class="col s12 label-control" style="padding: 0px">Rows on page</label>
    <select class="form-control input-sm" [(ngModel)]="rowsOnPage">
        <option [ngValue]="5">5</option>
        <option [ngValue]="10">10</option>
      <option [ngValue]="20">20</option>
    </select>
  </div>
  <div class="col s12 m4">
    <label class="col s12 label-control" style="padding: 0">Sort by</label>
    <div class="col s6" style="padding: 0">
      <select class="form-control input-sm" [(ngModel)]="sortBy">
       ..................
      </select>
    </div>
    <div class="col s6" style="padding: 0">
      <select class="form-control input-sm" [(ngModel)]="sortOrder">
       ............
      </select>
    </div>
  </div>
</div>
<div class="panel panel-default">
  <table class="bordered table-bordered" [mfData]="gpss | dataFilter : filterQuery" #mf="mfDataTable" [mfRowsOnPage]="rowsOnPage"
    [(mfSortBy)]="sortBy" [(mfSortOrder)]="sortOrder">
    <thead>
      <tr>
        <th>Serial No. </th>
        <th>IMEI </th>
        <th>SIM no</th>
        <th>Price</th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let item of mf.data; let i=index">
        <td>{{item.gps_serial}}</td>
        <td>{{item.gps_imei}}</td>
        <td>{{item.gps_sim_iccid}}</td>
        <td>{{item.unit_price}} ALL</td>
        <td>
          <form [formGroup]="acitveGPSForm" class="col s12" *ngIf="auths.findPermission('gpsactivate')">
              <section class="example-section" >
                <mat-slide-toggle formControlName="gps_actived-{{i}}" class="example-margin" [checked]="item.gps_actived ===1" #elem (click)="onActivegps(item.gps_id, item.gps_actived, elem)" >
                </mat-slide-toggle>
              </section>
          </form>
          <td>
         .............
          </td>
      </tr>
    </tbody>
    <tfoot>
      <td colspan="5">
        <mfBootstrapPaginator [rowsOnPageSet]="[5,10,20]"></mfBootstrapPaginator>
      </td>
    </tfoot>
  </table>
</div>

Please, any idea, how to solve this problem?

Thanks

解决方案

It's because your control names are based on local indexes instead of Ids. So when your rows are filtered to display n results per page, your row n will have index 0, so it'll reuse the same form control as for actual row 0.

In app.component.ts, replace

controls['active-'+i] = ...

with

controls['active-'+this.homeboxsp[i].homeboxpackage_id] = ...

And on app.component.html, replace

formControlName="active-{{i}}"

with

formControlName="active-{{item.homeboxpackage_id}}" 

Modified stackblitz

这篇关于如何使控件正常工作以及在另一个分页中导航时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆