Skip to content
Snippets Groups Projects
Commit a9f0d999 authored by legac's avatar legac
Browse files

Add the grid mixins ExportCSV and ExportLatex.

parent 735c8b44
No related branches found
No related tags found
No related merge requests found
......@@ -13,6 +13,10 @@ Ext.define('App.grid.Panel', {
extend: 'Ext.grid.Panel',
alias: 'widget.xgrid',
mixins: {
csv: 'App.grid.mixin.ExportCSV',
latex: 'App.grid.mixin.ExportLatex'
},
requires: [
'Ext.grid.column.*',
'Ext.grid.feature.*',
......
/**
* Base class to export the content of a App.grid.Panel.
*
* @since 0.6.1.18
*
*/
Ext.define('App.grid.mixin.ExportBase', {
//
// Get the label and the alignment of the visible columns.
//
// @return {Object}
// @return {String[]} return.alignments
// @return {Boolean} return.hidden
// @return {String[]} return.labels empty for hidden headers
//
//
getColumnHeader: function () {
"use strict";
var me = this,
alignments = [],
columns,
container,
i,
label,
labels = [];
container = me.getDockedItems('headercontainer');
if (container.length) {
columns = container[0].getVisibleGridColumns();
for (i = 0; i < columns.length; i += 1) {
// the label of the RowNumberer column is a non breaking space
// replace it by an empty space to avoid crash in latex processing
label = columns[i].text;
if (label === "&#160") {
label = "";
}
labels.push(label);
if (columns[i].align) {
alignments.push(columns[i].align);
} else {
alignments.push('left');
}
}
}
return {
alignments: alignments,
hidden: container[0].isHidden(),
labels: labels
};
}
});
\ No newline at end of file
/**
* This class is used as a mixin.
* The main method translate the Ext.grid.View content into a CSV string.
*
* @since 0.6.1.15
*
*/
Ext.define('App.grid.mixin.ExportCSV', {
extend: 'App.grid.mixin.ExportBase',
config: {
/**
* @cfg {String}
* The CSV delimiter for string.
*/
delimiter: '"',
/**
* @cfg {String}
* The CSV field separator.
*
*/
separator: ','
},
//
// Surround the value with the CSV delimiter.
// Delimiter are only applied to non-numeric value.
//
// @param {String/Number} value
//
//
delimit: function (value) {
"use strict";
if (!Ext.isNumeric(value)) {
value = this.delimiter + value + this.delimiter;
}
return value;
},
/**
* Convert the Ext.grid.View content into a list of strings.
*
* @return {String[]} One string per line.
*
*/
toCSV: function () {
"use strict";
var me = this,
cells,
columnHeader,
csv = [],
labels = [],
div,
i,
j,
tdEls,
trEl,
trEls,
row,
total;
// build the row containing the column labels
columnHeader = this.getColumnHeader();
if (!columnHeader.hidden) {
for (i = 0; i < columnHeader.labels.length; i += 1) {
labels.push(this.delimit(columnHeader.labels[i]));
}
csv.push(labels.join(this.separator));
}
//
// convert the HTML row associated to the Ext.grid.View
// The structure of the HTML table is complex because it contains
// several types of row depending on the grouping features.
//
trEls = me.getView().getEl().select("table tr");
// process the trEls Ext.dom.CompositeElement
for (i = 0; i < trEls.getCount(); i += 1) {
trEl = trEls.item(i);
// grouping row
if (trEl.hasCls("x-grid-wrap-row")) {
div = trEl.down(".x-grid-group-title");
if (div) {
csv.push(this.delimit(div.dom.textContent));
}
// row with standard data cells. It includes summary row.
} else if (trEl.hasCls("x-grid-data-row") || trEl.hasCls("x-grid-row-summary")) {
cells = [];
tdEls = trEl.select("td");
// process the tdEls Ext.dom.CompositeElement
for (j = 0; j < tdEls.getCount(); j += 1) {
cells.push(this.delimit(tdEls.item(j).dom.textContent));
}
row = cells.join(this.separator);
// unfortunately the grand total appear as the first row (Ext JS 4.2)
if (i === 0 && trEl.hasCls("x-grid-row-summary")) {
total = row;
} else {
csv.push(row);
}
}
}
// push the grand total at the end
if (total) {
csv.push(total);
}
return csv;
}
});
\ No newline at end of file
/**
* This class is used as a mixin.
* The main method translate the Ext.grid.View content into a latex string.
*
* @since 0.6.1.15
*
*/
Ext.define('App.grid.mixin.ExportLatex', {
extend: 'App.grid.mixin.ExportBase',
/**
* Convert the Ext.grid.View content into a string.
* The information are encoded using LaTeX standard.
*
* @return {String[]} One string per line.
*
*/
toLaTeX: function () {
"use strict";
var me = this,
cells,
cmd,
columnsAlignment = '',
columnHeader,
div,
i,
j,
latex = [],
nColumns,
tdEl,
tdEls,
trEl,
trEls,
row,
total;
// build the column alignment, e.g. llrrc
columnHeader = this.getColumnHeader();
columnHeader.alignments.forEach(function (value) {
columnsAlignment += value[0];
});
// alias
nColumns = columnHeader.alignments.length;
// begin table
latex.push('\\begin{longtable}{' + columnsAlignment + '}');
// table header
if (!columnHeader.hidden) {
latex.push(columnHeader.labels.join(' & ') + ' \\\\');
latex.push('\\hline\\hline');
}
// table content
//
// convert the HTML row associated to the Ext.grid.View.
// The structure of the HTML table is complex because it contains
// several type of row depending on the grouping features.
//
trEls = me.getView().getEl().select("table tr");
// process the trEls Ext.dom.CompositeElement
for (i = 0; i < trEls.getCount(); i += 1) {
trEl = trEls.item(i);
// grouping row. It is represent as a latex multicolumn
if (trEl.hasCls("x-grid-wrap-row")) {
div = trEl.down(".x-grid-group-title");
if (div) {
cmd = '\\multicolumn{' + nColumns + '}{l}{' + div.dom.textContent + '} \\\\';
latex.push(cmd);
latex.push('\\hline');
}
// row with standard data cells. It includes summary row.
} else if (trEl.hasCls("x-grid-data-row") || trEl.hasCls("x-grid-row-summary")) {
cells = [];
tdEls = trEl.select("td");
// process the tdEls Ext.dom.CompositeElement
for (j = 0; j < tdEls.getCount(); j += 1) {
// shrink empty string to avoid latex processing failure
tdEl = tdEls.item(j).dom.textContent.trim();
cells.push(tdEl);
}
row = cells.join(' & ') + ' \\\\';
// unfortunately the grand total appear as the first row (Ext JS 4.2)
if (i === 0 && trEl.hasCls("x-grid-row-summary")) {
total = row;
} else {
latex.push(row);
}
}
}
// push the grand total at the end
if (total) {
latex.push('\\hline\\hline');
latex.push(total);
}
//end table
latex.push('\\end{longtable}');
return latex;
}
});
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment