Pages

Tuesday, February 26, 2013

Export Data to CSV in JQGRID










Code :


$(“#rdetail”).jqGrid(‘navGrid’, ‘#trdetail’,{view:true, del:false, add:false, edit:false, excel:true})
.navButtonAdd(‘#trdetail’,{
 caption:”,
 title:’export’,
 buttonicon:’ui-icon-newwin’,
 position:’last’,
 onClickButton:function (){
 exportExcel($(this),’main.php?action=receipt_excel’);
 }
});

//Craate function exportExcel

function exportExcel($id,path){
var keys=[], ii=0, rows=”";
var ids=$id.getDataIDs();       // Get All IDs
var row=$id.getRowData(ids[0]); // Get First row to get the labels
   for (var k in row) {
     keys[ii++]=k;    // capture col names
     rows=rows+k+”\t”;     // output each Column as tab delimited
    }
   rows=rows+”\n”;   // Output header with end of line
   for(i=0;i<ids.length;i++) {
   row=$id.getRowData(ids[i]); // get each row
   for(j=0;j<keys.length;j++) rows=rows+row[keys[j]]+”\t”; // output each Row as tab delimited
   rows=rows+”\n”;  // output each row with end of line
  }

rows=rows+”\n”;  // end of line at the end
var form = “<form name=’csvexportform’ action=’”+path+”‘ method=’post’>”;
form = form + “<input type=’hidden’ name=’csvBuffer’ value=’”+rows+”‘>”;
form = form + “</form><script>document.csvexportform.submit();</sc”+”ript>”;
OpenWindow=window.open(”, ”);
OpenWindow.document.write(form);
OpenWindow.document.close();
}

No comments:

Post a Comment