<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
	<title>jQuery Grid Plugin - jqGrid - Topic: Exporting to CSV from the Grid - another answer</title>
	<link>http://www.trirand.com/blog/?page_id=393/help/exporting-to-csv-from-the-grid-another-answer</link>
	<description><![CDATA[Grid plugin]]></description>
	<generator>Simple:Press Version 5.7.5.3</generator>
	<atom:link href="http://www.trirand.com/blog/?page_id=393/help/exporting-to-csv-from-the-grid-another-answer/rss" rel="self" type="application/rss+xml" />
        <item>
        	<title>loudmouthman on Exporting to CSV from the Grid - another answer</title>
        	<link>http://www.trirand.com/blog/?page_id=393/help/exporting-to-csv-from-the-grid-another-answer#p14522</link>
        	<category>Help</category>
        	<guid isPermaLink="true">http://www.trirand.com/blog/?page_id=393/help/exporting-to-csv-from-the-grid-another-answer#p14522</guid>
        	        	<description><![CDATA[<p>here is my Answer to the question, how do I export to a CSV file from the grid.</p>
</p>
<p>&#8211; Javascript to define the table, note the window.location.href is basically calling the same URL with the query string that a grid reload is, however it is not doing it inside Ajax which means your getting all the headers back into the browser.</p>
</p>
<p>&#160;jQuery("#listings").jqGrid({<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; url:"&#60;? echo $urlBase; ?&#62;ajax/loadgrid_searches.php?" + new Date().getTime(),</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8230;</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; ).navButtonAdd(&#39;pager&#39;,{caption:"",title:"Export",buttonicon:"ui-icon-disk",onClickButton:function(){</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; var postData =&#160; $("#listings").jqGrid(&#39;getGridParam&#39;,&#39;postData&#39;);<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; var str=&#39;&#39;;<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; for(i in postData)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; str+=i+"="+postData[i]+"&#38;";<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; window.location.href=( "&#60;? echo $urlBase; ?&#62;ajax/loadgrid_searches.php?export=excel&#38;"+ str + new Date().getTime());<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; },<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; position:"last"<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; });</p>
</p>
<p>&#8211; The PHP File which loads the Grid a few comments.</p>
<p>&#160;&#8211; You may not wish to&#160; want to set the LIMIT on the export</p>
<p>$lt =&#160; "&#160; LIMIT&#160;&#160; "&#160; .&#160; $start .&#160; "&#160; , "&#160; .&#160;&#160; $limit ;</p>
<p>if( $_REQUEST[&#39;export&#39;]==&#39;excel&#39;){<br />$file="export_searches.csv";<br />header("Content-Disposition: attachment; filename=" . urlencode($file));<br />header("Content-Type: application/force-download");<br />header("Content-Type: application/octet-stream");<br />header("Content-Type: application/download");<br />header("Content-Description: File Transfer");<br />header("Content-Type: text/x-csv");<br />#header("Content-Length: " . strlen($export));<br />$lt =&#160; ""&#160; ;<br />flush();<br />}</p>
<p>In the SQL string that later follows you can pass $lt to the end of the string</p>
</p>
<p>When iterating around the rows returning the results, either build a result response or echo the results &#8230; made real basic here</p>
<p>if( $_REQUEST[&#39;export&#39;]==&#39;excel&#39;){<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; echo """&#160; . $row[&#39;orderdate&#39;] . "","" . $row[&#39;orderid&#39;] . "","" .$row[&#39;reqno&#39;] . "","" .$row[&#39;ordertype&#39;] . "","" .$row[&#39;title&#39;] . "","" .$row[&#39;phone&#39;] . "","" .$row[&#39;sitecode&#39;] . "","" .$row[&#39;material&#39;] . "","" .$row[&#39;description&#39;] . "","" .$row[&#39;qty&#39;] . "","" .$row[&#39;unit&#39;] . "","" .$row[&#39;price&#39;] . "","" .$row[&#39;discount&#39;] . "","" .$row[&#39;cost&#39;] . "","" .$row[&#39;buyer&#39;] . "","" .$row[&#39;duedate&#39;].""";<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; echo "rn" ;<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }else{<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $result-&#62;rows[$i][&#39;id&#39;]=$row[&#39;ohrecordno&#39;];<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $result-&#62;rows[$i][&#39;cell&#39;]=array($row[&#39;orderdate&#39;],$row[&#39;orderid&#39;],$row[&#39;reqno&#39;],$row[&#39;ordertype&#39;],$row[&#39;title&#39;],$row[&#39;phone&#39;],$row[&#39;sitecode&#39;],$row[&#39;material&#39;],$row[&#39;description&#39;],$row[&#39;qty&#39;],$row[&#39;unit&#39;],$row[&#39;price&#39;],$row[&#39;discount&#39;],$row[&#39;cost&#39;],$row[&#39;buyer&#39;],$row[&#39;duedate&#39;]);<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $i++;<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p>at the end of the Loadgrid</p>
<p>if(! $_REQUEST[&#39;export&#39;]==&#39;excel&#39;){<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; echo json_encode($result);<br />}else{<br />exit;<br />}</p>
</p>
<p>There is probably a more elegant way but if like me youve been searching for some help then this might give you some direction.</p>
<p>Hope it helped.</p></p>
]]></description>
        	        	<pubDate>Wed, 03 Feb 2010 18:41:35 +0200</pubDate>
        </item>
</channel>
</rss>