0

I have a data grid using editoptions:{dataUrl:'getMetadata?type=' + myType+ '&column=colOne'}. I also have bound a function to the ajaxComplete event in order to do a custom header check after requests are made. This is working fine on everything I've seen with JQGrids request except the dataUrl request. Ajax complete does not fire after the get request is performed. ajaxStart and ajaxStop are indeed getting fired appropriately. Complete/Error/Success are not.

According to the jqgrid documentation for dataUrl it should be using an ajax call in order to grab the data.

'The data is obtained via an AJAX call and should be a valid HTML select element with the desired options ...'

Is there something else I'm missing here? Binding code below.

$('body').bind('ajaxComplete',function(event,request,settings){
    if (request.getResponseHeader('REQUIRES_AUTH') === '1'){
       location.reload();
    };
});
//Other binds added to see what is getting fired
$('body').bind('ajaxError',function(event,request,settings){
    if (request.getResponseHeader('REQUIRES_AUTH') === '1'){
       location.reload();
    };
});
$('body').bind('ajaxSuccess',function(event,request,settings){
    if (request.getResponseHeader('REQUIRES_AUTH') === '1'){
       location.reload();
    };
});
$('body').bind('ajaxStart',function(){
    var b = "ABC";
    var c = "DEF";
});
$('body').bind('ajaxStop',function(event,request,settings){
    var b = "ABC";
    var c = "DEF";
});

Edit: Forgot to add that the request is coming back with a 200 status.

-----------------------------Ajax Select Options issue here------------------------

The following code when put anywhere (currently in document.ready) is causing all select boxes to not display in searches.

$.extend($.jgrid.defaults, {
            ajaxSelectOptions: {    
                complete: function (jqXHR) {
                    if (jqXHR.getResponseHeader('REQUIRES_AUTH') === '1') 
                    {
                        location.reload();
                    } 
                    return;
                }
        }});

ColModel and Names

    gridForm.colNames = ['ID','Field1','Field2','Field3','Field4','Field5','Last User Id','Modified Date' ];
gridForm.colModel = [
                      {name:'id', editable: false, edittype:'text',search:true, stype:'text'},        
                      {name:'Field1', editable: checkedOutByUser, edittype:'text', search:true,editrules:{required:true}, stype:'text'},
                      {name:'Field2', editable: checkedOutByUser, edittype:'select', editoptions:{dataUrl:dataUrl + "&search=false"}, search:true,editrules:{required:true}, searchoptions:{dataUrl:dataUrl + "&search=true"}, stype:'select'},
                      {name:'Field3',  editable: checkedOutByUser, edittype:'select', editoptions:{dataUrl:dataUrl + "&search=false"}, editrules:{required:true},search:true, searchoptions:{dataUrl:dataUrl + "&search=true"}, stype:'select'},
                      {name:'Field4',  editable: checkedOutByUser, edittype:'select', editoptions:{dataUrl:dataUrl + "&search=false"}, editrules:{required:true}, search:true, searchoptions:{dataUrl:dataUrl + "&search=true"}, stype:'select'},
                      {name:'Field5', editable: false, edittype:'text', search:true, stype:'text'},
                      {name:'userId', editable: false, edittype:'text', search:true, stype:'text'},
                      {name:'modifiedDate', editable: false, search:true, stype:'text', searchoptions:{dataInit:function(el){defaultCalendar.create(el, "componentGrid");}}}                             
                      ];

Grid definition, using some ternary based so we can re-use the jqgrid call for similar grids

    $("#myGrid").jqGrid(
        {
            caption:gridForm.caption,
            overflow:'hidden',
            url:gridForm.url?gridForm.url:url,
            height: gridForm.height?gridForm.height:'auto',
            datatype: "json",
            colNames:gridForm.colNames,
            colModel:gridForm.colModel,
            recordtext: 'Record(s) {0} - {1} of {2}', 
            rowNum: 20, 
            sortname: gridForm.sortindex?gridForm.sortindex:"id",
            sortorder: gridForm.sortorder?gridForm.sortorder:"desc",
            cellEdit: false,
            cellurl : 'updateRow', 
            editurl:'addRow',
            cellsubmit : 'remote',
            toolbarfilter: true,
            onCellSelect:gridForm.onCellSelect?gridForm.onCellSelect:null,
            pager: jQuery('#pager'),
            viewrecords: gridForm.viewrecords?gridForm.viewrecords:true,
            gridview: gridForm.gridview?gridForm.gridview:true,
            shrinkToFit: true,
            multiselect: gridForm.multiselect?gridForm.multiselect:true});
jQuery("#myGrid").jqGrid('filterToolbar');

and finally, the HTML being returned from the data URL

<select><option value=''</option><option value='Dummy Entry'>Dummy Entry</option><option value='Next Entry'>Next Entry</option><option value='ThirdEntry'>ThirdEntry</option><option value='FourthEntry'>FourthEntry</option></select>

Sorry about the wall of text, but I tried to add everything that might seems useful. Note that this has worked perfectly fine (select boxes in search populating) across numerous (10+) jqgrids that we have until enabling the ajaxSelectOptions default.

Joseph
  • 1,442
  • 21
  • 44

1 Answers1

1

I would recommend you to use ajaxSelectOptions option of jqGrid to customize the Ajax request which uses jqGrid to get data from dataUrl. I hope that the parameter ajaxSelectOptions could look in your case like

ajaxSelectOptions: {
    complete: function (jqXHR) {
        if (jqXHR.getResponseHeader('REQUIRES_AUTH') === '1') {
            location.reload();
        }
    }
}
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • 'This option allows to set global ajax settings for the select element when the select is obtained via dataUrl option in editoptions or searchoptions objects' So is that specific to the grid the option is applied to? I would assume so but the word 'global' throws me off a bit. – Joseph Apr 19 '12 at 15:44
  • 1
    @Joseph: If you need make some options default you should just extend `$.jgrid.defaults` (see [here](http://stackoverflow.com/a/2678731/315935)). In your case you can do `$.extend($.jgrid.defaults, {ajaxSelectOptions: {complete: function (jqXHR) {if (jqXHR.getResponseHeader('REQUIRES_AUTH') === '1') {location.reload();}}}});` – Oleg Apr 19 '12 at 16:06
  • You sir, are very very helpful. That cuts the amount of code I have to change down considerably. – Joseph Apr 19 '12 at 16:15
  • @Joseph: You are welcome! In general it would be good practice to set your project specific settings of jqGrid by `$.jgrid.defaults`, `$.jgrid.search`, `$.jgrid.edit`, `$.jgrid.inlineEdit` and so on. New event system in jqGrid 4.3.2 allows you additional define jQuery events instead of callbacks which are named as "events" in the jqGrid documentation. So you can make the code of individual grid very small. It will improve maintenance of the large project. – Oleg Apr 19 '12 at 16:24
  • Actually, after looking further into this, adding the defaults causes none of the select dropdowns to be displayed. The `complete` function is executed every time, but the resulting dropdowns are never created, regardless of what is in the function. Even an empty `complete` function is preventing the dropdowns from displaying. – Joseph Apr 24 '12 at 21:26
  • @Joseph: Sorry, but you should append your question with the current code which has the described problem. Probably `location.reload();` has some problems? You should also include an example of response from `dataUrl` and the full definition of the column which use `dataUrl`. Probably posting of more jqGrid option could be also helpful. – Oleg Apr 24 '12 at 21:34
  • I posted everything I could think of at this point. If you don't see anything that should be wrong, I'll go poke around in the JQGrid src tomorrow. I will also try using ajaxSelectOptions on the grids themselves instead of a default to see if that is an issue. – Joseph Apr 24 '12 at 21:47
  • @Joseph: I don't see clear bugs in your code excepting the error in ``). I recommend you additionally use `template` option of `colModel` (see [here](http://stackoverflow.com/a/6047856/315935)). It will simplify your grids. I define typically some templates in the same place of common code where I modify `$.jgrid.defaults`. You should also remove default properties like `editable: false, search:true, stype:'text'`, remove non-existing options like `overflow:'hidden'` and replace `pager: jQuery('#pager')` to `pager: '#pager'`. – Oleg Apr 24 '12 at 22:09
  • @Joseph: I think you should include `jquery.jqGrid.src.js` instead of `jquery.jqGrid.min.js` and debug the place over `complete` function. You don't posted `location.reload();`. You should verify that it produces no exceptions. – Oleg Apr 24 '12 at 22:12
  • 1
    @Joseph: I have an idea: probably you use some *old* version of jqGrid? The current version of jqGrid (see [here](https://github.com/tonytomov/jqGrid/blob/v4.3.2/js/grid.common.js#L345-385)) uses `success` and `complete` is free. Some old version used `complete` instead and `success` was free. So verify that you use the last version of jqGrid. Just download the last version [here](http://www.trirand.com/blog/?page_id=6). – Oleg Apr 25 '12 at 05:54
  • The `location.reload` call only occurs if a user times out, so generally its not being called. I commented it out for now because it doesn't seem to be the root of the problem. I'm going to debug into the .src file today and if that doesn't work I'll try grabbing the latest jqgrid to see if that fixes it. – Joseph Apr 25 '12 at 13:28
  • @Joseph: I'm not sure what you mean under "wait until there is more bandwidth". In any way I would recommend you to upgrade jqGrid to the last current version. – Oleg Apr 25 '12 at 14:20
  • Upgrading JQGrid solved the problem. (Update:Deleted unnecessary chatter) – Joseph Apr 25 '12 at 15:05