Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to adjust the column width of jqgrid AFTER the data is loaded?

My ultimate goal is to have jqgrid to auto-set the column width according to the data content. As part of the path to go there, I need to be able to set the column width AFTER the data is loaded by which time I would know the maximum width of each column. But once I know the maximum width of a column, how can I set the width of each column in the "loadComplete" event and have the grid refresh with the new width of each column? Most of the posts I've found on the net are regarding the grid's overall width. What I want is to set each individual column's width and have the horizontal scrollbar appear automatically if the total width is too long.

Update: After seeing Oleg's awesome demo, I ended up coding this in the dqGrid (4.5.4) itself. Here's what I did:

Locate the function

addJSONData = function(data,t, rcnt, more, adjust) {

then locate within this function

            for (j=0;j<rowReader.length;j++) {
                v = $.jgrid.getAccessor(cur,rowReader[j]);
                rd[ts.p.colModel[j+gi+si+ni].name] = v;
                rowData.push(addCell(idr, v, j + gi + si + ni, i + rcnt, cur, rd));

                // my addtion: stores the largest header size
                var newWidth = v.length * 6; 
                if (ts.grid.headers[j].width < newWidth) {
                    ts.grid.headers[j].width = newWidth;
                }
            }

Then right before the end } of this function, add the following

        // my addition: invoke the resizing logic
        for (j = 0; j < rowReader.length; j++) {
            ts.grid.resizing = { idx: j };
            ts.grid.dragEnd();
        }

After that, the grid will adjust the width of each column according to the content. One thing I still need help on is how to calculate preciously the new width. My current hard-coded calculation

var newWidth = v.length * 6;

is obviously not very scalable.

like image 506
dreamfly Avatar asked Nov 15 '13 23:11

dreamfly


1 Answers

Short answer on your question could be: jqGrid provide no method which allows to change column width after grid have created. In such cases I remind well known aphorism of Kozma Prutkov: "Who prevents you invent gunpowder waterproof?". It means about the following: if something not exists it can be still invented. So I wrote such method based on the solution suggested in the answer where I suggested to call internal method dragEnd to resize grid's column.

The demo demonstrate the usage of new method. The demo allows to choose some column of the grid and then to change the width to new value which you specify:

enter image description here

The code of new method you can find below

$.jgrid.extend({
    setColWidth: function (iCol, newWidth, adjustGridWidth) {
        return this.each(function () {
            var $self = $(this), grid = this.grid, p = this.p, colName, colModel = p.colModel, i, nCol;
            if (typeof iCol === "string") {
                // the first parametrer is column name instead of index
                colName = iCol;
                for (i = 0, nCol = colModel.length; i < nCol; i++) {
                    if (colModel[i].name === colName) {
                        iCol = i;
                        break;
                    }
                }
                if (i >= nCol) {
                    return; // error: non-existing column name specified as the first parameter
                }
            } else if (typeof iCol !== "number") {
                return; // error: wrong parameters
            }
            grid.resizing = { idx: iCol };
            grid.headers[iCol].newWidth = newWidth;
            grid.newWidth = p.tblwidth + newWidth - grid.headers[iCol].width;
            grid.dragEnd();   // adjust column width
            if (adjustGridWidth !== false) {
                $self.jqGrid("setGridWidth", grid.newWidth, false); // adjust grid width too
            }
        });
    }
});

You can include the method in your project and then use setColWidth method. The first parameter of the method is either the column index or the column name. The second parameter is a number which specify new value of the width on the column. The third parameter is optional. If it's not specified then the width of the grid will be automatically adjusted corresponds to the changing of the column width. So the grid will have no scroll bars if it hasn't before changing the column width. If you want to hold the original grid width you should specify false as the third parameter of setColWidth method.

UPDATED: The latest (updated) version of setColWidth can be downloaded from github. The new free version of jqGrid which is developing currently here includes the method as in the basis module of jqGrid. So if you use jquery.jqGrid.min.js, jquery.jqGrid.min.map and jquery.jqGrid.src.js from here then you don't need include jQuery.jqGrid.autoWidthColumns.js with setColWidth as plugin.

UPDATED 2: I modified the above code to corresponds the latest version of setColWidth which I published to github.

UPDATEd 3: The method setColWidth is included in free jqGrid fork of jqGrid, which I develop since the end of 2014. It includes many other new features like extending the width of the column based on the width of content of the column. See the wiki article for more details.

like image 116
Oleg Avatar answered Oct 14 '22 00:10

Oleg