Forum



18:21

20/04/2011

I am using jqgrid with MVC. I am trying to add export to excel functioanlity to the grid pager using a custom button add feature of jqgrid. Excel button is showing up in the pager. I have created a empty excel file and gave that URL.
When I click on excel button in the pager, excel is opening but jqgrid data is not exported. I think I am missing something or I am not using this feature correctly.
Could someone help on how to do export to excel (if possible PDF as well).
Your help is greatly appreciated.
Thanks
Miki
<table id="list2"></table>
<div id="pager2"></div>
<a href="#" id="a1">Get data from selected row</a>
<script type="text/javascript">
jQuery("#list2").jqGrid({
url: '/JsonProducts/GetJQGridJsonData/',
datatype: "json",
colNames: ['ProductId', 'ProductNumber', 'Size', 'ReleaseDate', 'Amount', 'Description','fileAttached'],
colModel: [
{ name: 'ProductId', index: 'ProductId', width: 100, sortable: true, sorttype: "int" },
{ name: 'ProductNumber', index: 'ProductNumber', width: 100, sortable: true, sorttype: "text" },
{ name: 'Size', index: 'Size', width: 100, sortable: true, sorttype: "text" },
{ name: 'ReleaseDate', index: 'ReleaseDate', width: 100, datefmt: "mm/dd/yyyy", sorttype: "date", sortable: true },
{ name: 'Amount', index: 'Amount', width: 100, sortable: true, sorttype: "float" },
{name: 'Description', hidden: true, index: 'Description', width: 100, sortable: false, editable: true, editrules: { edithidden: true} },
{name: 'fileAttached', index: 'fileAttached', align: "center", width: 40 }
],
rowNum: 20,
rowList: [10, 20, 30],
pager: '#pager2',
sortname: 'ProductId',
viewrecords: true,
sortorder: "desc",
caption: "Products List",
multiselect: true,
width: 800,
height: 600,
// toppager: true,
loadonce: true, // to enable sorting on client side
sortable: true //to enable sorting
});
jQuery("#list2").jqGrid('navGrid', '#pager2', { edit: false, add: false, del: false, view: true });
jQuery("#list2").jqGrid('filterToolbar', { stringResult: true, searchOnEnter: false });
jQuery("#list2").jqGrid('navButtonAdd', '#pager2', {
caption: "Export to Excel",
onClickButton: function () {
jQuery("#list2").jqGrid('excelExport', { url: '/Export/ExportExcel.xls' });
}
});
</script>
20:47

20/04/2011

I am proviidng my controller code to populate the grid. I have not written any code to export in the controller. I am assuming that, JQGrid will do the export functionality. If not would you please provide me a link or if there is any code published already in this help forum in case if you know.
This is my controller code to populate the grid:
public virtual JsonResult GetJQGridJsonData(int page, int rows, string search, string sidx, string sord)
{
List<Product> jqData = new List<Product>();
jqData = Repository.GetProducts();
int pageSize = rows;
int totalRecords = jqData.Count();
int totalPages;
if (totalRecords > 0)
totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
else
totalPages = 0;
// if for some reasons the requested page is greater than the total set the requested page to total page
if (page > totalPages) page = totalPages;
//total – Total number of Pages.
//page – Current page Index.
//records – Total number of records in the rows group.
//rows – An array with the data plus an identifier.
//id – The unique row identifier, needs to be an int from what I have found.
//cell – An array of the data for the grid.
var jsonData = new JqGridJsonData
{
total = totalPages,
page = page,
records = totalRecords,
rows = jqData.Select(vm =>
new JQGridRowJson
{
id = vm.ProductId.ToString(),
cell = new[]{
vm.ProductId.ToString(),
vm.ProductNumber,
vm.Size,
vm.ReleaseDate.ToShortDateString(),
vm.Amount.ToString(),
vm.Description,
}
}).ToArray()
};
//<a href="http://www.pageresource.com/linkus.htm" /> </a>
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
Most Users Ever Online: 715
Currently Online:
69 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.comModerators: tony: 7721, Rumen[Trirand]: 81
Administrators: admin: 66