How to get jqGrid filtered data in json format? - jqgrid

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?

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').

Related

jsGrid - get data typed by the user in a search navGrid

I'm trying to bind a table and a graph using d3 and jqGrid library. For that I have to get the search typed by the user in the searchbox (my table looks like this : http://www.guriddo.net/demo/guriddojs/)
I've found this function :
grid.getGridParam("postData").filters
but I don't know how to use it. I thought about the trigger event "jqGridToolbarAfterSearch" to get the data after each search but doesn't seems to work...
If someone has an idea I'll be very grateful!
Thanks.
Ps : if the same method exist to set data, I'm interested too.
I hope that I correctly understand your problem. I suppose that you first converts the CSV data of the demo to some more continent data format: array of items with some properties (name, economy, cylinders, displacement, power, weight, mph, year). Then you can use datatype: "local" and data as the input data. I suppose that the user apply the local filter and then you want to get the filtered data
If you use free jqGrid fork of jqGrid (it's the fork which I develop) then you can get lastSelectedData parameter (var filteredData = $grid.jqGrid("getGridParam", "lastSelectedData");) to have the array of filtered items (see the demo). After that you can use d3 with the filtered items.

Jqgrid afterSearch data - how to get it

I need to get only the data that is a result of the search as opposed to the entire set.
This code gives me the entire set:
var data = $("#my-grid").getGridParam("data");
The answer on your question depends on which fork of jqGrid and which version you use. If you use free jqGrid (4.8/4.9) then use lastSelectedData parameter instead of data. You will get the subset of filtered and sorted data (all data, not just the current page!!!). See the readme and the first demo at the end of readme.
If you have to use old jqGrid versions then you can follow the answer which shows how one can subclass internal method $.jgrid.from to be able to save the results of the last select over the local data.

Date Ranges in jqGrid Searches

We are using advanced search in the latest version of jqGrid, and our search dialog is configured to be always visible on the page above the grid. The structure of our data lists is dynamic. Thus, when we are going to display a list, we first do an ajax call to get the list of columns for the grid. We then construct the data model for the grid and make a request for the data.
Currently, in the request to get the columns, we return the data type of the column. If the data is a date, we display a date picker in the search form. However, some of our customers HATE having to use <= >= for date ranges. They want to be able to pick a date column and then set a start and end date using two side-by-side date pickers. I've been pushing them off for a while now because they have the ability to do date range searches, but the complaining isn't stopping. (It's more clicks to add the second filter with the end date)
Is there any way I can modify jqGrid to give me a date range control when I am configuring a search on a date column? I really don't want to have to set up an external search dialog UI just to deal with these complaints, but product-management is pushing really hard to get "normal" date ranges for the grids.
You can create your own custom search dialog. See this question which I asked couple days ago.
using setGridParam to change your postData array and include extra values in the filters JSON object that will be carried over to your server side where you can dissect it. In your case you can pass over your data range Start and End inside the filter item of the postData. Then reload your jqGrid like this
var $grid = $("#list');
//$grid.setGridParam({datatype:'json', page:1}).trigger('reloadGrid');
var post_data = {searchField:'',searchString:'', searchOper:'',
filters:'{"groupOp":"OR","rules":['+
'{"field":"Date","op":"ge","data":"2012-04-23"},'+
'{"field":"Date","op":"lt","data":"2012-04-25"}' +
']}'
};
$grid.setGridParam({postData:post_data}).trigger('reloadGrid');
The above will save the postData array with the new config and the reloadGrid sends a request to the server with the new postData array. This will preserve the paging as well; however, to get the old view of your grid (without search terms) you need to implement the reset button separately too and trigger reloadGrid after that for this to take effect.
Not sure if you have solved your problem by now; however, I am putting this solution here for any one from the future who has the same issue.
As far I know there is no way to do this, but to write it yourself.
http://www.trirand.com/jqgridwiki/doku.php?id=wiki:custom_searching
Filter jqGrid Data by Date Range?

JQ Grid sorting integers

Can anyone please tell me how to sort integers/numbers in JQ grid.
the regular sorting gives a distorted order.
Hi i got it all done when i use loadonce=true and sorttype = 'int'. But i cant use loadonce, since i need to reload the grid several times for my purpose.
Can anyone say how to do it without using loadonce??
Thanks,
Devan
If you use datatype:'json' or datatype:'xml', then the server is responsible for the data sorting. The sidx and the sord parameters which will be send as the part of request to the server describes which sorting should use the server.
If you want to implement client side sorting and paging of data you can use loadonce:true jqGrid parameter. In the case you should define sorttype property (having default value 'text') which describes the data type from the column. After the first data loading the datatype of the jqGrid will be automatically changed to 'local' and the sorting, paging and filtering/searching of data will be implemented locally by jqGrid itself.

jqgrid append rows from xml

I'm using a grid which have editurl: "clientArray",datatype: 'xmlstring', datastr: '<%=_xml_string %>'.
I have a button in my page , and when i click it i make an ajax call and get an xml containing new rows that i want to add to my grid.
Currently i'm looping through the xml rows and using addRowData for each row.
My question is how can append these rows to the current grid? I want the rows that are currently in the grid to still be there and append to them the new rows from the xml i get.
Thank's In Advance.
You can for example prepare an array of data with the new rows. You can add all the rows in one call of addRowData (read the documentation for more information about different parameters of addRowData).
The things will be more easy if you would return JSON data from the server instead of XML. It is recommended for the most situations. Then you can just make one call of addRowData with the data returned from the server to append there to the grid. Optionally you can needed to use small customization of the localReader (see here for more information)
depending on the formatting of the returned data, you could use addXMLData, however you may have to manipulate the returned XML first in order to have it format correctly.

Resources