2

I am using jqGrid 4.8.2 and have set up my grid with a fixed width. But, when I modify the displayed columns using columnChooser, the width of the grid is readjusted to the combined width of all columns and the horizontal scroll is gone. I've tried resetting the width within the 'done' columnChooser param but it does not work. After some debugging it seems the resizing happens after 'done' is completed and after the dialog window is closed. I created a button to reset the width manually and that works just fine so I know resetting the width can happen. I just don't know which event to use to trigger it.

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
              }
           }
        }); 
    }

});

1 Answers1

1

You don't posted the most important part of your code: colModel. At least the definition of the column which should be "fixed" is important to know.

The column which should have fixed width should have fixed: true property. It will prevent its width from changing.

UPDATED: I'm still not sure what you want to implement, but I don't know specific behavior of jqGrid 4.8.2 because I develop alternative fork free jqGrid which you can try just including URL to the code on GitHub (see here).

In general there are exist two important internal parameters: tblwidth and width. The tblwidth is the width of the body of the grid (the <table>) and there are exist width which is the total width of the grid: the width of outer div. I suppose that jqGrid 4.8.2 makes wrong choice in your case between tblwidth and width. I would recommend you to try to replace var gwdth = $('#contract_grid').jqGrid("getGridParam","width"); inside of the code of done callback to var gwdth = $('#contract_grid').jqGrid("getGridParam","tblwidth");

Free jqGrid saves the original width which you used during creating the grid in widthOrg option of jqGrid and it uses the following line inside of columnChooser

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

You don't specified autowidth: true and you used width: 1200 option during creating the grid (which will be widthOrg later). So free jqGrid should uses tblwidth in your case by default instead of width.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • I updated the code with a portion of the colModel. I don't want to enforce fixed column widths. I want a fixed grid width with the overflow handled by a horizontal scroll. That all works fine. My issue is with whatever is firing after the columnChooser window closes. It removes the grid width so everything shows up on the screen and the horizontal scroll is gone. I want to attach to whatever is firing and make sure the grid width is reset. I just don't know what that is. – user2754423 Jun 05 '15 at 15:22
  • 1
    @user2754423: I posted **UPDATED** part of my answer. I recommend you to try to use `tblwidth` parameter instead of `width`. I recommend you to try Free jqGrid directly from the GitHub (you need just change 3 URLs temporary) and to see what do my fork of jqGrid by default. – Oleg Jun 05 '15 at 15:45
  • Thank you so much for your help! Setting tblwidth to the width value inside of done did the trick. I will post the working code. – user2754423 Jun 05 '15 at 16:35
  • @Oleg you are a boss. You saved me on the multiselect and this issue after updating to 4.8.2. Thanks! – masterwok Jun 22 '15 at 19:54