PDA

View Full Version : Add exportRenderer to pivot



ogimenezb
10 Jan 2020, 7:17 AM
I have recently needed a custom render to be exported and have seen that pivot does not export custom renders

Would be nice if you can implement it.

I have made a simple override for this case

Thanks

Ext.define('Daemez.base.override.pivot.plugin.Exporter', {
override: 'Ext.pivot.plugin.Exporter',

/**
* @methodprepareData
* @inheritdoc
*/
prepareData: function(config) {
var me = this,
matrix, group, columns, headers, total, i, j, dLen, tLen,
dataIndexes, row, value;

me.matrix = matrix = me.cmp.getMatrix();
me.onlyExpandedNodes = (config && config.onlyExpandedNodes);

if (!me.onlyExpandedNodes) {
me.setColumnsExpanded(matrix.topAxis.getTree(), true);
}

columns = Ext.clone(matrix.getColumnHeaders());
headers = me.getColumnHeaders(columns, config);
dataIndexes = me.getDataIndexColumns(columns);

if (!me.onlyExpandedNodes) {
me.setColumnsExpanded(matrix.topAxis.getTree());
}

group = {
columns: headers,
groups: []
};
me.extractGroups(group, matrix.leftAxis.getTree(), dataIndexes);

tLen = matrix.totals.length;
dLen = dataIndexes.length;

if (tLen) {
group.summaries = [];

for (i = 0; i < tLen; i++) {
total = matrix.totals[i];

row = {
cells: [{
value: total.title
}]
};

for (j = 1; j < dLen; j++) {
value = total.record.data[dataIndexes[j].dataIndex];
if ((value != null || value !== 0) && dataIndexes[j].exportRenderer) {
value = dataIndexes[j].renderer(value);
}
row.cells.push({
value: (value == null || value === 0) && matrix.showZeroAsBlank ? '' : value
});
}

group.summaries.push(row);
}
}

me.matrix = me.onlyExpandedNodes = null;

return new Ext.exporter.data.Table(group);
},

/**
* Find all columns that have a dataIndex
*
* @paramcolumns
*
* @returns{Array}
*
* @private
*/
getDataIndexColumns: function(columns) {
var cols = [],
i, col, doExtract;

for (i = 0; i < columns.length; i++) {
col = columns[i];
doExtract = this.onlyExpandedNodes
? (!col.group || col.group.expanded || (!col.group.expanded && col.subTotal))
: true;

if (doExtract) {

if (col.dataIndex) {
cols.push({
renderer: col.dimension ? col.dimension.renderer ? col.dimension.renderer : null : null,
exportRenderer: col.dimension ? col.dimension.exportRenderer ? col.dimension.exportRenderer : false : false,
dataIndex: col.dataIndex,
agg: col.dimension ? col.dimension.getId() : null
});
}
else if (col.columns) {
Ext.Array.insert(cols, cols.length, this.getDataIndexColumns(col.columns));
}
}
}

return cols;
},

/**
* Extract data from left axis groups.
*
* @paramgroup
* @paramitems
* @paramcolumns
*
* @returns{Object}
*
* @private
*/
extractGroups: function(group, items, columns) {
var i, j, iLen, cLen, doExtract, item, row,
subGroup, record, value, cells;

iLen = items.length;

for (i = 0; i < iLen; i++) {
item = items[i];

if (item.record) {
group.rows = group.rows || [];

cells = [];
row = {
cells: cells
};

for (j = 0; j < columns.length; j++) {
value = item.record.data[columns[j].dataIndex];
if ((value != null || value !== 0) && columns[j].exportRenderer) {
value = columns[j].renderer(value);
}
cells.push({
// eslint-disable-next-line max-len
value: (value == null || value === 0) && this.matrix.showZeroAsBlank ? '' : value
});
}

group.rows.push(row);
}
else if (item.children) {
group.groups = group.groups || [];

subGroup = {
text: item.name
};
doExtract = this.onlyExpandedNodes ? item.expanded : true;

if (doExtract) {
this.extractGroups(subGroup, item.children, columns);
}

subGroup.summaries = [];
cells = [{
value: (doExtract ? item.getTextTotal() : item.value)
}];
row = {
cells: cells
};

record = (item.expanded ? item.records.expanded : item.records.collapsed);
cLen = columns.length;

for (j = 1; j < cLen; j++) {
value = record.data[columns[j].dataIndex];
if ((value != null || value !== 0) && columns[j].exportRenderer) {
value = columns[j].renderer(value);
}
cells.push({
// eslint-disable-next-line max-len
value: (value == null || value === 0) && this.matrix.showZeroAsBlank ? '' : value
});
}

subGroup.summaries.push(row);
group.groups.push(subGroup);
}
}
}
});