The 'getRows' method only returns the current page rows. You can get the data you expect before exporting to excel. If you issue continues, please show some code snippets to demonstrate your issue.
Hi Jarry,
Thanks for taking your time. My problem is the default page size is 10 rows, if user use filterbox to filter data and after filtered the data consists of 2 pages(14 rows for example), if user press the export csv button, it will only extract the 10 rows record in the 1st page. I need the excel to extract all 14 rows that's filtered by user instead of only 10 rows on the 1st page.
My current code snippets as below:
Javascript/Jquery:
dg.datagrid({
width:'auto',
height:'auto',
remoteSort:false,
nowrap:false,
fitColumns:true,
striped:true,
pagination:true,
multiSort:true,
url:'php/get_coo_data.php',
queryParams:{bpID:login},
columns:[[
{field:'item',title:'Item',width:40,sortable:true},
{field:'desc1',title:'Desciption',width:100,sortable:true},
{field:'prod_type',title:'Product Type',width:60,align:'center',sortable:true},
{field:'buyer_1',title:'Buyer 1',width:40,align:'center',sortable:true,editor:'text'},
{field:'buyer_2',title:'Buyer 2',width:40,align:'center',sortable:true,editor:'text'},
{field:'bpid',title:'BP',width:40,align:'center',sortable:true},
{field:'bpname',title:'BP Name',width:150,align:'center',sortable:true},
{field:'manufacturer',title:'Manufacturer',width:100,align:'center',editor:'text'},
{field:'state_1',title:'State 1',width:50,align:'center'},
{field:'country_1',title:'Country 1',width:50,align:'center'},
{field:'state_2',title:'State 2',width:50,align:'center'},
{field:'country_2',title:'Country 2',width:50,align:'center'},
{field:'state_3',title:'State 3',width:50,align:'center'},
{field:'country_3',title:'Country 3',width:50,align:'center'}
]]
});
dg.datagrid('enableFilter'); // enable filter
$(function(){
$('#btnExportCsv').bind('click', function(){
var rows = $('#dg').datagrid('getRows');
$('#dg').datagrid('toCsv',{
rows: rows,
filename: 'Material_coo.csv'
})
});
});
While my server side code:
$bpID = isset($_POST['bpID']) && $_POST['bpID'] !=='' ? htmlspecialchars($_POST['bpID']) : '';
##*** Sorting ***
$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'item';
$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';
##*** Pagination ***
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
$offset = ($page-1)*$rows;
##for easyui, sqlsvr pagination
$max = $rows*$page;
$result = array();
$whereSQL = isset($_POST['bpID']) && $_POST['bpID'] !=='' ? "WHERE TRIM(bpid) = '$bpID'" : '' ;
try{
$result = $link->query("SELECT COUNT(*) FROM item_country_origin $whereSQL");
$row = $result->fetch_row();
$response["total"] = $row[0];
} catch (mysqli_sql_exception $e) {
echo $e->__toString();
}
$sql = "SELECT * FROM item_country_origin $whereSQL ORDER BY $sort $order";
try{
$stmt = $link->prepare($sql);
$stmt->execute();
} catch (mysqli_sql_exception $e) {
echo $e->__toString();
}
$res="";
if (!($res = $stmt->get_result())) {
//echo "<br/>Getting result set failed: (" . $stmt->errno . ") " . $stmt->error;
}else{
$numOfRows = $res->num_rows;
//echo "number of rows: " . $numOfRows;
}
$users = array();
while(($row = $res->fetch_assoc()) !== null ){
array_push($users, $row);
}
$response["rows"] = $users;
echo json_encode($response);