Reputation: 149
All the articles on the internet states that a server side call is required to export jqgrid to excel. I am looking for a mechanism where I could export once loaded grid to excel without having to make a call. How can I achieve that ?
Upvotes: 2
Views: 978
Reputation: 1256
You could capture a click event of an Export button and then loop through the jqgrid through javascript below:
$(':button[id=ExportExcel]').click(function () {
for (iRow = 0; iRow < cRows; iRow++) {
row = rows[iRow];
if ($(row).hasClass("jqgrow")) {
cellsOfRow = row.cells;
$(cellsOfRow[iCol]).text()
// construct your html table here and then, combining code from below
}
}:
Then use similar code to below to populate your table ready for export, but use the jqgrid data as the source instead of the html table created below.
function fnExcelReport()
{
var tab_text="<table><tr>";
var textRange;
tab = document.getElementById('TableData'); // id of actual table on your page
for(j = 0 ; j < tab.rows.length ; j++)
{
tab_text=tab_text+tab.rows[j].innerHTML;
tab_text=tab_text+"</tr><tr>";
}
tab_text = tab_text+"</tr></table>";
var txt = document.getElementById('txtArea1').contentWindow;
txt.document.open("txt/html","replace");
txt.document.write(tab_text);
txt.document.close();
txt.focus();
tb = txt.document.execCommand("SaveAs",true,"Say Thanks to Sumit.xls");
return (tb);
}
Upvotes: 1