【问题标题】:Table not getting updated on clicking "Remove Selected" button second time in Angular 8在Angular 8中第二次单击“删除选定”按钮时表格未更新
【发布时间】:2020-12-01 12:29:45
【问题描述】:

我需要在“删除选定”btn 单击时从表中删除选定的行(复选框)。 问题 - 第一次单击“删除选定”btn 时工作正常,但是当我再次复制相同的步骤(单击复选框,然后单击删除选定)时,表格没有在屏幕上更新,就像每次检查长度后一样btn 点击,长度正在减少,即逻辑上工作正常。我是角度的新手。提前致谢。

请在此处找到我的 UI 图像 - UI.png

在下面分享我的代码 sn-p -

.ts 文件 -

items = [
    {
      id:1,
      execution_type: 'yagging',
      msisdn: 1898763212,
      bscode: 9876,
      nid: 875456,
      dob: '05/07/1991',
      poscode: 3456789,
      batchId: 12345,

    },
    {
      id:2,
      execution_type: 'KCPMSISDN Tagging',
      msisdn: 1798763212,
      bscode: 654367,
      nid: 875456,
      dob: '05/07/1991',
      poscode: 4789987,
      batchId: 12345,

    },
    { 
      id:3,
      execution_type: 'KCPMSISDN Tagging',
      msisdn: 1798763213,
      bscode: 654367,
      nid: 875456,
      dob: '05/07/1991',
      poscode: 3456789,
      batchId: 12345,

    },

bulkList: {
    execution_type: string
    msisdn: any
    bscode: any
    nid: any
    dob: string
    poscode: any
    batchId:any
  }[] = [];

public searchString : string;
  
  selectedOption: null;

constructor(private fb:FormBuilder,
    private router: Router,
    private route: ActivatedRoute,) { }
    @ViewChild('focus', { static: false }) input: ElementRef;

  ngOnInit(){ 
      this.bulkList = this.items;
      this.checkboxes = new Array(this.bulkList.length);
      this.checkboxes.fill(false);
      this.filterdata = new Array();
      
      console.log("hi",this.checkboxes)
  }
  selectedRow: Number;
  checkboxes: boolean[];
  checks=false;
  filterdata:any[];
  
createForm() {
    this.form = this.fb.group({
      execution_typ: ['', [Validators.required]],
      msisdn: ['', [Validators.required]],
      bscode: ['', [Validators.required]],
      nid: ['', [Validators.required]],
      dob: [''],
      poscode: [''],
      batchId:[''],
  })
}
validateMsisdn() {
    console.log("inside ")
    this.errors.msisdn =  Validator.validate({
      value: this.searchString,
      validations: [VALIDATIONS.CONTACT],
      options: { len: [11] }
    });
validate() {
    this.validateMsisdn();
    return this.errors.msisdn.valid;
  }

setClickedRow(index) {
  // console.log("inside set clicked row")
  // console.log(index)
  this.selectedRow = index;
}
toggleSelection(event, i, value) {
  console.log("inside toggle ")
  console.log(i);
  console.log("value inside toggle ",value)
  console.log("inside toggle ",event.target.checked,"   index ",i)
  this.checkboxes[i] = event.target.checked;
  console.log("toggle sel", this.checkboxes[i])
  
  if (this.checkboxes[i]==true)
  {
    console.log("inside if-true")
    this.filterdata.push(value);
  }
  else{
    console.log("inside if-false", i )
    
    this.filterdata.splice(this.filterdata.indexOf(value),1);
  }
  console.log("data ",this.filterdata);
}
delete()
{ 
  
  var atleastOneSelected = this.checkboxes.some(checkbox => checkbox === true);

  if (!atleastOneSelected) {
    alert("No rows selected.")
    return;
  }
 
  console.log("length " ,this.checkboxes.length)
for(let i=0;i<=this.filterdata.length-1;i++)
{
    console.log("for loop ");
    console.log("this.fil ",this.filterdata[i]);
    const foundIndex = this.bulkList.findIndex(({ msisdn }) => msisdn === this.filterdata[i]);
    console.log("index  -- ", foundIndex);
this.bulkList.splice(foundIndex,1);
console.log("length after ",this.bulkList.length)

  }
console.log("check boc length ",this.checkboxes.length)
this.checkboxes = this.checkboxes.filter(checkbox => checkbox === false);
this.searchString="";
this.filterdata=new Array();

}

.html 文件 -

<div class="container-fluid">
    <div class="card-custom">
        <div class="card-header">
            <div class="align-item-cener">
                <h1 class="col-10"><marquee direction="left">{{title}}</marquee></h1>
            </div>
        </div>
        <!-- Execution type dropdown -->
        <div class="card-body">
            <div class="row">
                <!-- <div class="col-sm"> -->
                <div class="form-group">
                    <!-- <div class="col-md-6"> -->
                    <select name="" id="" class="form-control" style="margin-right:95px" >
                        <option value="">Please Select</option>
                        <option class="othercolor" *ngFor="let option of bulkProcessList" [value]='option'>
                            {{option}}
                        </option>
                        
                    </select>
                    <select name="" id="" class="form-control" style="margin-left:25px" [(ngModel)]="selectedOption" >
                        <option value="">Please Select</option>
                        <!-- <option [ngValue]="null" [disabled]="false" >All</option> -->
                        <option class="othercolor" *ngFor="let option of searchBox" [value]='option' >
                            {{option}}
                        </option>
                        
                     
                    </select>
            
            
                    <input class="form-control-search" [(ngModel)]="searchString" (keyup)="validateMsisdn()"
                        placeholder="Please Search" name="searchString" type="text"  appDigitsOnly="true"[disabled]="disableMsisdn">
                       
                        
                        <button type="submit" class="searchbtn" (click)="onEdit(item.id)">
                            <fa-icon [icon]="fa.faSearch"></fa-icon></button>
                            <button class="btn btn-primary btn float-right" (click)="onCreate()">
                                <fa-icon [icon]="fa.faPlus"></fa-icon> Create New
                            </button>
                            
                            
                </div>
            </div>
            <div class="col-lg-6">
                <div class="form-group">
                
            <small class="form-text text-danger">{{getMessage(errors.msisdn)}}</small>
            </div>
        </div>
        </div>
        <!-- View window -->
        <div class="table-wrapper"  >
            <table class="table table-responsive-md">
                <thead>
                    <tr>
                        <th><input type="checkbox" (change)="bulk($event)"  />Select All</th>
                        
                        <th>Execution Type<br><input class="form-control-headersearch" id="searchbox" [(ngModel)]="searchText" autocomplete="off"
                            placeholder="Please Search" name="srch-term" id="ed-srch-term" type="text"><button type="submit" class="headersearchbtn" (click)="onEdit(item.id)">
                                <fa-icon [icon]="fa.faSearch"></fa-icon></button></th>
                        <th>MSISDN<br><input class="form-control-headersearch" [(ngModel)]="searchText" autocomplete="off"
                            placeholder="Please Search" name="srch-term" id="ed-srch-term" type="text"><button type="submit" class="headersearchbtn" (click)="onEdit(item.id)">
                                <fa-icon [icon]="fa.faSearch"></fa-icon></button></th>
                        <th>BSCODE</th>
                        <th>NID</th>
                        <th>DOB</th>
                        <th>POSCODE<br><input class="form-control-headersearch" [(ngModel)]="searchText" autocomplete="off"
                            placeholder="Please Search" name="srch-term" id="ed-srch-term" type="text"><button type="submit" class="headersearchbtn" (click)="onEdit(item.id)">
                                <fa-icon [icon]="fa.faSearch"></fa-icon></button></th>
                        <th>EDIT</th>
               
                    </tr>
                </thead>
                <tbody *ngIf="bulkList.length > 0">
                    <tr *ngFor="let item of bulkList | filter :{msisdn:searchString,bscode:searchString,poscode:searchString}
                    let i=index;"(click)="setClickedRow(i)"  [class.active]="i == selectedRow">
                    
                    
                    <!-- filter:'execution_type':searchString"> -->
                        <td><input type="checkbox" (click)="toggleSelection($event, i,item.msisdn)" [checked]="checkboxes[i]" /> </td>
                        <td>{{item.execution_type}}</td>
                        <td>{{item.msisdn}}</td>
                        <td>{{item.bscode}}</td>
                        <td>{{item.nid}}</td>
                        <td>{{item.dob}}</td>
                        <td>{{item.poscode}}</td>
                        <td>
                            <button class="btn btn-outline-secondary btn-sm"  (click)="onEdit(item.id)">
                                <fa-icon [icon]="fa.faEdit"></fa-icon>
                            </button>
                        </td>
                        <!-- <app-contorl-message [control]="form.get('adList')"></app-contorl-message> -->
                    </tr>
                </tbody>
            </table>
            
            <!-- <div class="row justify-content-end">
                <ngb-pagination [(page)]="page" [pageSize]="pageSize" [collectionSize]="bulkList.length" class="mr-4"></ngb-pagination>
            </div> -->
        </div>
        <button class="form-control" (click)="onCreate()">
            <fa-icon [icon]="fa.faPlus"></fa-icon> ADD
        </button>
        <button class="form-control" (click)="delete()">
             Remove Selected
        </button>
        <button class="form-control" (click)="deleteAll()">
             Remove All
        </button>
    </div>
</div>

【问题讨论】:

    标签: html angular typescript arraylist filter


    【解决方案1】:

    这将是一个索引问题。 这样想: 您的索引 i 是在 ngFor 循环进行时建立的。 当您删除一个时,您的索引将“损坏”,随后的索引设置和删除将失败。

    我的建议:为您的项目提供一个生成的索引,并在您的对象数组中删除与此 ID 匹配的对象。

    【讨论】:

    • 嗨,麦克,删除并没有失败,只是屏幕上的数据没有更新。根据日志,在每次删除点击时,列表的长度都会减少,所以我相信功能是正确的,但不知道为什么同样没有反映在屏幕上..
    猜你喜欢
    • 1970-01-01
    • 2019-07-27
    • 2019-11-05
    • 1970-01-01
    • 2013-06-18
    • 2013-11-22
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多