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

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

问题描述

我遵循了这个 演示 请按照下面的代码进行操作:

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代码:

<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?

谢谢

推荐答案

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

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.

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆