jqGrid : easy way to have the refresh button - jqgrid

I just want to display the refresh button on my jqGrids. I thought it was provided naturally so I did this :
$.extend($.jgrid.nav, {
refresh: true,
refreshstate: "current"
});
but it does nothing. no button appearing.
How can I add a refresh button just triggering the reloadGrid ?
thank you

$.jgrid.nav cab be used to specify default values of navGrid parameters, but to create "refresh" button or some other button in the grid you need explicitly call navGrid method. For example
$("#gridId").jqGrid("navGrid", "#pager", {
add: false, edit: false, del: false, search: false, refresh: true
});
The method navGrid creates navigator structures in the specified pager and then places specified buttons in the navigator bar.
If you use toppager: true option then top pager will be created additionally. The pager will have id = grid_id + "_toppager" (gridId_toppager if id of the grid is gridId). You can have one or two pagers. You can call navGrid explicitly for one from the pagers or you can use cloneToTop: true option of navGrid to place the pager on every page.
If you have existing project with many grids you have sure one common JavaScript file with settings which you included on every page. You placed sure $.extend($.jgrid.nav, {...}); in the file. Whyt you can do is to specify common onInitGrid callback which calls navGrid:
$.extend($.jgrid.defaults, {
onInitGrid: function () {
var $self = $(this), p = $self.jqGrid("getGridParam");
if (p.pager) {
$self.jqGrid("navGrid", p.pager,
{add: false, edit: false, del: false, search: false, refresh: true, cloneToTop: true});
} else if (p.toppager) {
$self.jqGrid("navGrid", "#" + $.jgrid.jqID(p.id) + "_toppager",
{add: false, edit: false, del: false, search: false, refresh: true});
}
}
});

Related

jqGrid Trying to apply filter(s) on Grid creation

I have an object that contains the following filter string:
prefs.filters={"groupOp":"AND","rules":[{"field":"FirstName","op":"cn","data":"max"}]}
Here is my grid create, where I am trying to apply the filters in the postData setting:
// Set up the jquery grid
$("#jqGridTable").jqGrid(
{
// Ajax related configurations
url: jqDataUrl,
datatype: "json",
mtype: "GET",
autowidth: true,
// Configure the columns
colModel: columnModels,
// Grid total width and height
height: "100%",
// customize jqgrid post init
gridComplete: function()
{
CRef.updateJqGridPagerIcons("jqGridTable");
},
// Paging
toppager: true,
rowNum: 20,
rowList: [5, 10, 20, 50, 100],
viewrecords: true, // Specify if "total number of records" is displayed
// Default sorting
sortname: typeof prefs.sortCol !== "undefined" ? prefs.sortCol : "LastName",
sortorder: typeof prefs.sortCol !== "undefined" ? prefs.sortOrd : "asc",
sorttype: "text",
sortable: true,
postData: typeof prefs.filters !== "undefined" ? { filters: prefs.filters } : {},
//also tried this...
//postData: typeof prefs.filters !== "undefined" ? { JSON.stringify(filters: prefs.filters) } : {},
//remaining property settings excluded from post to keep short.
Update: Using .navGrid on grid as follows:
.navGrid("#jqGridTable",
{ refresh: true, add: false, edit: false, del: false, refreshtitle: getRefreshText('#Model.Instruction'), searchtitle: searchText },
{}, // settings for edit
{}, // settings for add
{}, // settings for delete
{ closeAfterSearch: true, closeOnEscape: true, multipleSearch: true, multipleGroup: true });
When grid is created, the filter in postData is not applied. I also tried triggering reload event after the initial create and that to did not apply filter either.
From other posts, I believe I'm on correct path, but appear to be missing something.
Update after comments:
I added the following code to loadComplete...
if ($("#jqGridTable").jqGrid("getGridParam", "datatype") === "json") {
setTimeout(function () {
$("#jqGridTable").jqGrid("setGridParam", {
datatype: "local",
postData: { filters: prefs.filters, sord: prefs.sortOrd, sidx: prefs.sortCol },
search: true
});
$("#jqGridTable").trigger("reloadGrid");
}, 50);
}
and it successfully retains the filters. :)
However, now I have interesting issue side effect. I can sort on columns and some columns will change to sort asc/desc correctly, but when I go to others and sort, they sort but in the order that they originally loaded which is neither asc or desc.
You have to set search: true option of jqGrid if you want that filters will be applied. Moreover you use datatype: "json". It means that the filters will be just send to the server and your server code have to decode the filters and to use there. One more remark. The correct value for postData would be { filters: JSON.stringify(prefs.filters) }.
UPDATED: I recommend you upgrade to the latest version (4.12.1) or free jqGrid. It's the fork of jGrid which I develop since the end of 2014. To use free jqGrid you can just change the URLs to jqGrid files to URLs described in the wiki article. After that you can use the following options:
loadonce: true,
forceClientSorting: true,
search: true,
postData: { filters: prefs.filters },
sortname: prefs.sortCol,
sortorder: prefs.sortOrd
and remove the loadComplete code which you posted in "Update after comments" part of your question. Free jqGrid will load all data returned from the server, apply the filter prefs.filters locally, sort the results locally and display the first page of the results (based on the page size rowNum: 20).
The old demo loads the JSON data from the server, sort the data locally and filter by isFinal === 1 and finally display the first page of results. The demo uses additionally custom sorting using sortfunc, additionalProperties, which allows to saved additionally properties in local data.
You can additionally include reloadGridOptions: { fromServer: true } to other options of navGrid which you use (refresh: true, add: false, ...). It will change the behavior of the Refresh button of the navigator bar to reload the data from the server if the user clicks on the button. See another answer, which I posted today for more information about new options of free jqGrid and loadonce: true.

How can I wire up a JqGrid search dialog?

.navGrid('#selectedInmatePager', { edit: false, add: false, del: false });
Gives me the search button on my JqGrid. How can I get the data from the dialog box and send it through a webmethod?

JqGrid: how to refresh the grid in afterSubmit?

I have two buttons in the pager, one for adding a new record. See the code below:
$(grid_selector).jqGrid('navGrid', pager_selector,
{
edit: false,
add: true,
addicon : 'icon-plus-sign purple',
del: true,
delicon : 'icon-trash red',
search: false,
refresh: false,
view: false,
},
null, //no need for edit
{
//new record form
url: "/add",
...
afterSubmit : function(response, postdata) {
// ??? how to refresh the grid
}
},
...
}
After adding a record, I am hoping to refresh the grid. Could someone let me know how to do this?
I am using local data for the test purpose.
Regards.
Make sure that you are using updated version of jqGrid in your application
If all other parameters are are correct ( refresh:true etc ) you can use this to reload Grid
$(grid_selector).trigger("reloadGrid");
Please note this condition too
The reloading is not performed due to the datatype got change to local(loadonce is true).You need to reload the grid manually in afterSubmit function for form editing. You need to set the datatype to json before triggering reload event.
$(grid_selector).jqGrid('setGridParam',{datatype:'json'}).trigger('reloadGrid');
Default property used in form editing is reloadAfterSubmit: true. It means that jqGrid trigger reloadGrid processing of afterSubmit.
You wrote "I am using local data". It's difficult to answer on your question because you don't posted more full code which you use. I try to guess what you do. Because jqGrid don't support local for editing till now then I suppose that your problem exist because you use loadonce: true option. If the option are used then jqGrid changes the original datatype to "local" after the first loading of data. In other words jqGrid "breaks" connection to the server (to url) after the first loading of data.
So you need just to reset the original datatype ("json" or "xml") before jqGrid trigger reloadGrid. The corresponding code will be about the following:
$(grid_selector).jqGrid("navGrid", pager_selector,
{
edit: false,
addicon : 'icon-plus-sign purple',
delicon : 'icon-trash red',
search: false,
beforeRefresh: function () {
$(this).jqGrid("setGridParam", { datatype: "json" });
}
},
{}, //no need for edit
{
//new record form
url: "/add",
...
afterSubmit: function () {
$(this).jqGrid("setGridParam", { datatype: "json" });
}
},
...
}
The above code add "Refresh" button and uses the same beforeRefresh and afterSubmit. In the way "Refresh" button reloads the data from the server. I personally find existence of such button practical in loadonce: true scenario.

rowpos and colpos in beforShowForm

Is there an option to include rowpos and colpos in beforShowForm. I understand that it should be used under formoption setting of colModel. but my grid has customised edit (say status) and normal edit. I want different alignment for these two. below are my code for reference
bulkgrid.jqGrid('navGrid','#bulkktrackpager',{
edit: true,
add: true,
del: true,
search: true,
view: true,
//cloneToTop: true,
}).navButtonAdd('#bulkktrackpager',{
caption:"Status",
buttonicon:"ui-icon-lightbulb",
position:"last",
});
any idea???? many thanks..
}).navButtonAdd('#bulkktrackpager',{
caption:"Status",
buttonicon:"ui-icon-lightbulb",
position:"last",
onClickButton: function(){
var $self = $(this);
$self.jqGrid("editGridRow", $self.jqGrid("getGridParam", "selrow"),
{
beforeInitData: function(formid) {
bulkgrid.setColProp('status', {
formoptions : {
rowpos : 1,
colpos: 1,
},
});
bulkgrid.setColProp('ctno', {
formoptions : {
rowpos : 1,
colpos: 2,
},
});
//similaryly other elements
},
beforeShowForm: function(form) {
$("#tr_agent").hide();
},
recreateForm: true,
editData: {//Function to Add parameters to the status
oper: 'status',
},
closeAfterEdit: true,
reloadAfterSubmit: true,
});
}
});
Images
Image2
You can use rowpos and colpos properties of formoptions. You can set the values dynamically inside of beforeInitData callback. You should use recreateForm: true option additionally to be sure that jqGrid uses the current values.
The demo created for the answer demonstrate "static" usage of rowpos and colpos properties of formoptions. If you need to change alignment of all labels of the you can set text-align style (see the answer). Alternatively you can set CSS style text-align for specific labels only. You need to set the style inside of beforeShowForm callback for example.

How to force delete action button to call delete controller in jqgrid

jqGrid delete action button is used to delete row.
This button click calls Edit method, it looks like it uses editurl parameter.
How to force it to call Delete method like Delete toolbar button calls ?
$(function () {
var grid = $("#grid");
grid.jqGrid({
url: '/GetData',
colModel: [{
"formatter":"actions",
"formatoptions":{"keys":true,"delbutton":true,
...
}}],
editurl: '/Edit',
});
grid.navGrid("#grid_toppager", null,null,null, { url: '/Delete' } );
});
You should include delOptions option in the formatoptions:
formatter: "actions",
formatoptions: {
keys: true,
delbutton: true,
delOptions: {
url: "/Delete"
}
}
Inside of delOptions you can use any from the properties and any events of the delGridRow method.

Resources