How to dynamically pass in data to search with jqGrid? - jqgrid

I am using jqGrid to render some data. Now I want the ability to modify the data based on the values of two different select boxes. For example, I have a dropdown of location id's and a dropdown of date ranges. I want to filter by the location id and date range, handling this logic in my /something/search action. How can I pass this additional data into jqGrid dynamically? So, (1) on the initial load and (2) when on onChange event is fired, i'll pass in something like {data: {location_id: 10, range_start: '1/1/2012', range_end: '1/5/2010'}}. I could then read this in as a param, just like I do for "page", "rows", "sidx", etc..
Edit:
Included my existing code if needed:
grid.jqGrid({
url: "/something/search",
datatype: "json",
colNames: ['', 'ID', 'Description', 'Start', 'End', 'Last Updated'],
colModel: [{name:'act',index:'act', width:16,sortable:false},
{ name: 'id', index: 'id', width: 100, hidden: true },
{ name: 'description', index: 'description', width: 200 },
{ name: 'start_date', index: 'start_date', width: 120 },
{ name: 'end_date', index: 'end_date', width: 120 },
{ name: 'last_update', index: 'last_update', width: 120 }],
rowNum: 20,
rowList: [10, 20, 50],
pager: '#data-list-pager',
sortname: 'ident',
viewrecords: true,
sortorder: "desc",
multiselect: false,
height: "100%",
caption: "",
altRows: true,
width: 865});

The first way to solve the problem is to follow the way which I described here. In the case the controls which the user will use to filter the grid will be outside of the grid.
Another way which I can suggest is to use the Toolbar Searching. The advantage of the approach is that the controls used for searching will be integrated in the grid. In the case you can use either the select control or text input with the jQuery UI Datepicker.
To add the Toolbar Searching you need just call filterToolbar with the parameters which you prefer. If you would you stringResult: true option the format of the filters parameter which will be send to the server will be the same like in case of the usage of advanced searching. To define 'greater or equal' searching operation for the 'start_date' and the 'less or equal' searching operation for the 'end_date' you need just add searchoptions having 'ge' or 'le' as the first element of the sopt property.
As the result one will be able to filter the grid:
and
see the demo.

Related

jqgrid default pagination not working with ajax and xml data

I have created a jqgrid as follows:
$("#table_jqgrid").jqGrid({
autowidth: true,
shrinkToFit: true,
datatype: function(postdata) {
jQuery.ajax({
url: 'getxmlInfo',
data:postdata,
dataType:"xml",
complete: function(xmldata,stat){
debugger;
console.log(xmldata);
if(stat=="success") {
var thegrid = jQuery("#table_jqgrid")[0];
console.log(thegrid)
debugger;
thegrid.addXmlData(xmldata.responseXML);
}
}
});
},
colNames: ['col1', 'col2', 'col3', 'col4', 'col5', 'col6'],
colModel :[
{name: 'col1', index: 'col1', width: 60, sorttype: "int"},
{name: 'col2', index: 'col2', width: 90, sorttype: "date"},
{name: 'col3', index: 'col3', width: 100},
{name: 'col4', index: 'col14', width: 80, align: "right", sorttype: "float"},
{name: 'col5', index: 'col5', width: 80, align: "right", sorttype: "float"},
{name: 'col6', index: 'col6', width: 80, align: "right", sorttype: "float"}
],
pager: '#pager_jqgrid',
loadonce:true,
rowNum:10,
rowList:[10,20,30],
sortname: 'col1',
sortorder: 'desc',
rowTotal:40,
viewrecords: true,
});
Everything is working fine when changing records per page.But pagination is not correct.
getxmlInfo is a url to servlet function which returns corresponding xml as ajax response.
Initially page number is 1 and records/page is 10 then 10 rows will be shown.Is there any way to set total number of pages in jqgrid?
After a long search one link revealed there exists an option that is setting rowTotal parameter.But it is not working .How we can persist pagination data on each ajax call.Is there exists any solution to set totalrows on each ajax call.
I would strictly recommend you don't use datatype as function if you need make just an Ajax call to the server. More as 90% of the code which implements such feature uses it in a wrong way. Your code for example can send Ajax request only once in Internet Explorer because well known problem with caching of Ajax requests. You use additionally complete callback instead of "success", which is also not full correctly. The "notmodified" is an example of successful response too. You should never use jQuery.ajax if you are not a real specialist in the subject.
You should understated jqGrid also really good to be able to use datatype as function. You can find an example of the implementation in the answer. You can see that the code is not so short. The answer is written many years ago and it's not supports many features added in jqGrid later. The function datatype in jqGrid 4.5.3 and later for example contains 5 parameters. The function addXmlData which you try to call contains 5 parameters too. Your current implementation of datatype break some standard callbacks, for example loadComplete, loadError, beforeProcessing. Some features of jqGrid like virtual scrolling or frozen columns will be broken too.
I recommend you to replace datatype to
url: "getxmlInfo",
datatype: "xml"
I recommend you additionally remove all index properties from colModel. You use loadonce: true option which require to the index and name value are the same. You use for example name: 'col4', index: 'col14' which break the rule.
I strictly recommend you to add gridview: true option which will just improve performance of the loading of the grid. You should consider to use autoencode: true option if the XML data returned from the server don't contains HTML fragments which need be set in the cells of jqGrid.

JQGrid not loading data from array variable

I have a problem that is confounding me for a few days. Initially, I tested a hard-coded approach within my javascript function which loads local variable containing a simple array as follows:
var myGridData = [
{ ID: "55505", Item: "Mortgage foreclosure", Class: "36", Status: "Pending" },
{ ID: "55506", Item: "Food truck parks", Class: "43", Status: "Pending" }];
for (var i = 0; i <= myGridData.length; i++)
jQuery("#popGrid").jqGrid('addRowData', i + 1, myGridData[i]);
And the above hard-coded approach works. But when I assign the data dynamically to the my GridData variable (based on user selections from another grid, my grid populates with empty rows, one row for each character in the variable (i.e. 154 rows using the above - instead of the two expected). I've double checked the two strings and they are identical (including the correct qualifiers, brackets, etc. so I am at a loss. Any ideas?
Thanks in advance,
Neale
Thanks Oleg, I can look into filling grid during its creation…once I understand everything a bit better. I’m kind of stabbing in the dark. Below, I’ve included code I use to obtain the array data string from the “main” page – which is then passed to the dialog via a hidden field. I’ve followed that up with my JQGrid parameters and colModel:
var temp = document.getElementById('<%=txtArray.ClientID%>').value;
var myGridData = temp;
jQuery("#popGrid").jqGrid({
multiselect: true,
data: myGridData,
datatype: 'local',
colNames: [
'ID',
'Item',
'Class',
'Status',
],
colModel: [
{ name: 'ID', index:'ID', key: true, width:50 },
{ name: 'Item', index: 'Item', key: true, width:100 },
{ name: 'Class', index: 'Class', key: true, width: 150 },
{ name: 'Status', index: 'Status', key: true, width: 250 },
],
multiSort: true,
sortable: true,
loadonce : true,
rownum: 5,
width: 850,
height: 100,
scrollOffset: 1,
shrinkToFit: true,
sortname: 'ID',
viewrecords: true,
gridview: true,
sortorder: "desc",
pager: '#popPager'
});

what are colnames and colmodel in jqgrid

Can you explain what are these colnames and colmodel in jqgrid ?
Where are getting used ? defining columns , then ...how these 2 colnames / colmodels are inter related and all ??
and can please point why sorting is not happening in my below code:
<script type="text/javascript">
$('#grid').jqGrid({
url: '#(Url.Action("LoadIssues","Home"))',
datatype: 'json',
colNames: ['Category', 'Description', 'Issue_Title', 'LOGGED_BY', 'Notes', 'Priority', 'Status'],
mtype: 'GET',
colModel: [
{ name: 'Category', width: 100 },
{ name: 'Description', width: 100 },
{ name: 'Issue_Title', width: 100 },
{ name: 'LOGGED_BY', width: 100 },
{ name: 'Notes', width: 100 },
{ name: 'Priority', width: 100 },
{ name: 'Status', width: 100 }
],
jsonReader: {
id: 'id',
repeatitems: false
},
rowNum: 10,
rowList: [5, 10, 20, 30],
gridview: true,
pager: '#gridpager',
sortname: 'Description',
sortorder: "desc",
viewrecords: true,
shrinkToFit: true,
width: $('#gridContainer').width(),
height: 200,
hidegrid: false,
gridComplete: function () {
$('#gridContainer').find('.ui-jqgrid-titlebar').hide();
}
})
In a nutshell, colNames defines the names of your jqGrid columns on the page, and colModel specifies options for each column (name in the dataset, width, etc).
The documentation has more information:
colModel Array which describes the parameters of the columns.This is the most important part of the grid. For a full description of all valid values see colModel API.
colNames
An array in which we place the names of the columns. This is the text that appears in the head of the grid (header layer). The names are separated with commas. Note that the number of elements in this array should be equal of the number elements in the colModel array.

jqgrid saving records for inline editing

in jqgrid for inline editing when we click the save icon, ** internally it calls the saveRow method, but i want to call my custom method where i will implement my save logic as well calling to controller method.**
i used below code for grid.
var grid = jQuery("#list5").jqGrid({
url: '/home1/GetUserData',
datatype: "json",
mtype: "POST",
colNames: ['Code', 'LoginID', 'Emailid', 'CreateDate', 'PostalCode', 'Mobile'],
colModel: [
{name: 'Code', index: 'Code', width: '16%', editable: true, sortable: true },
{ name: 'LoginID', index: 'LoginID', width: '16%', editable: true, sortable: true },
{ name: 'Emailid', index: 'Emailid', width: '16%', editable: true,
sortable: true },
],
rowNum: 10,
height: '100%',
scrollOffset: 0,
rowList: 10,
shrinkToFit: true,
pager: $("#pager2"),
editurl: "/home1/EditUserData",
caption: "Simple data manipulation"
});
jQuery("#list5").jqGrid('navGrid', '#pager2', { edit: false, add: false, del: true, search: false, refresh: false }, {}, {}, { url: '/home1/DeleteUserData' });
jQuery('#list5').jqGrid('inlineNav', '#pager2', { edit: true, add: true},
});
});
so please anyone let me know how to implement it.
I don't really understand your requirements. saveRow has a lot of customization possibilities. You can rename all parameters which will be sent to the server using prmNames option of jqGrid. Using extraparam parameter of saveRow you can specify additional information which can be sent to the server. The callback serializeRowData can be used to implement your custom serialization. For example you can convert the data to JSON. Using aftersavefunc you can make some custom actions after the data will be successfully saved on the server. So I recommend you use the features instead of implementing your custom saveRow method.
UPDATED: If you want to have navigator icon which uses your custom saveRow you should don't add "Save" button by inlineNav. You can use save: false option of inlineNav. Then you can just use navButtonAdd and add your custom icon which look exactly like original "Save" button and make call of your "custom saveRow" in the onClickButton callback.

jqgrid form editing problem

I'm using jqGrid with mvc 2 like this:
jQuery("#extension_grid").jqGrid({
url: '/Extension/Report',
datatype: "json",
direction: "rtl",
height: "auto",
jsonReader: { root: "rows", page: "page", total: "total", records: "records", repeatitems: false, userdata: "UserData" },
colModel:
[
{ name: 'id', label: 'داخلی', key: true, search: true, width: 55 },
{ name: 'assigned_user', label: 'کاربر', width: 90, editable: true },
{ name: 'creation_date', label: 'تاریخ ایجاد', width: 100, formatter: 'date', formatoptions: { newformat: 'Y-m-d H:i:s'} }
],
rowNum: -1,
pager: '#extension_pager',
sortname: 'id',
viewrecords: true,
sortorder: "asc",
caption: "داخلی‌ها",
editurl: '/Extension/MyEdit'
});
jQuery("#extension_grid").jqGrid('navGrid', '#extension_pager', { edit: true, add: true, del: true }, {}, {}, {}, { multipleSearch: true });
when I select a row and click the edit button a dialog appears and I can edit the row. after submit, data is posted to the editurl successfully. but changes are not saved to grid client side. should I save the changes client side manually?
I tried with datatype local and it works!!! what should I do? is there any problem with using json data and form editing?
The situation which you described seems me very strange. There are default setting reloadAfterSubmit:true for "Add" and "Edit" forms. It means that after submitting of the "Edit" form for example the grid contain will be reloaded. You can verify with respect of Fiddler or Firebug that the grid reloading take place. So either your server part '/Extension/MyEdit' not save the data or another the server '/Extension/Report' don't get the refreshed data. Do you have some kind of data caching on the server?
So you should analyse the problem which you have more exctly. If you would not solve the problem yourself you should update/append your question with more additional information.

Resources