Hi,
I tri to use my function to export all input elements in datatables (same function of datatables with few changes), but but the problème is that whene i change an element and change page, if clic export the vale of element changed is not correcte. i have the initial data.
Can you help me ?
sorry about my English
code :
$.fn.dataTable.Api.register( 'buttons.exportData()', function ( options ) {
if ( this.context.length ) {
var dt = new $.fn.dataTable.Api(this.context[0]);
var _exportTextarea = $('<textarea/>')[0];
var config = $.extend( true, {}, {
rows: null,
columns: '',
modifier: {
search: 'applied',
order: 'applied'
},
orthogonal: 'display',
stripHtml: true,
stripNewlines: true,
decodeEntities: true,
trim: true,
format: {
header: function ( d ) {
return strip( d );
},
footer: function ( d ) {
return strip( d );
},
body: function ( d ) {
return strip( d );
}
}
}, options );
var strip = function ( str ) {
if ( typeof str !== 'string' ) {
return str;
}
if ( config.stripHtml ) {
str = str.replace( /<[^>]*>/g, '' );
}
if ( config.trim ) {
str = str.replace( /^\s+|\s+$/g, '' );
}
if ( config.stripNewlines ) {
str = str.replace( /\n/g, ' ' );
}
if ( config.decodeEntities ) {
_exportTextarea.innerHTML = str;
str = _exportTextarea.value;
}
if ($.isNumeric(str.replace( / /g, '' ))) {
//str = str.replace( / /g, '' );
str = str.replace(".", ',' );
}
return str;
};
var header = dt.columns( config.columns ).indexes().map( function (idx) {
var el = dt.column( idx ).header();
return config.format.header( el.innerHTML, idx, el );
} ).toArray();
var footer = dt.table().footer() ?
dt.columns( config.columns ).indexes().map( function (idx) {
var el = dt.column( idx ).footer();
return config.format.footer( el ? el.innerHTML : '', idx, el );
} ).toArray() :
null;
var rowIndexes = dt.rows( config.rows, config.modifier ).indexes().toArray();
var selectedCells = dt.cells( rowIndexes, config.columns );
var cells = selectedCells
.render( config.orthogonal )
.toArray();
var cellNodes = selectedCells
.nodes()
.toArray();
var columns = header.length;
var rows = columns > 0 ? cells.length / columns : 0;
var body = new Array( rows );
var cellCounter = 0;
for ( var i=0, ien=rows ; i<ien ; i++ ) {
var row = new Array( columns );
for ( var j=0 ; j<columns ; j++ ) {
var val = cellNodes[cellCounter] ? cellNodes[cellCounter].innerHTML : cells[cellCounter];
val = "<div>" + val + "</div>";
// priority is for select if not existe, get data from textbox if existe, if not get data from cell
var selectChield = $(val).children('select')[0];
if (selectChield) {
val = $(selectChield).find('option:selected').text();
} else {
selectChield = $(val).find('input').filter(':visible').not(':input[readonly]')[0];
if (selectChield) {
val = $(selectChield).val();
}
}
row[j] = config.format.body( val, i, j, cellNodes[ cellCounter ] );
cellCounter++;
}
body[i] = row;
}
return {
header: header,
footer: footer,
body: body
};
}
} );