jqGrid 4.8.2 完成 columnChooser 后网格宽度发生变化

jqGrid 4.8.2 Grid width changes after columnChooser is done

我正在使用 jqGrid 4.8.2 并设置了固定宽度的网格。但是,当我使用 columnChooser 修改显示的列时,网格的宽度被重新调整为所有列的组合宽度,并且水平滚动消失了。我尝试重置 'done' columnChooser 参数中的宽度,但它不起作用。经过一些调试后,似乎调整大小发生在 'done' 完成后并且对话框 window 关闭后。我创建了一个手动重置宽度的按钮,效果很好,所以我知道可以重置宽度。我只是不知道使用哪个事件来触发它。

var defaultColModel = 
[   
    {name:'REQUESTEDDATE'
        ,index:'r.requestedDate'
        ,label:'Request Date'
        ,sorttype:"date"
        ,template: "date"
        ,search:true
        ,width:100
        ,hidden:false
    },
    {name:'VENDORNAME'
        ,index:'v.companyName'
        ,label:'Vendor'
        ,search:true
        ,stype:'text'
        ,width:150
        ,formatter:'showlink'
        ,formatoptions:{baseLinkUrl:'<cfoutput>#application.rootpath#</cfoutput>', addParam: '&page=dsp_request#vendorT', idName:'REQUESTID'}
        ,hidden:false
    },
    {name:'VENDORID'
        ,index:'r.vendorID'
        ,label:'Vendor ID'
        ,search:true
        ,stype:'text'
        ,width:100
        ,hidden:true
    },
    {name:'VENDORCONTACT'
        ,index:'vendorContact'
        ,label:'Vendor Contact'
        ,stype:'text'
        ,search:true
        ,width:100
        ,hidden:true
    } // there are many more cols.  Just showing a few to illustrate how they are defined.
]
var myGrid = jQuery("#contract_grid").jqGrid({
    url:            'cfc/com_ajxRequestNew.cfc?method=getReqJSON&returnformat=json',
    datatype:       'json',
    postData:       {filters: myFilters},
    mtype:          'POST',
    search:         true,
    colModel:       defaultColModel,
    gridview:       false, //added for IE 
    altRows:        true,
    emptyrecords:   'NO DATA FOUND',
    height:         400,
    width:          1200,
    sortname:       lastSortName,
    sortorder:      lastSortOrder,
    page:           lastPage,
    pager:          jQuery('#report_pager'),
    rowNum:         lastRowNum,
    rowList:        [10,20,50,100],
    viewrecords:    true,
    clearSearch:    false,
    caption:        "Contracts Dashboard",
    sortable:       true,
    shrinkToFit:    false,
    excel:          true,
    ajaxSelectOptions: {type: "GET"},
    gridComplete: function() {//removed code for simplicity}
});
jQuery("#contract_grid").navGrid('#report_pager',{
    edit:false,
    add:false,
    del:false,
    search:false,
    refresh:false
}).navButtonAdd("#report_pager",{
    caption: "Columns",
    buttonicon: "ui-icon-calculator",
    title: "Select and Reorder Columns",
    jqModal: true,
    onClickButton: function(e){

        $('#contract_grid').jqGrid('columnChooser', {
            dialog_opts: {
                modal: true,
                minWidth: 470,
                show: 'blind',
                hide: 'explode'
            },
            done : function (perm) {
              if (perm) {
                  // "OK" button are clicked
                  $('#contract_grid').jqGrid("remapColumns", perm, true);

                  // **UPDATED WORKING CODE**get the width set for the grid
                  var gwdth = $('#contract_grid').jqGrid("getGridParam","width");
                 // set the tblwidth so the grid does not get resized
                  $('#contract_grid').setGridParam({tblwidth:gwdth});

              } else {
                  // we can do some action in case of "Cancel" button clicked
              }
           }
        }); 
    }

});

您没有发布代码中最重要的部分:colModel。至少应该 "fixed" 的列的定义很重要。

应该固定宽度的列应该有fixed: true 属性。它将防止其宽度发生变化。

更新:我仍然不确定你想实现什么,但我不知道 jqGrid 4.8.2 的具体行为,因为我开发了替代的 fork free jqGrid which you can try just including URL to the code on GitHub (see here).

一般来说有两个重要的内部参数:tblwidthwidthtblwidth 是网格主体的宽度(<table>)并且存在 width 这是网格的 宽度: 外部宽度 div。我想 jqGrid 4.8.2 在 tblwidthwidth 之间做出了错误的选择。我建议您尝试将 done 回调代码中的 var gwdth = $('#contract_grid').jqGrid("getGridParam","width"); 替换为 var gwdth = $('#contract_grid').jqGrid("getGridParam","tblwidth");

免费的 jqGrid 保存了你在 jqGrid 的 widthOrg 选项中创建网格时使用的 original width 并且它在 columnChooser

$self.jqGrid("setGridWidth",
    !p.autowidth && (p.widthOrg === undefined || p.widthOrg === "auto" || p.widthOrg === "100%") ? p.tblwidth : p.width,
    p.shrinkToFit);

您没有指定 autowidth: true 并且在创建网格时使用了 width: 1200 选项(稍后将是 widthOrg)。所以免费的 jqGrid 应该在你的情况下默认使用 tblwidth 而不是 width.