Κυριακή 13 Μαρτίου 2011

Extract HTML table's data in a new window

The question:
How could I extract the data that already rendered as a HTML Table in my web site, as a new Table that will be easily copied-pasted in an Excel spreadsheet?

The answer:

In case that our Table element has 'data' as id then we could use the following javascript function:




function showDataInNewWindow(){

try{

var tmp = '<table border="1"><thead><tr>';

$("#data th").each(function () { // iterate tr

tmp= tmp+'<th>'+$(this).text()+'</th>';

});

tmp=tmp+'</tr></thead><tbody>';

$('#data tr').each(function () { // iterate tr

tmp = tmp+' <tr>';

$(this).find('td').each(function () { // iterate td

tmp =tmp+ '<td>'+$(this).text()+'</td>';

});

tmp=tmp+'</tr>';

});

tmp=tmp+'</tbody></table>'

myWindow=window.open('','Data','width=800,height=600,left=0,top=0');

myWindow.document.write(tmp);

myWindow.document.close();

myWindow.focus();

}catch(e){

alert(e);

}

}


Notes:
1) jQuery library has been used
2) For more information leave a comment and I will reply as soon as possible!

LinkWithin

Blog Widget by LinkWithin

Mobile edition