0

My grid is as below,

<table id="grid"></table>

var data = [[48803, "DSK1", "", "02200220", "OPEN"], [48769, "APPR", "", "7773333777733337777333377773333777733337777333377773333777733337777333377773333777733337", "ENTERED"]];

$("#grid").jqGrid({
    datatype: "local",
    height: 250,
    colNames: ['Inv No', 'Thingy', 'Blank', 'Number', 'Status'],
    colModel: [{
        name: 'id',
        index: 'id',
        width: 60,
        sorttype: "int"},
    {
        name: 'thingy',
        index: 'thingy',
        width: 90,
        sorttype: "date"},
    {
        name: 'blank',
        index: 'blank',
        width: 30},
    {
        name: 'number',
        index: 'number',
        width: 80,
        sorttype: "float"},
    {
        name: 'status',
        index: 'status',
        width: 80,
        sorttype: "float"}
    ],
    caption: "Stack Overflow Example",
    // ondblClickRow: function(rowid,iRow,iCol,e){alert('double clicked');}
});

var names = ["id", "thingy", "blank", "number", "status"];
var mydata = [];

for (var i = 0; i < data.length; i++) {
    mydata[i] = {};
    for (var j = 0; j < data[i].length; j++) {
        mydata[i][names[j]] = data[i][j];
    }
}

for (var i = 0; i <= mydata.length; i++) {
    $("#grid").jqGrid('addRowData', i + 1, mydata[i]);
}


$("#grid").on("jqGridAfterLoadComplete jqGridRemapColumns", function () {
        var $this = $("#grid"),
        $cells = $this.find(">tbody>tr>td"),
        $colHeaders = $this.closest(".ui-jqgrid-view").find(">.ui-jqgrid-hdiv>.ui-jqgrid-hbox>.ui-jqgrid-htable>thead>.ui-jqgrid-labels>.ui-th-column>div"),
        colModel = $this.jqGrid("getGridParam", "colModel"),
        iCol,
        iRow,
        rows,
        row,
        n = $.isArray(colModel) ? colModel.length : 0,
        cm,
        colWidth,
        idColHeadPrexif = "jqgh_" + this.id + "_";
        $cells.wrapInner("<span class='mywrapping'></span>");
        $colHeaders.wrapInner("<span class='mywrapping'></span>");

        for (iCol = 0; iCol < n; iCol++) {
            cm = colModel[iCol];
            if (cm.hidden) {
                continue;
            }
            colWidth = $("#" + idColHeadPrexif + $.jgrid.jqID(cm.name) + ">.mywrapping").outerWidth() + 25; // 25px for sorting icons
            for (iRow = 0, rows = this.rows; iRow < rows.length; iRow++) {
                row = rows[iRow];
                if ($(row).hasClass("jqgrow")) {
                    colWidth = Math.max(colWidth, $(row.cells[iCol]).find(".mywrapping").outerWidth());
                }
            }
            //$("#grid").jqGrid("setColWidth", iCol, colWidth);
            alert(iCol + colWidth);
            //$("#grid").jqGrid('setColProp',iCol,{width:colWidth});
            $('#grid tr').find('td:eq('+iCol+')').each(function(){$(this).css('width',colWidth);}); // will set the column widths

            //var gw = $("#grid").jqGrid('getGridParam','width');

            //$("#grid").jqGrid('setGridWidth',gw);
        }
    });


<style type="text/css">
    .ui-jqgrid tr.jqgrow td {
        word-wrap: break-word; /* IE 5.5+ and CSS3 */
        white-space: pre-wrap; /* CSS3 */
        white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
        white-space: -pre-wrap; /* Opera 4-6 */
        white-space: -o-pre-wrap; /* Opera 7 */
        overflow: hidden;
        height: auto;
        vertical-align: middle;
        padding-top: 3px;
        padding-bottom: 3px
    }
</style>

I am trying to set auto width for the columns using ,

 $("#grid").jqGrid("setColWidth", iCol, colWidth);

which have resulted me in 'setColWidth is undefined'. BTW i am using jquery1.8.

Then i tried using this way,

$('#grid tr').find('td:eq('+iCol+')').each(function(){$(this).css('width',colWidth);});

this is not giving any script error however it is not setting the width for column as well.

But the width is being printed for each column correctly when i do ,

        alert(iCol + colWidth);

What is that i am doing mistake in this code? Can any help me in this issue?

how can i resolve this issue...?

Need help please...

Thanks in Advance...

Sandy
  • 313
  • 1
  • 8
  • 23
  • Which fork of jqGrid (old jqGrid in version <=4.7, [free jqGrid](https://github.com/free-jqgrid/jqGrid) or [Guriddo jqGrid JS](http://guriddo.net/?page_id=103334)) you use and in which version use use? – Oleg Jul 28 '15 at 15:00
  • @Oleg the challenge I am facing here is 1) this data 7773333777733337777333377773333777733337777333377773333777733337777333377773333777733337 has to be word wrap as well 2) that column namely 'Number' width should be adjusted accordingly... Maximum row width can be upto 10+ lines... Can u please help? – Sandy Jul 28 '15 at 16:40
  • @Oleg : I am using jqGrid 4.4.1 - jQuery.. – Sandy Jul 28 '15 at 16:45

1 Answers1

1

jqGrid 4.4.1 is really very old version. In any way it have no setColWidth method, which I introduced as plugin to jqGrid in the answer originally. If you have to use setColWidth you have to include jQuery.jqGrid.setColWidth.js which you can download from GitHub. Later I included the method and implemented the functionality auto-width in free jqGrid 4.8. It's the fork of jqGrid which I develop (see the readme). See the wiki article for more information.

I recommend you to update to free jqGrid 4.9.1 or to the latest code which you can download from GitHub. In the case you will be have setColWidth method and many other features.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • this is pure legacy code and I cannot upgrade to any of the jquery version.. is there any fix with the existing version of jquery? Please suggest? – Sandy Jul 28 '15 at 16:54
  • @Sandy: Probably you didn't understand what I mean. There are no `setColWidth` method at all in the retro version 4.4.1. You can try to use the plugin `jQuery.jqGrid.setColWidth.js` which you have to download from [here](https://github.com/OlegKi/jqGrid-plugins) and include after jqGrid exactly like I do in [the demo](http://www.ok-soft-gmbh.com/jqGrid/ChangeColumnWidth.htm) from [the answer](http://stackoverflow.com/a/20030652/315935) or another more recent [demo](http://www.ok-soft-gmbh.com/jqGrid/ChangeColumnWidthColumnGrouping.htm). I make no guaranty that it work with jqGrid 4.4.1, try it – Oleg Jul 28 '15 at 17:21
  • Do i need to include autoWidthColumns.js as well for 4.4.1 version? I have just included jQuery.jqGrid.setColWidth.js? – Sandy Jul 28 '15 at 17:49
  • @Sandy: I'm not sure what you do. If you want to use `autoWidthColumns` method (see [the answer](http://stackoverflow.com/a/26753600/315935)) then you have to include `jQuery.jqGrid.autoWidthColumns.js` too. – Oleg Jul 28 '15 at 17:52
  • can you please share link for demo file of .jgrid.extend({ setColWidth? My js are successfully loaded but .. this event is not being fired... – Sandy Jul 28 '15 at 18:32
  • @Sandy: I posted you [the link](http://www.ok-soft-gmbh.com/jqGrid/ChangeColumnWidthColumnGrouping.htm) already. If you opens the source code you will see that if includes `http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.6.0/plugins/jQuery.jqGrid.setColWidth.js` directly after `jquery.jqGrid.src.js`. You need download `jQuery.jqGrid.setColWidth.js` and include it after `jquery.jqGrid.min.js`/`jquery.jqGrid.src.js`. It uses the `$.jgrid.extend(...)` already (see [the code](https://github.com/OlegKi/jqGrid-plugins/blob/master/jQuery.jqGrid.setColWidth.js)). – Oleg Jul 28 '15 at 19:20