From 5ff5bb686c785a81f4ebc7906a208e8e80e7b51b Mon Sep 17 00:00:00 2001 From: Pierre Rousseau Date: Mon, 5 Dec 2022 13:16:23 +0000 Subject: [PATCH] [IMP] spreadsheet: include first row grouping at insertion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With this revision, the label of the field of the first row groupby is inserted as the row title. Task-id 2901960 closes odoo/odoo#107220 Related: odoo/enterprise#37395 Signed-off-by: Rémi Rahir (rar) --- .../static/src/pivot/pivot_model.js | 5 +++- .../static/src/pivot/pivot_table.js | 10 +++++++- .../src/pivot/plugins/pivot_core_plugin.js | 17 ++++++++++---- .../tests/pivots/model/pivot_plugin_test.js | 23 +++++++++++++++++++ .../spreadsheet/static/tests/utils/pivot.js | 3 ++- 5 files changed, 50 insertions(+), 8 deletions(-) diff --git a/addons/spreadsheet/static/src/pivot/pivot_model.js b/addons/spreadsheet/static/src/pivot/pivot_model.js index 31cd19a1479..69045656e2c 100644 --- a/addons/spreadsheet/static/src/pivot/pivot_model.js +++ b/addons/spreadsheet/static/src/pivot/pivot_model.js @@ -380,7 +380,10 @@ export class SpreadsheetPivotModel extends PivotModel { const rows = this._getSpreadsheetRows(this.data.rowGroupTree); rows.push(rows.shift()); //Put the Total row at the end. const measures = this.metaData.activeMeasures; - return new SpreadsheetPivotTable(cols, rows, measures); + const rowTitle = this.metaData.rowGroupBys[0] + ? this.getFormattedGroupBy(this.metaData.rowGroupBys[0]) + : ""; + return new SpreadsheetPivotTable(cols, rows, measures, rowTitle); } //-------------------------------------------------------------------------- diff --git a/addons/spreadsheet/static/src/pivot/pivot_table.js b/addons/spreadsheet/static/src/pivot/pivot_table.js index afe3748641c..28bf9475908 100644 --- a/addons/spreadsheet/static/src/pivot/pivot_table.js +++ b/addons/spreadsheet/static/src/pivot/pivot_table.js @@ -15,6 +15,7 @@ * @property {Column[][]} cols * @property {Row[]} rows * @property {string[]} measures + * @property {string} rowTitle */ /** @@ -64,11 +65,13 @@ export class SpreadsheetPivotTable { * @param {Column[][]} cols * @param {Row[]} rows * @param {string[]} measures + * @param {string} rowTitle */ - constructor(cols, rows, measures) { + constructor(cols, rows, measures, rowTitle = "") { this._cols = cols; this._rows = rows; this._measures = measures; + this._rowTitle = rowTitle; } /** @@ -168,6 +171,10 @@ export class SpreadsheetPivotTable { return this._rows[index]; } + getRowTitle() { + return this._rowTitle; + } + /** * @returns {SpreadsheetTableData} */ @@ -176,6 +183,7 @@ export class SpreadsheetPivotTable { cols: this._cols, rows: this._rows, measures: this._measures, + rowTitle: this._rowTitle, }; } } diff --git a/addons/spreadsheet/static/src/pivot/plugins/pivot_core_plugin.js b/addons/spreadsheet/static/src/pivot/plugins/pivot_core_plugin.js index 01176411880..161e0fb3dcd 100644 --- a/addons/spreadsheet/static/src/pivot/plugins/pivot_core_plugin.js +++ b/addons/spreadsheet/static/src/pivot/plugins/pivot_core_plugin.js @@ -90,8 +90,8 @@ export default class PivotCorePlugin extends CorePlugin { const { sheetId, col, row, id, definition, dataSourceId } = cmd; /** @type [number,number] */ const anchor = [col, row]; - const { cols, rows, measures } = cmd.table; - const table = new SpreadsheetPivotTable(cols, rows, measures); + const { cols, rows, measures, rowTitle } = cmd.table; + const table = new SpreadsheetPivotTable(cols, rows, measures, rowTitle); this._addPivot(id, definition, dataSourceId); this._insertPivot(sheetId, anchor, id, table); this.history.update("nextId", parseInt(id, 10) + 1); @@ -101,8 +101,8 @@ export default class PivotCorePlugin extends CorePlugin { const { sheetId, col, row, id } = cmd; /** @type [number,number] */ const anchor = [col, row]; - const { cols, rows, measures } = cmd.table; - const table = new SpreadsheetPivotTable(cols, rows, measures); + const { cols, rows, measures, rowTitle } = cmd.table; + const table = new SpreadsheetPivotTable(cols, rows, measures, rowTitle); this._insertPivot(sheetId, anchor, id, table); break; } @@ -429,8 +429,15 @@ export default class PivotCorePlugin extends CorePlugin { * @param {SpreadsheetPivotTable} table */ _insertRows(sheetId, anchor, id, table) { - let y = anchor[1] + table.getColHeight(); + let y = anchor[1] + table.getColHeight() - 1; const x = anchor[0]; + this.dispatch("UPDATE_CELL", { + col: x, + row: y, + sheetId, + content: table.getRowTitle(), + }); + y++; for (const row of table.getRowHeaders()) { const args = [id]; for (let i = 0; i < row.fields.length; i++) { diff --git a/addons/spreadsheet/static/tests/pivots/model/pivot_plugin_test.js b/addons/spreadsheet/static/tests/pivots/model/pivot_plugin_test.js index 245aea42a78..aeff022a818 100644 --- a/addons/spreadsheet/static/tests/pivots/model/pivot_plugin_test.js +++ b/addons/spreadsheet/static/tests/pivots/model/pivot_plugin_test.js @@ -754,4 +754,27 @@ QUnit.module("spreadsheet > pivot plugin", {}, () => { assert.equal(cell.error.message, "ya done!"); } ); + + QUnit.test("Title of the first row is inserted as row title", async (assert) => { + const { model } = await createSpreadsheetWithPivot({ + arch: /*xml*/ ` + + + `, + }); + assert.strictEqual(getCellContent(model, "A2"), "Bar"); + }); + + QUnit.test( + "Title of the first row is not inserted if there is no row group bys", + async (assert) => { + const { model } = await createSpreadsheetWithPivot({ + arch: /*xml*/ ` + + + `, + }); + assert.strictEqual(getCellContent(model, "A2"), ""); + } + ); }); diff --git a/addons/spreadsheet/static/tests/utils/pivot.js b/addons/spreadsheet/static/tests/utils/pivot.js index 29160d5ceff..099ab150cf9 100644 --- a/addons/spreadsheet/static/tests/utils/pivot.js +++ b/addons/spreadsheet/static/tests/utils/pivot.js @@ -34,11 +34,12 @@ export async function insertPivotInSpreadsheet(model, params) { }; const dataSource = model.config.custom.dataSources.create(PivotDataSource, definition); await dataSource.load(); - const { cols, rows, measures } = dataSource.getTableStructure().export(); + const { cols, rows, measures, rowTitle } = dataSource.getTableStructure().export(); const table = { cols, rows, measures, + rowTitle, }; const [col, row] = params.anchor || [0, 0]; model.dispatch("INSERT_PIVOT", {