2

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!

Oleg
  • 217,934
  • 30
  • 386
  • 757
bignermo
  • 91
  • 1
  • 2
  • 11

2 Answers2

3

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.

Oleg
  • 217,934
  • 30
  • 386
  • 757
  • Thanks a lot Oleg! Your solution works! However, I would also be interested into how to acquire a reference to the "el", i.e. the dropdown but from outside the dataInit event. One thing that crosses my mind is to trap the reference within the dataInit event and assign it to external variable for later use. Still, that relies on dataInit event :( – bignermo Nov 10 '10 at 07:43
  • Of cause saving the reference to the value `el` in an external variable will work. It seems to me that you can do the same just with `$("#gs_StateId")` because the id of the elements from the filter toolbar will be constructed from the `gs_` prefix and the name of the corresponding column. – Oleg Nov 11 '10 at 10:28
0

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.

bignermo
  • 91
  • 1
  • 2
  • 11
  • Please don't add new information in the new answer. It is difficult to find it. Why you don't like `$('#gs_StateId')`? You can not use syntax like `$('#myGrid> ...)` because toolbar is **outside of** `#myGrid`. It iy inside of `#gview_myGrid`. Old answers about searching/filtering http://stackoverflow.com/questions/3974324/jqgrid-using-multiple-methods-to-filter-data, http://stackoverflow.com/questions/3981874/multiple-search-with-multiplefields-by-default and http://stackoverflow.com/questions/3989786/jqgrid-clear-search-criteria shows how `postData` used in searching and could be intrestring – Oleg Nov 11 '10 at 10:50
  • Best wisches to Prague! I have the best memories about the town. I visited it many years ago as I was a student. – Oleg Nov 11 '10 at 10:52