lundi 11 octobre 2021

Problems whit selected item on all data

I want to deselect item all selected when I select an item.

This code works good for all parts, but I have a problem when select an another item.

.html

<p>All select<p/>
  <p-checkbox (onChange)="onSelectAll($event.checked, quotationIndex)" [binary]="true"
            [ngModel]="comp.isSelected">
          </p-checkbox>
   ............
   <p-checkbox [ngModel]="item.isSelected" (onChange)="
              onSelectItem($event.checked, comp, itemIndex)
              " [binary]="true"></p-checkbox>

.ts

  onSelectAll(checked: boolean, selectedIndex: number): void {
    if (!this.emptyList) {
      this.comparison.forEach((yard, index) => {
        if (checked) {
          if (index === selectedIndex) {
            yard.jobsList.forEach(job => job.isSelected = checked);
            yard.isSelected = checked;
          } else {
            yard.jobsList.forEach(job => job.isSelected = !checked);
            yard.isSelected = !checked;
          }
        } else {
          yard.jobsList.forEach(job => job.isSelected = checked);
          yard.isSelected = checked;
        }

      })
    }
  }

  onSelectItem(
    checked: boolean,
    yard: QuotationComparison,
    selectedItemIndex: number
  ): void {
    let selectedJobs = 0;
    yard.jobsList.forEach((job, index) => {
      if (index === selectedItemIndex) {
        job.isSelected = checked;
      }
      if (job.isSelected) {
        selectedJobs++;
      }
    })
    yard.isSelected = yard.jobsList.length === selectedJobs;
  }

Have you any idea please how to solve problem?




Aucun commentaire:

Enregistrer un commentaire