Forum

November 2nd, 2014
A A A
Avatar

Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

The forums are currently locked and only available for read only access
sp_Feed Topic RSS sp_TopicIcon
Exporting to CSV from the Grid - another answer
03/02/2010
18:41
Avatar
loudmouthman
England
Member
Members
Forum Posts: 4
Member Since:
03/02/2010
sp_UserOfflineSmall Offline

here is my Answer to the question, how do I export to a CSV file from the grid.

– 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.

 jQuery("#listings").jqGrid({
        url:"<? echo $urlBase; ?>ajax/loadgrid_searches.php?" + new Date().getTime(),

         …

        ).navButtonAdd('pager',{caption:"",title:"Export",buttonicon:"ui-icon-disk",onClickButton:function(){

                                       var postData =  $("#listings").jqGrid('getGridParam','postData');
                                        var str='';
                                          for(i in postData)
                                                  str+=i+"="+postData[i]+"&";
                                          window.location.href=( "<? echo $urlBase; ?>ajax/loadgrid_searches.php?export=excel&"+ str + new Date().getTime());
                                      },
                                position:"last"
                                });

– The PHP File which loads the Grid a few comments.

 – You may not wish to  want to set the LIMIT on the export

$lt =  "  LIMIT   "  .  $start .  "  , "  .   $limit ;

if( $_REQUEST['export']=='excel'){
$file="export_searches.csv";
header("Content-Disposition: attachment; filename=" . urlencode($file));
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Type: text/x-csv");
#header("Content-Length: " . strlen($export));
$lt =  ""  ;
flush();
}

In the SQL string that later follows you can pass $lt to the end of the string

When iterating around the rows returning the results, either build a result response or echo the results … made real basic here

if( $_REQUEST['export']=='excel'){
                echo """  . $row['orderdate'] . "","" . $row['orderid'] . "","" .$row['reqno'] . "","" .$row['ordertype'] . "","" .$row['title'] . "","" .$row['phone'] . "","" .$row['sitecode'] . "","" .$row['material'] . "","" .$row['description'] . "","" .$row['qty'] . "","" .$row['unit'] . "","" .$row['price'] . "","" .$row['discount'] . "","" .$row['cost'] . "","" .$row['buyer'] . "","" .$row['duedate'].""";
                echo "rn" ;
        }else{
                $result->rows[$i]['id']=$row['ohrecordno'];
                $result->rows[$i]['cell']=array($row['orderdate'],$row['orderid'],$row['reqno'],$row['ordertype'],$row['title'],$row['phone'],$row['sitecode'],$row['material'],$row['description'],$row['qty'],$row['unit'],$row['price'],$row['discount'],$row['cost'],$row['buyer'],$row['duedate']);
        }
        $i++;
        }

at the end of the Loadgrid

if(! $_REQUEST['export']=='excel'){
        echo json_encode($result);
}else{
exit;
}

There is probably a more elegant way but if like me youve been searching for some help then this might give you some direction.

Hope it helped.

Forum Timezone: Europe/Sofia

Most Users Ever Online: 715

Currently Online:
86 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

OlegK: 1255

markw65: 179

kobruleht: 144

phicarre: 132

YamilBracho: 124

Renso: 118

Member Stats:

Guest Posters: 447

Members: 11373

Moderators: 2

Admins: 1

Forum Stats:

Groups: 1

Forums: 8

Topics: 10592

Posts: 31289

Newest Members:

, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.com

Moderators: tony: 7721, Rumen[Trirand]: 81

Administrators: admin: 66

Comments are closed.
Privacy Policy   Terms and Conditions   Contact Information