From 8412a156f08e7c2bf9f5ca1db7236471db86b512 Mon Sep 17 00:00:00 2001 From: Saif Ur Rehman Date: Sat, 19 Jan 2019 02:24:14 +0500 Subject: [PATCH 1/7] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Accumulator=20event?= =?UTF-8?q?=20for=20totals=20row?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/body-renderer.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/body-renderer.js b/src/body-renderer.js index 1c180c9..e439456 100644 --- a/src/body-renderer.js +++ b/src/body-renderer.js @@ -10,6 +10,7 @@ export default class BodyRenderer { this.bodyScrollable = instance.bodyScrollable; this.footer = this.instance.footer; this.log = instance.log; + this.events = instance.events; } renderRows(rows) { @@ -82,12 +83,25 @@ export default class BodyRenderer { column: col }; }); + + const rowCount = this.visibleRows.length; const totalRow = this.visibleRows.reduce((acc, prevRow) => { return acc.map((cell, i) => { const prevCell = prevRow[i]; - if (typeof prevCell.content === 'number') { - cell.content += prevRow[i].content; + + let useDefaultAccumulator = true; + if (this.events.accumulator) { + const rowData = this.datamanager.getData(prevRow.meta.rowIndex); + const res = this.events.accumulator(cell, prevCell, rowData, rowCount); + if (res !== false) { + useDefaultAccumulator = false; + } } + + if (useDefaultAccumulator && typeof prevCell.content === 'number') { + cell.content += prevCell.content; + } + if (!cell.format && prevCell.format) { cell.format = prevCell.format; } From bca0988840386f05acda889c89c67d84564f400a Mon Sep 17 00:00:00 2001 From: Saif Ur Rehman Date: Wed, 23 Jan 2019 22:50:53 +0500 Subject: [PATCH 2/7] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Changed=20values=20pa?= =?UTF-8?q?ssed=20to=20accumulator=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit and moved function to hooks.totalAccumulator --- src/body-renderer.js | 62 ++++++++++++++++++++++++++++---------------- src/datatable.js | 7 +++++ 2 files changed, 47 insertions(+), 22 deletions(-) diff --git a/src/body-renderer.js b/src/body-renderer.js index e439456..26e065e 100644 --- a/src/body-renderer.js +++ b/src/body-renderer.js @@ -10,7 +10,7 @@ export default class BodyRenderer { this.bodyScrollable = instance.bodyScrollable; this.footer = this.instance.footer; this.log = instance.log; - this.events = instance.events; + this.hooks = instance.hooks; } renderRows(rows) { @@ -70,8 +70,9 @@ export default class BodyRenderer { } getTotalRow() { - const columns = this.datamanager.getColumns(); - const totalRowTemplate = columns.map(col => { + const self = this; + const columns = self.datamanager.getColumns(); + const totalRow = columns.map(col => { let content = 0; if (['_rowIndex', '_checkbox'].includes(col.id)) { content = ''; @@ -84,30 +85,47 @@ export default class BodyRenderer { }; }); - const rowCount = this.visibleRows.length; - const totalRow = this.visibleRows.reduce((acc, prevRow) => { - return acc.map((cell, i) => { - const prevCell = prevRow[i]; - - let useDefaultAccumulator = true; - if (this.events.accumulator) { - const rowData = this.datamanager.getData(prevRow.meta.rowIndex); - const res = this.events.accumulator(cell, prevCell, rowData, rowCount); - if (res !== false) { - useDefaultAccumulator = false; - } + for (let column of columns) { + if (['_rowIndex', '_checkbox'].includes(column.id)) { + continue; + } + + let useDefaultAccumulator = !self.hooks.totalAccumulator; + let total = 0; + + if (!useDefaultAccumulator) { + const values = self.visibleRows.map(row => { + return {rowIndex: row.meta.rowIndex, content: row[column.colIndex].content}; + }); + const result = self.hooks.totalAccumulator.call(self.instance, column, values); + if (result === false) { + useDefaultAccumulator = true; + } else { + total = result; } + } - if (useDefaultAccumulator && typeof prevCell.content === 'number') { - cell.content += prevCell.content; + if (useDefaultAccumulator) { + for (let i = 0; i < self.visibleRows.length; ++i) { + const cell = self.visibleRows[i][column.colIndex]; + if (typeof cell.content === 'number') { + total += cell.content; + } } + } - if (!cell.format && prevCell.format) { - cell.format = prevCell.format; + let format = null; + for (let i = 0; i < self.visibleRows.length; ++i) { + const cell = self.visibleRows[i][column.colIndex]; + if (cell.format) { + format = cell.format; + break; } - return Object.assign({}, cell); - }); - }, totalRowTemplate); + } + + totalRow[column.colIndex].content = total; + totalRow[column.colIndex].format = format; + } return totalRow; } diff --git a/src/datatable.js b/src/datatable.js index 7234860..5bf5af0 100644 --- a/src/datatable.js +++ b/src/datatable.js @@ -59,6 +59,13 @@ class DataTable { options.events || {} ); this.fireEvent = this.fireEvent.bind(this); + + // custom user events + this.hooks = Object.assign( + {}, + this.options.hooks || {}, + options.events || {} + ); } prepare() { From 2ed6feb3cf6540c573d6960b52a98e02a89a9e08 Mon Sep 17 00:00:00 2001 From: Saif Ur Rehman Date: Sun, 3 Feb 2019 04:48:54 +0500 Subject: [PATCH 3/7] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Total=20row=20set=20n?= =?UTF-8?q?ull=20for=20column=20without=20any=20number?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/body-renderer.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/body-renderer.js b/src/body-renderer.js index 26e065e..20018f3 100644 --- a/src/body-renderer.js +++ b/src/body-renderer.js @@ -91,7 +91,7 @@ export default class BodyRenderer { } let useDefaultAccumulator = !self.hooks.totalAccumulator; - let total = 0; + let total = null; if (!useDefaultAccumulator) { const values = self.visibleRows.map(row => { @@ -109,6 +109,9 @@ export default class BodyRenderer { for (let i = 0; i < self.visibleRows.length; ++i) { const cell = self.visibleRows[i][column.colIndex]; if (typeof cell.content === 'number') { + if (total == null) { + total = 0; + } total += cell.content; } } From 4c75753abfd86dd2a7aae940dcce515bc35e6392 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Fri, 8 Feb 2019 16:02:14 +0530 Subject: [PATCH 4/7] fix: Refactor accumulator - Rename to columnTotal --- index.html | 19 ++++++++++++++- src/body-renderer.js | 58 ++++++++++++++------------------------------ src/defaults.js | 3 +++ 3 files changed, 39 insertions(+), 41 deletions(-) diff --git a/index.html b/index.html index 5c04f2d..ec66ca4 100644 --- a/index.html +++ b/index.html @@ -216,7 +216,24 @@

Frappé DataTable

return format($input.value); } } - } + }, + hooks: { + columnTotal(columnValues, cell) { + if (cell.colIndex === 5) { + // calculated average for 5th column + const sum = columnValues.reduce((acc, value) => { + if (typeof value === 'number') { + return acc + value + } + return acc + }, 0); + return sum / columnValues.length + } + if (cell.colIndex === 2) { + return 'Total' + } + } + } }); console.log(performance.now() - start); diff --git a/src/body-renderer.js b/src/body-renderer.js index 20018f3..1020eb2 100644 --- a/src/body-renderer.js +++ b/src/body-renderer.js @@ -70,9 +70,8 @@ export default class BodyRenderer { } getTotalRow() { - const self = this; - const columns = self.datamanager.getColumns(); - const totalRow = columns.map(col => { + const columns = this.datamanager.getColumns(); + const totalRowTemplate = columns.map(col => { let content = 0; if (['_rowIndex', '_checkbox'].includes(col.id)) { content = ''; @@ -85,50 +84,29 @@ export default class BodyRenderer { }; }); - for (let column of columns) { - if (['_rowIndex', '_checkbox'].includes(column.id)) { - continue; - } + const totalRow = totalRowTemplate.map((cell, i) => { + if (typeof cell.content !== 'number') return cell; - let useDefaultAccumulator = !self.hooks.totalAccumulator; - let total = null; - - if (!useDefaultAccumulator) { - const values = self.visibleRows.map(row => { - return {rowIndex: row.meta.rowIndex, content: row[column.colIndex].content}; - }); - const result = self.hooks.totalAccumulator.call(self.instance, column, values); - if (result === false) { - useDefaultAccumulator = true; - } else { - total = result; + if (this.options.hooks.columnTotal) { + const columnValues = this.visibleRows.map(row => row[i].content); + const result = this.options.hooks.columnTotal.call(this.instance, columnValues, cell); + if (result != null) { + cell.content = result; + return cell; } } - if (useDefaultAccumulator) { - for (let i = 0; i < self.visibleRows.length; ++i) { - const cell = self.visibleRows[i][column.colIndex]; - if (typeof cell.content === 'number') { - if (total == null) { - total = 0; - } - total += cell.content; - } + cell.content = this.visibleRows.reduce((acc, prevRow) => { + const prevCell = prevRow[i]; + if (typeof prevCell.content === 'number') { + return acc + prevCell.content; } - } + return acc; + }, cell.content); - let format = null; - for (let i = 0; i < self.visibleRows.length; ++i) { - const cell = self.visibleRows[i][column.colIndex]; - if (cell.format) { - format = cell.format; - break; - } - } + return cell; + }); - totalRow[column.colIndex].content = total; - totalRow[column.colIndex].format = format; - } return totalRow; } diff --git a/src/defaults.js b/src/defaults.js index e404905..af0396c 100644 --- a/src/defaults.js +++ b/src/defaults.js @@ -38,6 +38,9 @@ export default { onCheckRow(row) {}, onDestroy() {} }, + hooks: { + columnTotal: null + }, sortIndicator: { asc: '↑', desc: '↓', From c516b4a53636888e47f7391514a7dcc74d8c640d Mon Sep 17 00:00:00 2001 From: Saif Ur Rehman Date: Fri, 8 Feb 2019 21:50:23 +0500 Subject: [PATCH 5/7] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Prevent=20showing=200?= =?UTF-8?q?=20in=20total=20row=20for=20Data=20fieldtype?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/body-renderer.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/body-renderer.js b/src/body-renderer.js index 1020eb2..45753c0 100644 --- a/src/body-renderer.js +++ b/src/body-renderer.js @@ -72,7 +72,7 @@ export default class BodyRenderer { getTotalRow() { const columns = this.datamanager.getColumns(); const totalRowTemplate = columns.map(col => { - let content = 0; + let content = null; if (['_rowIndex', '_checkbox'].includes(col.id)) { content = ''; } @@ -85,7 +85,7 @@ export default class BodyRenderer { }); const totalRow = totalRowTemplate.map((cell, i) => { - if (typeof cell.content !== 'number') return cell; + if (cell.content === '') return cell; if (this.options.hooks.columnTotal) { const columnValues = this.visibleRows.map(row => row[i].content); @@ -99,6 +99,7 @@ export default class BodyRenderer { cell.content = this.visibleRows.reduce((acc, prevRow) => { const prevCell = prevRow[i]; if (typeof prevCell.content === 'number') { + if (acc == null) acc = 0; return acc + prevCell.content; } return acc; From 47f2ff10d8b3557665d58fbeddf8d42b79914ec4 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Tue, 12 Feb 2019 19:35:58 +0530 Subject: [PATCH 6/7] fix: Remove hooks object from parent --- src/datatable.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/datatable.js b/src/datatable.js index 5bf5af0..7234860 100644 --- a/src/datatable.js +++ b/src/datatable.js @@ -59,13 +59,6 @@ class DataTable { options.events || {} ); this.fireEvent = this.fireEvent.bind(this); - - // custom user events - this.hooks = Object.assign( - {}, - this.options.hooks || {}, - options.events || {} - ); } prepare() { From 6387f303163cdac30784681534539e0b79a16dff Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Tue, 12 Feb 2019 19:36:49 +0530 Subject: [PATCH 7/7] fix: Remove separate hooks initialization --- src/body-renderer.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/body-renderer.js b/src/body-renderer.js index 45753c0..b635fc3 100644 --- a/src/body-renderer.js +++ b/src/body-renderer.js @@ -10,7 +10,6 @@ export default class BodyRenderer { this.bodyScrollable = instance.bodyScrollable; this.footer = this.instance.footer; this.log = instance.log; - this.hooks = instance.hooks; } renderRows(rows) {