Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

p-table: p-tableHeaderCheckbox not working when selection is containing id's instead of the full entity #17142

Open
Tpower36 opened this issue Dec 19, 2024 · 5 comments
Labels
Resolution: Help Wanted Issue or pull request requires extra help and feedback
Milestone

Comments

@Tpower36
Copy link

Tpower36 commented Dec 19, 2024

Describe the bug

When using id's in the table selection, the p-tableHeaderCheckbox does not work properly because it adds the full entities in the selection.

Environment

NA

Reproducer

https://stackblitz.com/edit/gmz5zhcn?file=src%2Fapp%2Ftable-checkbox-selection-demo.html

Angular version

18.0.1

PrimeNG version

18.0.0

Build / Runtime

Angular CLI App

Language

TypeScript

Node version (for AoT issues node --version)

18.20.3

Browser(s)

No response

Steps to reproduce the behavior

Click on the checkbox in the header -> Rows are not all selected.

Expected behavior

Rows are all selected when clicking on the header checkbox

@Tpower36 Tpower36 added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Dec 19, 2024
@mertsincan mertsincan added Resolution: Help Wanted Issue or pull request requires extra help and feedback and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Dec 19, 2024
@mertsincan mertsincan added this to the 19.x milestone Dec 19, 2024
@github-project-automation github-project-automation bot moved this to Review in PrimeNG Dec 19, 2024
Copy link

Due to PrimeNG team's busy roadmap, this issue is available for anyone to work on. Make sure to reference this issue in your pull request. ✨ Thank you for your contribution! ✨

@qwadrox
Copy link
Contributor

qwadrox commented Dec 19, 2024

- <p-tableCheckbox [value]="product.id" />
+ <p-tableCheckbox [value]="product" />

p-tableCheckbox's current implementation expects the row data, not the identifier. You have to pass the rowData to make it work.

What is your use case? why is it not good to pass the full object?

@Tpower36
Copy link
Author

- <p-tableCheckbox [value]="product.id" />
+ <p-tableCheckbox [value]="product" />

p-tableCheckbox's current implementation expects the row data, not the identifier. You have to pass the rowData to make it work.

What is your use case? why is it not good to pass the full object?

We have the usecase that we need to init the table with elements already selected in it. But the parent components that pass the selected elements only know the id's.

As a workaround we deal with two arrays one with id's and one with row data, and always sync them when selection changes or on initialization.

The p-tableCheckbox work fine with the identifier, it's only the p-tableHeaderCheckbox that doesn't seem to work.

@qwadrox
Copy link
Contributor

qwadrox commented Dec 20, 2024

@Tpower36 Hi!

"We have the usecase that we need to init the table with elements already selected in it"
Once you have the product id array, from the parent you can just do something like:

ngOnInit() {
    const productIdSelectedFromParent = ['1000', '1003'];
    this.productService.getProductsMini().then((data: Product[]) => {
      this.products = data;
      this.selectedProducts = data.filter((product) =>
        productIdSelectedFromParent.includes(product.id)
      );
    });
  }

https://stackblitz.com/edit/d4neckx3?file=src%2Fapp%2Ftable-checkbox-selection-demo.ts

@Tpower36
Copy link
Author

@Tpower36 Hi!

"We have the usecase that we need to init the table with elements already selected in it" Once you have the product id array, from the parent you can just do something like:

ngOnInit() {
    const productIdSelectedFromParent = ['1000', '1003'];
    this.productService.getProductsMini().then((data: Product[]) => {
      this.products = data;
      this.selectedProducts = data.filter((product) =>
        productIdSelectedFromParent.includes(product.id)
      );
    });
  }

https://stackblitz.com/edit/d4neckx3?file=src%2Fapp%2Ftable-checkbox-selection-demo.ts

Yes that's about what I do to init the table. But then the parent component needs to be updated at each selection change, and sometimes even needs to modify the selection so we still need the list of id's.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Help Wanted Issue or pull request requires extra help and feedback
Projects
Status: Review
Development

No branches or pull requests

3 participants