From 46e48f062f000336ae279ed0b58cc5d0ac7a5cfc Mon Sep 17 00:00:00 2001 From: ZitRos Date: Fri, 10 Feb 2017 13:42:50 +0200 Subject: [PATCH] lpt.getRowsValues, lpt.getModel, lpt.triggers.rowClick, lpt.triggers.rowSelect add --- package.json | 2 +- readme.md | 7 ++++++- source/js/LightPivotTable.js | 27 +++++++++++++++++++++++++++ source/js/PivotView.js | 16 ++++++++++++++-- 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 50bb9f2..cc767d3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "LightPivotTable", "author": "ZitRo", - "version": "1.7.1", + "version": "1.7.4", "description": "A lightweight pivot table for MDX2JSON source for InterSystems Cache", "main": "test/testServer.js", "repository": { diff --git a/readme.md b/readme.md index fc10be0..0e74c53 100644 --- a/readme.md +++ b/readme.md @@ -53,7 +53,10 @@ var setup = { // Object that contain settings. Properties in brackets can be mis // if cellDrillThrough callback returns boolean false, DrillThrough won't be performed. , cellDrillThrough: function ({Object { event: {event}, filters: {string[]}, cellData: {object} }}) {} , responseHandler: function ({Object {url: {string}, status: {number}}}) {} - , rowSelect: function ({Array}) {} + // triggers when row selected in listing: + , rowSelect: function (row, rowData) { console.log("Row #", row, rowData); } + // if rowClick callback returns boolean false, DrillDown won't be performed. + , rowClick: function (row, rowData) { console.log(row, rowData); } , contentRendered: function () {} , cellSelected: function ({ x: Number, y: Number, leftHeaderColumnsNumber: Number, topHeaderRowsNumber: Number }) { return false; // return false to block default click action @@ -98,6 +101,8 @@ lp.refresh(); // refresh pivot contents lp.updateSizes(); // recalculate pivot sizes lp.changeBasicMDX("..."); // change mdx for LPT lp.getActualMDX(); // returns currently displayed MDX +var rows = lp.getRowsValues([1,2,3]); // returns the values in rows 1, 2, 3 +var model = lp.getModel(); // returns data model representing currently rendered data set lp.getSelectedRows(); // returns array with selected rows indexes. First row have index 1. lp.attachTrigger("contentRendered", function (lptInstance) { }); // attaches trigger during runtime lp.setRowCount(5); // sets the number of rows to display diff --git a/source/js/LightPivotTable.js b/source/js/LightPivotTable.js index 42a709d..328f5a6 100644 --- a/source/js/LightPivotTable.js +++ b/source/js/LightPivotTable.js @@ -144,6 +144,33 @@ LightPivotTable.prototype.getSelectedRows = function () { }; +/** + * Return array of passed rows values. + * @param {Number[]} rows - Rows indices in the table. Index 1 points to the first row with data. + * @returns {*[]} + */ +LightPivotTable.prototype.getRowsValues = function (rows) { + if (typeof rows === "undefined") + return []; + if (!(rows instanceof Array)) + rows = [rows]; + if (rows.length === 0) + return []; + var d = this.dataController.getData(), + arr = []; + for (var i = 0; i < rows.length; i++) { + arr.push(d.rawData[rows[i] - 1 + (d.info.topHeaderRowsNumber || 1)]); + } + return arr; +}; + +/** + * Returns currently rendered model. + */ +LightPivotTable.prototype.getModel = function () { + return this.dataController.getData(); +}; + /** * Performs resizing. */ diff --git a/source/js/PivotView.js b/source/js/PivotView.js index fe3ff61..1a5848e 100644 --- a/source/js/PivotView.js +++ b/source/js/PivotView.js @@ -364,7 +364,15 @@ PivotView.prototype._columnClickHandler = function (columnIndex) { PivotView.prototype._rowClickHandler = function (rowIndex, cellData) { - this.controller.tryDrillDown(cellData.source.path); + var res = true; + if (typeof this.controller.CONFIG.triggers["rowClick"] === "function") { + var d = this.controller.getRowsValues([rowIndex])[0].slice( + this.controller.dataController.getData().info.leftHeaderColumnsNumber + ); + res = this.controller.CONFIG.triggers["rowClick"](rowIndex, d); + } + if (res !== false) + this.controller.tryDrillDown(cellData.source.path); }; @@ -727,7 +735,11 @@ PivotView.prototype.selectRow = function (select, rowNumber) { delete this.selectedRows[rowNumber]; if (typeof this.controller.CONFIG.triggers["rowSelect"] === "function") { - this.controller.CONFIG.triggers["rowSelect"](this.controller.getSelectedRows()); + var rows = this.controller.getSelectedRows(); + this.controller.CONFIG.triggers["rowSelect"]( + rows, + this.controller.getRowsValues(rows) + ); } };