1

I want to get data of jqGrid in my controller. I am getting all data of jqGrid using this

$("#gridSourceKey").jqGrid('getGridParam', 'data')

but I want filtered data in JSON format. How can I get it?

Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
  • I thought that jqGrid *get already* the data from the controller. Why you need to send **the same data** back to the server? Probably you need to have ids of filtered rows only? The server can get the other information directly from the database. Moreover **it's really important to know which fork of jqGrid you use and in which version**. – Oleg May 07 '15 at 10:15
  • Let me put in this way. I have two chunks of code 1) var gridData = $("#grid").jqGrid('getGridParam','data'); var postData = JSON.stringify(gridData); alert("gridData- "+postData); 2) var gridData1 = $("#grid").jqGrid('getRowData'); var postData1 = JSON.stringify(gridData1); alert("gridData- "+postData1); In first chunk I can parse grid data using JSON.stringify() and I can get json string which is in key-value pair format. In second chunk if I do JSON.stringify() it will give me html. How I can get json data from "gridData1"? Is there any other workaround? – Mohan Thakare May 07 '15 at 11:12
  • 1) If you want that I ask on your questions you should answer on my questions too. 2) `getRowData` can gives not full data if the required data are not on the current page. 3) `getRowData` get you **unformatted data**. If you don't correctly filled or used incorrect `colModel` properties (like custom `formatter` without `unformat`) then you can have the problem with HTML code fragments of the cells. 4) The results can be different in different versions of jqGrid 5) One can use `getDataIDs` to get ids of rows on the current page and to use `getLocalRow` to get data of *specific* rows. – Oleg May 07 '15 at 11:33
  • I am not able add new question. So I am writing my question here itself.function exportExcel() { var gridData = $("#grid").jqGrid ('getGridParam','data'); var postData = JSON.stringify(gridData); $.ajax({ type: "POST", contentType: 'application/json', url: "${exportExcelUrl}", data: postData, success: function(response) { if(response=="SUCCESS") { window.location.href='${downloadOfferListExcelUrl}'; } } }); } this is my code – Mohan Thakare May 07 '15 at 12:58
  • But I need only filtered data. So I write var gridData = $("#grid").jqGrid('getRowData'); If I call JSON.stringify(gridData) it will give me json key-value pair string. But in the value I will get html. Further I need to pass this json data to my controller which will not going to be work. Because controller want proper key-value pair(but not html in value) – Mohan Thakare May 07 '15 at 12:59
  • I am not able write new question because of some formatting issue. Sorry for inconvinience – Mohan Thakare May 07 '15 at 13:00
  • You can click on "edit" link below the text of your question and modify the text. Typically one **appends** the old text with the words "UPDATED:" which follows additional information. One writes additionally short comment to inform other people who wrote his comments or answers. New comment produces notification and other people can read the new information. – Oleg May 07 '15 at 13:03
  • About your question: You should add some information about jqGrid which you creates. It's very important always to know: which version of jqGrid from which fork ([free jqGrid](https://github.com/free-jqgrid/jqGrid), [Guriddo jqGrid JS](http://guriddo.net/?page_id=103334) or the old jqGrid in version <= 4.7) and which version you use. Which `datatype` have grid? Do you use `loadonce: true` (only in case of `datatype: "json"` or `datatype: "xml"`)? Do you use local paging of data or display all information on one page? ... – Oleg May 07 '15 at 13:08
  • some code chunk $("#grid").jqGrid({ url:'${recordsUrl}', datatype: 'json',loadonce: true, .... I will check which fork of jqgrid is used – Mohan Thakare May 07 '15 at 13:25
  • Fork is "jqGrid 4.3.2 - jQuery Grid" – Mohan Thakare May 08 '15 at 08:30

1 Answers1

3

The old version of jqGrid (jqGrid 4.3.2, which you use) provides no possibility to get filtered data. So you have to upgrade to free jqGrid and just use

$("#gridSourceKey").jqGrid('getGridParam', 'lastSelectedData')

instead of

$("#gridSourceKey").jqGrid('getGridParam', 'data')

Only if you really can't update jqGrid which you use then you should follow the tricky solution described in the answer. The solution consist from three steps: 1) overwriting (subclassing) the original internal jqGrid function $.jgrid.from to the function which saves the last filtering results in local lastSelected variable. 2) loadComplete which places the results from the local lastSelected variable to new custom jqGrid parameter lastSelected. 3) the usage of $("#gridSourceKey").jqGrid('getGridParam', 'lastSelected').

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks a lot Oleg. I am struggling for this issue since last 4 days. Daily I was banging my head on desk. Finally its working now. It is done by overwriting of the select method of $.jgrid.from. I will test it for other scenarios. A BIG THANKS TO YOU. – Mohan Thakare May 08 '15 at 10:15