javascript - Exporting Specific jqGrid columns to Excel Sheet -
i exporting grid data excel sheet , working awesome. thing want include columns. following: id,name,assign date,check date,due date, notes. how this? here have far:
jqgrid:
$(function(){ $("#list").jqgrid({ url:'request.php', editurl: "jqgridcrud.php", datatype: 'xml', mtype: 'get', height: 530, width: 850, scrolloffset:0, colnames:['id','project', 'assigned to','assign date','check date','due date','attached','notes',''], colmodel :[ {name:'id', index:'id', width:25}, {name:'name', index:'name', width:235, align:'left',editable:true, editoptions:{ size:60} }, {name:'assigned to', index:'id_continent', width:55, align:'right',editable:true,edittype:'select', editoptions:{value: "henry:henry; ramon:ramon; paul:paul" },mtype:'post' }, {name:'assign date', index:'lastvisit', width:70, align:'right',formatter: 'date',srcformat:'yyyy-mm-dd',newformat: 'm/dd/yy',editable:true, edittype: 'text',mtype:'post' ,editoptions:{size:10, datainit:function(elem){$(elem).datepicker({dateformat:'m/dd/yy'});}}} , {name:'check date', index:'cdate', width:70, align:'right',formatter: 'date',srcformat:'yyyy-mm-dd',newformat: 'm/dd/yy', edittype: 'text',editable:true ,mtype:'post' ,editoptions:{size:10, datainit:function(elem){$(elem).datepicker({dateformat:'m/dd/yy'});}}} , {name:'due date', index:'ddate', width:70, align:'right',formatter: 'date',srcformat:'yyyy-mm-dd',newformat: 'm/dd/yy',editable:true, edittype: 'text',editoptions:{size:10, datainit:function(elem){$(elem).datepicker({dateformat:'m/dd/yy'});}}} , {name:'files', index:'email', width:40,align:'center',sortable:false,mtype:'post', formatter:function(cellvalue, options, rowobject){ return '<a href="' + cellvalue + '" target="_blank">files </a> ' } }, {name:'notes', index:'notes', width:100, align:'left',editable:true,edittype:'textarea', editoptions:{ rows:5,cols:60,maxlength:200} }, {name:'act',index:'act',width:25 ,align:'left', sortable:false,formatter: "actions",cellattr: function () { return ' title="delete project"'; }, formatoptions: { keys: true, delbutton: true, editbutton:false, deloptions: { url: 'jqgridcrud.php', aftershowform: function ($form) { $("#ddata", $form.parent()).click(); }, beforeshowform: function ($form) { $("td.delmsg", $form[0]).html("do want delete row <b>id=" + $("#list").jqgrid('getgridparam','selrow') + "</b>?"); }, msg: "remove selected project?", bsubmit: "remove", bcancel: "cancel" } }}, ], pager: '#pager', rownum:20, rowlist:[20,40,80], sortname: 'id', sortorder: 'desc', viewrecords: true, gridview: true, caption: 'pending assignments', ondblclickrow: function(rowid) { $(this).jqgrid('viewgridrow', rowid, {width:550,height:550,recreateform:true, closeonescape:true,reloadaftersubmit:true, modal:true,mtype:'post',top:350,left: 30});} }); $.extend($.jgrid.nav, {delicon: "ui-icon-circle-check", deltext: "project complete", }); $("#list").jqgrid("navgrid", "#pager", { add: false, search: false, edit:false, refresh:false }) .navbuttonadd('#pager',{ caption:"export excel", buttonicon:"ui-icon-save", onclickbutton: function(){ exportexcel(); }, position:"last" }); // setup grid print capability. setprintgrid('list','pager','print'); });
excel export function:
function exportexcel() { var mya=new array(); mya=$("#list").getdataids(); // ids var data=$("#list").getrowdata(mya[0]); // first row labels var colnames=new array(); var ii=0; (var in data){colnames[ii++]=i;} // capture col names var html=""; var filtered = [ 6, 8]; for(k=0;k<colnames.length;k++) { if (filtered.indexof(k) >= 0) { html=html+colnames[k]+"\t"; // output each column tab delimited } html=html+"\n"; } // output header end of line for(i=0;i<mya.length;i++) { data=$("#list").getrowdata(mya[i]); // each row for(j=0;j<colnames.length;j++) { if (filtered.indexof(j) >= 0){ html=html+data[colnames[j]]+"\t"; // output each row tab delimited } html=html+"\n"; // output each row end of line }} html=html+"\n"; // end of line @ end document.forms[0].csvbuffer.value=html; document.forms[0].method='post'; document.forms[0].action='csvexport.php'; // send server open contents in excel file document.forms[0].target='_blank'; document.forms[0].submit(); }
php on server:
<?php header('content-type: application/vnd.ms-excel'); header("content-disposition: attachment; filename=current-projects.xls"); header("pragma: no-cache"); $buffer = $_post['csvbuffer']; try{ echo $buffer; }catch(exception $e){ } ?>
based on code, modify exportexcel
filter out columns not want. simple array of indices want filter output. if current value of j
or k
filtered, not include column in header or data output.
for example, filter header rows using indexof array function:
var filtered = [2, 6, 8]; ... for(k=0;k<colnames.length;k++) { if (filtered.indexof(k) >= 0){ html=html+colnames[k]+"\t"; // output each column tab delimited } html=html+"\n"; }
you can use same type of code filter data itself.
does help?
Comments
Post a Comment