swimlane / ngx-datatable

✨ A feature-rich yet lightweight data-table crafted for Angular
http://swimlane.github.io/ngx-datatable/
MIT License
4.63k stars 1.68k forks source link

Header's Select All checkbox breaks "selected" model binding #1530

Open dstj opened 6 years ago

dstj commented 6 years ago

I'm submitting a ... (check one with "x")

[ ] bug report => search github for a similar issue or PR before submitting
[x] feature request
[ ] support request => Please do not submit support request here, post on Stackoverflow or Gitter

Current behavior When I check the header's "Select All" checkbox, the [selected] model is not updated and never updated again.

When I check individual rows, the selected model is updated as long as I do not check the header's Select All.

The footer's selected count is still updated as expected.

Handling the (select) output with the splice/push combo as in the demo, is the way around it, but the documentation is pretty unclear.

Note: Maybe related to #818, I'm unsure...

Expected behavior The header's "Select All" checkbox should update the selected model.

Reproduction of the problem I reproduced it here: https://plnkr.co/5NHahUqqU1NVW2dH

ezgif com-video-to-gif

What is the motivation / use case for changing the behavior? It really looks broken... it half works, then stops without any error message in the logs.

Please tell us about your environment:

james-poulose commented 5 years ago

Does anybody have any work around for this please?

hugochouinard commented 5 years ago

I have the same exact behavior, in firefox and chrome @swimlane/ngx-datatable: 14.0.0

dstj commented 5 years ago

@james-poulose, as mentioned in the original issue:

Handling the (select) output with the splice/push combo as in the demo, is the way around it, but the documentation is pretty unclear.

i.e. this one: http://swimlane.github.io/ngx-datatable/#chkbox-selection

And the relevant code is:

  onSelect({ selected }) {
    console.log('Select Event', selected, this.selected);

    this.selected.splice(0, this.selected.length);
    this.selected.push(...selected);
  }
gauravpandvia commented 4 years ago

For me the above code was throwing an error like Cannot delete property 'x' of [object Array\

May be the above code, makes the properties in this.selected as non configurable. So, i changed the above code to the below and it worked now :- ` /**

manishsharma83 commented 3 years ago

@james-poulose, as mentioned in the original issue:

Handling the (select) output with the splice/push combo as in the demo, is the way around it, but the documentation is pretty unclear.

i.e. this one: http://swimlane.github.io/ngx-datatable/#chkbox-selection

And the relevant code is:

  onSelect({ selected }) {
    console.log('Select Event', selected, this.selected);

    this.selected.splice(0, this.selected.length);
    this.selected.push(...selected);
  }

Thanks Bro... Your code perfectly worked for me!!!

Arti3DPlayer commented 3 years ago

same for version 19.0.0 and angular 11.

onSelect({ selected }) {
    console.log('Select Event', selected, this.selected);

    this.selected.splice(0, this.selected.length);
    this.selected.push(...selected);
  }

This is not working. When I click select all, all elements selected, but then something changing and only one left

Arti3DPlayer commented 3 years ago

https://user-images.githubusercontent.com/3278913/110382838-2291d680-8064-11eb-995b-814a17cedf38.mov

My onSelect method:

onSelect({selected}) {
        this.selectedInvoiceItemGroups.splice(0, this.selectedInvoiceItemGroups.length);
        this.selectedInvoiceItemGroups.push(...selected);
    }

My datatable:

<ngx-datatable
                                    class="material striped"
                                    [rows]="invoiceItemGroups"
                                    [columnMode]="ColumnMode.force"
                                    [headerHeight]="headerHeight"
                                    [rowHeight]="'auto'"
                                    [footerHeight]="50"
                                    [limit]="15"
                                    [externalSorting]="true"
                                    [selected]="selectedInvoiceItemGroups"
                                    [selectionType]="SelectionType.checkbox"
                                    (sort)="onSort($event)"
                                    (select)="onSelect($event)"
                                    [sorts]="[{prop: storedParams.ordering.replace('-', ''), dir: (storedParams.ordering.startsWith('-')) ? 'desc' : 'asc'}]"
                                    [loadingIndicator]="invoiceItemGroupsIsLoading" #invoiceDatatable>

                                <ngx-datatable-column [width]="40"
                                                      [sortable]="false"
                                                      [canAutoResize]="false"
                                                      [draggable]="false"
                                                      [resizeable]="false">
                                    <ng-template ngx-datatable-header-template let-value="value"
                                                 let-allRowsSelected="allRowsSelected" let-selectFn="selectFn">
                                        <input type="checkbox" class="customCheckbox" [checked]="allRowsSelected"
                                               (change)="selectFn(!allRowsSelected)"/>
                                    </ng-template>
                                    <ng-template ngx-datatable-cell-template let-value="value" let-isSelected="isSelected"
                                                 let-onCheckboxChangeFn="onCheckboxChangeFn">
                                        <input type="checkbox" class="customCheckbox" [checked]="isSelected"
                                               (change)="onCheckboxChangeFn($event)"/>
                                    </ng-template>
                                </ngx-datatable-column>