Search code examples
angularprimengprimeng-turbotable

Programmatically set p-tableCheckbox


I have a nested turbotable (p-table inside p-table) with checkboxes and expandable rows.

I would like to be able programmatically set certain checkboxes on, e.g. if a user selects a row in the nested table then the ckeckbox of the parent row should get selected also.

Check this StackBlitz


Solution

  • I made quite a lot of changes in the code you joined but main part is here with comments :

    updateParentSelection(i, parentRow) {
        if (this.cars[i].length > 0) { // if subselection not empty
          if (this.parentSelection.indexOf(parentRow) === -1) { // if parent row not already selected
            this.parentSelection = this.parentSelection.concat(parentRow); // add parent row to parent selection
          }
        } else { // if subselection empty
          this.parentSelection.splice(this.parentSelection.indexOf(parentRow), 1).slice(0); // remove parent row from parent selection
          this.parentSelection = [].concat(this.parentSelection.splice(this.parentSelection.indexOf(parentRow), 1).slice(0)); // trick to update the view
        }
      }
    

    Don't hesitate if you have any questions.

    See forked StackBlitz