JQGrid editoptions dataurl不使用ajax get?

时间:2012-04-19 15:08:36

标签: jquery ajax jqgrid

我有一个使用editoptions:{dataUrl:'getMetadata?type=' + myType+ '&column=colOne'}的数据网格。我还为ajaxComplete事件绑定了一个函数,以便在发出请求后进行自定义标头检查。除了dataUrl请求之外,这对我在JQGrids请求中看到的所有内容都运行良好。执行get请求后不会触发Ajax完成。 ajaxStartajaxStop确实被解雇了。完成/错误/成功不是。

根据dataUrl的jqgrid文档,它应该使用ajax调用来获取数据。

'数据是通过AJAX调用获得的,应该是带有所需选项的有效HTML select元素......'

我在这里还有其他的东西吗?绑定代码如下。

$('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";
});

编辑:忘记添加请求以200状态返回。

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

以下代码放在任何位置(当前位于document.ready中)会导致所有选择框无法在搜索中显示。

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

ColModel和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");}}}                             
                      ];

网格定义,使用一些基于三元的,所以我们可以重用jqgrid调用类似的网格

    $("#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');

最后,从数据URL返回HTML

<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>

对于文字墙感到抱歉,但我尝试添加一些看似有用的内容。请注意,这在我们拥有的众多(10+)jqgrids中完全正常(在搜索填充中选择框),直到启用ajaxSelectOptions默认值。

1 个答案:

答案 0 :(得分:1)

我建议你使用jqGrid的ajaxSelectOptions选项来自定义使用jqGrid从dataUrl获取数据的Ajax请求。我希望参数ajaxSelectOptions可以在你的情况下看起来像

ajaxSelectOptions: {
    complete: function (jqXHR) {
        if (jqXHR.getResponseHeader('REQUIRES_AUTH') === '1') {
            location.reload();
        }
    }
}