我试图将 jqgrida 数据导出到 excel 或 csv 文件,我正在按照这个示例 php + jqgrid + 导出到 excel,但是在我单击按钮导出数据后没有任何反应。这是代码..
这是我的list.phtml,我在其中渲染网格,一旦我按下按钮导出做excel,它就会从jqgrid中获取数据并将其发送到操作/checkins/export。
<div class="simplebox grid740" style="z-index: 500;">
<script>
$(function() {
$("#toolbar").jqGrid({
caption:"Checkins",
colNames:['ID','Nome da Tag','Localização da Tag','Cliente','Utilizador','Data da Leitura','Data da Escrita',],
colModel:[
{name:'id_checkin',index:'id_checkin',hidden:true},
{name:'tag_nome',index:'tag_nome'},
{name:'localizacao',index:'localizacao'},
{name:'nome_cli',index:'nome_cli'},
{name:'nome_user',index:'nome_user'},
{name:'data_leitura',index:'data_leitura'},
{name:'data_escrita',index:'data_escrita'},
],
datatype:"json",
height:421,
rownumWidth:40,
pager:'#ptoolbar',
rowList:[10,20,30],
rowNum:10,
sortname:'id_cliente',
sortorder:'desc',
url:'/checkins/list/',
viewrecords:true,
width:740,
});
$("#toolbar").jqGrid('navGrid','#ptoolbar',{del:false,add:false,edit:false,search:false,excel:true});
jQuery("#toolbar").jqGrid('navButtonAdd','#ptoolbar',{
caption:"export",
onClickButton : function () {
alert('export to excel');
var mya=new Array();
mya=$("#toolbar").getDataIDs(); // Get All IDs
var data=$("#toolbar").getRowData(mya[0]); // Get First row to get the labels
var colNames=new Array();
var ii=0;
for (var i in data){colNames[ii++]=i;} // capture col names
var html="";
for(k=0;k<colNames.length;k++)
{
html=html+colNames[k]+"\t"; // output each Column as tab delimited
}
html=html+"\n"; // Output header with end of line
for(i=0;i<mya.length;i++)
{
data=$("#toolbar").getRowData(mya[i]); // get each row
for(j=0;j<colNames.length;j++)
{
html=html+data[colNames[j]]+"\t"; // output each Row as tab delimited
}
html=html+"\n"; // output each row with end of line
}
html=html+"\n"; // end of line at the end
$.ajax({
url: '/checkins/export',
type: 'POST',
data: {param1: html},
datatype: "json"
});
}
});
});
</script>
<table id="toolbar"></table>
<div id="ptoolbar"></div>
</div>
所以根据萤火虫它发送这个:param1:
id_checkin tag_nome localizacao nome_cli nome_user data_leitura data_escrita
1 sasa Maia;波尔图; 特雷特1212;管理员 2013-08-14 15:45:00 2013-08-14 16:00:00
2 莎莎玛雅;波尔图; 特雷特1212;树 2013-08-14 16:06:00 2013-08-14 16:05:00
3 莎莎玛雅;波尔图; 特雷特1212;测试 2013-08-14 12:15:00 2013-08-14 16:15:00
在我的操作 /checkins/export 中我这样做:
public function exportAction()
{
if($this->_request->isXmlHttpRequest())
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$param1 = $this->_request->getParam('param1');
header("Content-Type: application/vnd.ms-excel");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=contacts-download-" . time() . ".xls");
$buffer = $param1;
try{
echo $buffer;
}catch(Exception $e){
}
}
}
在这里,我接收 param1 信息并设置标题,然后回显数据。但是要求下载文件的框没有出现,我尝试了其他解决方案,但没有任何效果......我错过了什么。是否可以通过设置创建 excel 布局将视图呈现为 csv/excel 文件?提前感谢雨果