Reputation: 91
I am struggling to select, and manipulate, the dropdown from the jqGrid.
jQuery("#grid")...
colModel: [...
{ name: 'StateId', index: 'StateId', width: 350, align: 'center', stype: 'select',
edittype: 'select', searchoptions: { sopt: ['eq'] },
editoptions: { value: controllerMethods.GetStates()} },...
.
.
.
jQuery("#grid").jqGrid('filterToolbar', { stringResult:true, searchOnEnter:false });
I simply need to be able to set a default selected value of the dropdown control and I am unable to achieve that :(
Any help is appreciated!
Upvotes: 2
Views: 14166
Reputation: 91
I believe I found the way:
var stateIdDropDown = $('#gs_StateId');
In this case the gs_StateId is the DOM Id of the element (found out by the help of Firebug).
However, please reply If someone knows of a way on how to select the element in the following fashion:
var stateIdDropDown = $('#myGrid>whatever...StateId');
Thanks everyone.
Appendix 1:
.
.
.
//Preset default search filter.
SetGridSearchDefaults: function (grid)
{
var gridInfo = new Object();
var postData = grid.jqGrid('getGridParam', 'postData');
if (postData.filters==null)
{
postData.filters = '{"groupOp":"AND","rules":[{"field":"StateId","op":"eq","data":"1"}]}'
grid.jqGrid('setGridParam', { postData: postData });
}
},
//Pre-Select dropdowns.
PreSelectDropDowns: function ()
{
$('select#gs_StateId').val('1');//Status New
},
. . .
The SetGridSearchDefaults is called on Grid's beforeRequest event!
The PreSelelectDropDown is called on Grid's gridComplete event, please not the snippet!
jQuery("#grid").jqGrid({
.
.
.
gridComplete: function () {
if (firstLoad == true) {
commonMethods.PreSelectDropDowns(); //Pre-Select filter dropdowns.
firstLoad = false;
}
.
.
.
In this way I have managed to preserve the MVC pattern by forcing the GUI to drive the controller, i.e. I set the default search criteria at the GUI level.
Upvotes: 0
Reputation: 221997
You can use an additional searchoptions option
searchoptions:{
dataInit:function(el){
$("option:contains("+defaultCategory+")",el).attr("selected", "selected");
setTimeout(function(){
$(el).trigger('change');
},500);
}
}
where defaultCategory
is the option which you want have default. See small demo here.
Upvotes: 3