Rectangle 27 0

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:

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.

Thanks for the cool demo. See my "update" on what I ended up doing. Any idea on how to calculate the exact width in pixels that I should return?

@dreamfly: You are welcome! I find "update" a little strange. First of all it's new question (the original was "How to adjust the column width of jqGrid AFTER the data is loaded?"). Seconds I don't recommend to change source code of jqGrid if it's really required. Instead of that you can use loadComplete or beforeProcessing. Now about your main question: I think that you can wrap cell content inside of <span>...</span>. After that you can use jQuery.width to compute the exact width in pixel of every <span> and so of every text in the cell. Finally you can use setColWidth.

I am well aware of the pitfall of changing the source code. This is just a proof of concept on how difficult it is to implement this in my opinion a rather simple but very useful feature. especially the simple HTML table does that natively. Of course efficiency was also achieved without re-processing the data.

jquery - How to adjust the column width of jqgrid AFTER the data is lo...

jquery html jqgrid width
Rectangle 27 0

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:

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.

Thanks for the cool demo. See my "update" on what I ended up doing. Any idea on how to calculate the exact width in pixels that I should return?

@dreamfly: You are welcome! I find "update" a little strange. First of all it's new question (the original was "How to adjust the column width of jqGrid AFTER the data is loaded?"). Seconds I don't recommend to change source code of jqGrid if it's really required. Instead of that you can use loadComplete or beforeProcessing. Now about your main question: I think that you can wrap cell content inside of <span>...</span>. After that you can use jQuery.width to compute the exact width in pixel of every <span> and so of every text in the cell. Finally you can use setColWidth.

I am well aware of the pitfall of changing the source code. This is just a proof of concept on how difficult it is to implement this in my opinion a rather simple but very useful feature. especially the simple HTML table does that natively. Of course efficiency was also achieved without re-processing the data.

It came full cycle! I Just tried the free jqgrid with the {autoResizable: true } on every column and it worked. However the performance on IE11 is rather slow with just 500 rows and 13 columns of data. It seems it's running the autoResize logic on every scroll action.

@dreamfly. Could you provide the demo which uses non-minimized the latest version of free jGrid? Try the demo and make double-click between column headers. Moreover I not full understand why you want to use 500 rows without paging? The monitor is unable to display 500 rows (maximally probably 35). In the way you reduce performance of the grid in 100 times or more. the demo with 40000 rows works quickly.

jquery - How to adjust the column width of jqgrid AFTER the data is lo...

jquery html jqgrid width