3

I have many column in my Jqgrid.All Columns showing Extra space on both sides.i want that column without Extra spacing. Column Width should have width according to its content. I have tried autowidth but its not working.

Actual Behavior is looks like:

-----------------------------------------------
|     Name     |     Mobile     |    Email    |
----------------------------------------------- 

And What I need is :

----------------------
|Name|Mobile No|Email|
----------------------

here is my code

 $("#list").jqGrid({
     datatype: "local",
     data: mydata,
     colNames: ["Name", "Mobile", "Email", "Amount", "Tax", "Total", "Closed", "Shipped via", "Notes"],
     colModel: [
                { name: "id", width: 65, align: "center", sorttype: "int", hidden: true },
                { name: "invdate", width: 80, align: "center", sorttype: "date",
                    formatter: "date", formatoptions: { newformat: "d-M-Y" }, datefmt: "d-M-Y"
                },
                { name: "name", width: 70 },
                { name: "amount", width: 75, formatter: "number", sorttype: "number", align: "right" },
                { name: "tax", width: 55, formatter: "number", sorttype: "number", align: "right", hidden: true },
                { name: "total", width: 65, formatter: "number", sorttype: "number", align: "right" },
                { name: "closed", width: 75, align: "center", formatter: "checkbox",
                    edittype: "checkbox", editoptions: { value: "Yes:No", defaultValue: "Yes" }
                },
                { name: "ship_via", width: 100, align: "center", formatter: "select",
                    edittype: "select", editoptions: { value: "FE:FedEx;TN:TNT;IN:Intim", defaultValue: "Intime" }
                },
                { name: "note", width: 70, sortable: false }
            ],
     rowNum: 10,
     rowList: [5, 10, 20],
     pager: "#pager",

     rownumbers: true,
     sortname: "invdate",
     viewrecords: true,
     sortorder: "desc",
     caption: "Test for AltRows",
     height: "auto"
});

Kindly Solve my problem.

jasonscript
  • 6,039
  • 3
  • 28
  • 43
Mohsin Nasir
  • 33
  • 1
  • 1
  • 4

1 Answers1

5

The question is old. jqGrid don't provide variable column width. It uses always fixed column width and the width of every column will be set during creating the grid even before filling the grid with data. Moreover jqGrid don't provide a method which allows you to change the width of the column after the grid is created.

Nevertheless the requirement stay. One need to create some grids with columns which width are dynamically set based on content of the texts in the column.

I suggested in the answer setColWidth method which can do change column width after the grid is created. Using the method one do can suggest some implementation of your requirements. I created the demo for you which demonstrates this. It uses the following code

$("#list").bind("jqGridAfterLoadComplete", function () {
    var $this = $(this), iCol, iRow, rows, row, cm, colWidth,
        $cells = $this.find(">tbody>tr>td"),
        $colHeaders = $(this.grid.hDiv).find(">.ui-jqgrid-hbox>.ui-jqgrid-htable>thead>.ui-jqgrid-labels>.ui-th-column>div"),
        colModel = $this.jqGrid("getGridParam", "colModel"),
        n = $.isArray(colModel) ? colModel.length : 0,
        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];
        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());
            }
        }
        $this.jqGrid("setColWidth", iCol, colWidth);
    }
});

First of all I wrap the content of all cells of the grid and the content of the column headers within the span: <span class='mywrapping'></span>. It allows me to calculate the width of the content of the cells. Then I goes through all rows and all rows and find the max width which I use in the call setColWidth method.

Be carefully that the above solution only the first attempt to implement "autowidth" of columns based of the content. It could not work in case of more sophisticated content of the grid.

By the way you the width of columns will be changed on sorting and on paging because the maximal width of columns will be changed in the case.

UPDATED: If one uses columnChooser or other methods one need to recalculate the column width on more events as jqGridAfterLoadComplete. To do this one need just add the corresponding event name (event names divided spaces) in the bind/on after "jqGridAfterLoadComplete". For example the answer demonstrates recalculation the column widths after columnChooser. One uses just $("#list1").on("jqGridAfterLoadComplete jqGridRemapColumns", function () {...}); instead of $("#list").bind("jqGridAfterLoadComplete", function () {...});

UPDATED 2: The functionality in included out-of-the-box in free jqGrid, the fork of jqGrid, which I develop starting with the end of 2014. The feature was included already in the first version (v4.8) of free jqGrid. See the wiki. The current version of free jqGrid is 4.13.0. Thus one don't need to follow the tricks described above and just to upgrade jqGrid to the current version of free jqGrid. One can include cmTemplate: { autoResizable: true } to make all columns auto-resizable and to add autoresizeOnLoad: true option to reset the width of all columns at the end of every loading. Alternatively one can call autoResizeAllColumns() method whenever one want recalculate the width of all auto-resizable columns.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • there is one problem in this approch.after sorting two column the sorting icon of first(previous) column removes . – Mohsin Nasir Jun 23 '14 at 10:21
  • @MohsinNasir: Could you describe the test case in details? Could one reproduce the problem on [the demo](http://www.ok-soft-gmbh.com/jqGrid/AutowidthColumn.htm) which I posted? – Oleg Jun 23 '14 at 10:28
  • sorry for disturbing u again thats was my mistake of understanding . thanks – Mohsin Nasir Jun 23 '14 at 10:38
  • @MohsinNasir: No problem! By the way [the answer](http://stackoverflow.com/a/8914163/315935) and [this one](http://stackoverflow.com/a/9620930/315935) shows how one could modify the standard behavior of hiding sorting icons. – Oleg Jun 23 '14 at 10:42
  • @Oleg this works for me but when the cell text has very big length(>1500) i need to wrap the content and make sure there is no horizontal scroll bar appear. Is there any way that we can do this? – Sanju Rao Feb 23 '16 at 07:59
  • 1
    @SanjuRao: Free jqGrid have `maxColWidth` property which you can specify either as the option of jqGrid or in `colModel`. Default value is `300` (`autoResizing: {maxColWidth: 300}`). You can increase 300 to 1500. To have cell wrapping you need just add the corresponding CSS rule. See [the answer](http://stackoverflow.com/a/6915486/315935) for example. – Oleg Feb 23 '16 at 08:06
  • @Oleg I just notice in [here](http://www.ok-soft-gmbh.com/jqGrid/AutowidthColumn.htm). If I go to page 3 and apply sort on Date Field It returns me to first page. rather than keeping me in the 3rd page and applying sort. Is it possible to be in same page and sort it? – Sanju Rao Feb 24 '16 at 05:02
  • 1
    @SanjuRao: Sorry, but it's important to know the version and the fork of jqGrid, which you use. Do you use [free jqGrid](https://github.com/free-jqgrid/jqGrid) 4.13.0? I could easier answer on your question if I would see your JavaScript code. How you "go to page 3"? Do you use `reloadGrid`? Do you use `page` option of `reloadGrid`? Everything is possible, but I have to know details of your implementation. – Oleg Feb 24 '16 at 06:25
  • I use the page next icon that is available on bottom and go the respective next pages. – Sanju Rao Feb 24 '16 at 08:20
  • 1
    @SanjuRao: Sorry, but what is your current problem. The user clicks on the "Next Page" button on the page 2 and the page 3 will be not displayed? I suggested you to describe all more exactly. I think it would be better if you post separate question where you describe the environment and the problem exactly. Currently we spend our time without any results. I don't know for example the `datatype`, which you use, I don't know whether you use `loadonce: true` scenario, and the test case for the problem is unknown for me too... – Oleg Feb 24 '16 at 08:41
  • @Oleg you are right. Will have a separate post on this. Nonetheless thanks for your respoonse and help – Sanju Rao Feb 24 '16 at 09:07