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

SummaryRow gives error when there is checkboxable column #1412

Open cassmtnr opened 6 years ago

cassmtnr commented 6 years ago

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

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

Current behavior

<ngx-datatable
    class="bootstrap"
    [summaryRow]="enableSummary"
    [summaryPosition]="summaryPosition"
    [columnMode]="'force'"
    [headerHeight]="40"
    [rowHeight]="'auto'"
    (select)='selecionar($event)'
    [rows]="rows">
    <ngx-datatable-column
        [width]="30"
        [sortable]="false"
        [canAutoResize]="false"
        [draggable]="false"
        [resizeable]="false"
        [headerCheckboxable]="true"
        [checkboxable]="true">
    </ngx-datatable-column>
    <ngx-datatable-column 
        prop="valorDocumento" 
        [summaryFunc]="somarTotal">
    </ngx-datatable-column>
</ngx-datatable>

Gives error:

DataTableBodyComponent.html:76 ERROR TypeError: Reduce of empty array with no initial value
    at Array.reduce (<anonymous>)
    at defaultSumFunc (index.js:2320)
    at index.js:2346
    at Array.forEach (<anonymous>)
    at DataTableSummaryRowComponent../src/components/body/summary/summary-row.component.ts.DataTableSummaryRowComponent.updateValues (index.js:2341)
    at DataTableSummaryRowComponent../src/components/body/summary/summary-row.component.ts.DataTableSummaryRowComponent.ngOnChanges (index.js:2331)
    at checkAndUpdateDirectiveInline (core.js:12407)
    at checkAndUpdateNodeInline (core.js:13935)
    at checkAndUpdateNode (core.js:13878)
    at debugCheckAndUpdateNode (core.js:14771)

Expected behavior SummaryRow should ignore fields that doesn't have a summaryFunc (related to https://github.com/swimlane/ngx-datatable/issues/1411)

Please tell us about your environment: OS: Windows 10 IDE: Visual Studio Code package manager: Yarn and NPM

SirWojtek commented 6 years ago

Hello again @cassianomon, the origin of the problem are empty (or null) column values passed to the default summary function:

// summary-row.component.ts:11
function defaultSumFunc(cells: any[]): any {
  return cells
    .filter(cell => !!cell)
    .reduce((res, cell) => res + cell);
}

The error is caused by this function which tries to reduce an empty array - at least one of datatable column is empty. I'll try to fix the issue in this week. Thank you for discovering that.