1

I took code from this answer and I'm tying to do something like in first picture, to see if it's possible (I know it's is, it's just some JavaScript, HTML and CSS;) ).

Tha second picture is my progress so far. I put Details text in header:

$("#list_subgrid").append("Details").css('width', '100px');

I changed width of first column:

$(".jqgfirstrow").find("td:first").css({"height":"0px", "width":"100px"});

I can get to result in third picture if I change width of bunch of elements all over the place, but not sure that's correct way. And I can't get rid off horizontal scroll bar.

Have no idea how to put Details text into every cell in first column instead of plus sign, but plus sign can stay there.

And how to switch "subgrid" column to be last instead of first is completely beyond my knowledge...

enter image description here

Community
  • 1
  • 1
Roman
  • 13
  • 5
  • You should post your JavaScript code. Do you use [subgrid as grid](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:subgrid_as_grid) or [legacy subgrid](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:subgrid) without implementation of `subGridRowExpanded` callback? subgrid as grid (`subGridRowExpanded`) provides you maximal flexibility. Which version of jqGrid you use and from which fork ([free jqGrid](https://github.com/free-jqgrid/jqGrid), [Guriddo jqGrid JS](http://guriddo.net/?page_id=103334) or an old jqGrid in version <=4.7)? Free jqGrid allows to place any column on any place. – Oleg Apr 01 '16 at 09:26
  • I took code from this answer http://stackoverflow.com/a/10178440/6144040 and try to figure it out. Its subgrid as grid and it's last version of free jqGridd. – Roman Apr 01 '16 at 10:50
  • You should ask one question at one time. The current question liiks like. write me a book about all customization possibilities existing in subgrid. You should ask one question about one problem only. The goal of stackoverflow is providing common information for many readers. The people search for some words and should be able to find the answer on the *specific problem*. Because of that stackoverflow have some rules for writing the question. One from the rule: no commutative questions. Another rule: you should post your current attempts/code and not just ask somebody do 100% of your work. – Oleg Apr 01 '16 at 11:03
  • Inside of `subGridRowExpanded` one creates just **new grid** and one can use any options existing in the standard grid. Thus the part with the `width` of columns and the column headers is absolutely unclear for me. What problem you have? You can just set `width` property for every column or use `colNames` to specify the column headers. – Oleg Apr 01 '16 at 11:05
  • Sorry about that. I rewrited my post. – Roman Apr 01 '16 at 12:38
  • It's no problem! I have to do some other things now, but I will post an example of the usage later (in some hours). By the way you don't need to use manual customization (`$(".jqgfirstrow").find("td:first")` ...). – Oleg Apr 01 '16 at 12:48

1 Answers1

0

I wrote the answer, which you used as an example, many years ago. Now I would just to place the subgrid data together with the main data of the row, like details property below:

var myGridData = [
        // main grid data
        {id: "10", col1: "11", col2: "12", details: [
            // data for subgrid for the id=10
            {id: "10", c1: "aa", c2: "ab", c3: "ac"},
            {id: "20", c1: "ba", c2: "bb", c3: "bc"},
            {id: "30", c1: "ca", c2: "cb", c3: "cc"}
        ]},
        {id: "20", col1: "21", col2: "22", details: [
            // data for subgrid for the id=20
            {id: "10", c1: "xx", c2: "xy", c3: "xz"}
        ]}
    ];

The expression $(this).jqGrid("getLocalRow", rowid) get the item of the data and $(this).jqGrid("getLocalRow", rowid).details is the subgrid data of the row. As the result we can rewrite the original example like on the demo.

To have the column with the fixed text Details we can use simple formatter

formatter: function () {
    return details;
}

where details is defined for example like

var details = "<span class='fa fa-fw fa-plus'></span>&nbsp;" +
                    "<span class='mylink'>Details</span>";

(I used Font Awesome icon) and class mylink defined like

.mylink { text-decoration: underline; }

Now we can hide the "subgrid" column and to open/close the subgrid by simulation of click event on the hidden cell with + or - icon. We receive the following full code

var myGridData = [
        // main grid data
        {id: "10", col1: "11", col2: "12", details: [
            // data for subgrid for the id=10
            {id: "10", c1: "aa", c2: "ab", c3: "ac"},
            {id: "20", c1: "ba", c2: "bb", c3: "bc"},
            {id: "30", c1: "ca", c2: "cb", c3: "cc"}
        ]},
        {id: "20", col1: "21", col2: "22", details: [
            // data for subgrid for the id=20
            {id: "10", c1: "xx", c2: "xy", c3: "xz"}
        ]}
    ],
    $grid = $("#list"),
    details = "<span class='fa fa-fw fa-plus'></span>&nbsp;" +
                "<span class='mylink'>Details</span>";

$grid.jqGrid({
    data: myGridData,
    colModel: [
        { name: "col1", label: "Column 1" },
        { name: "col2", label: "Column 2" },
        { name: "details", label: "Details",
            align: "center", width: 70,
            formatter: function () {
                return details;
            } }
    ],
    cmTemplate: { width: 200 },
    iconSet: "fontAwesome",
    autoencode: true,
    sortname: "col1",
    sortorder: "desc",
    pager: true,
    caption: "Demonstrate how to create subgrid from local data",
    beforeSelectRow: function (rowid, e) {
        var $self = $(this),
            p = $self.jqGrid("getGridParam"),
            $td = $(e.target).closest("tr.jqgrow>td"),
            cm = $td.length > 0 ? p.colModel[$td[0].cellIndex] : null,
            cmName = cm != null ? cm.name : null;
        if (cmName === "details") {
            // simulate opening the subgrid
            $($td.parent()[0].cells[p.iColByName.subgrid]).click();
            // inverse +/-
            var $plusMinus = $td.find("span.fa");
            if ($plusMinus.hasClass("fa-minus")) {
                $plusMinus.removeClass("fa-minus").addClass("fa-plus");
            } else {
                $plusMinus.removeClass("fa-plus").addClass("fa-minus");
            }
        }

        return true;
    },
    subGrid: true,
    subGridRowExpanded: function (subgridDivId, rowid) {
        var $subgrid = $("<table id='" + subgridDivId + "_t'></table>"),
            $subgridDiv = $("#" + subgridDivId),
            subgridData = $(this).jqGrid("getLocalRow", rowid).details;

        $subgridDiv.closest(".subgrid-data").prev(".subgrid-cell").remove();
        var colspan = $subgridDiv.closest(".subgrid-data").attr("colspan");
        $subgridDiv.closest(".subgrid-data").attr("colspan", parseInt(colspan, 10) + 1);
        $subgridDiv.append($subgrid);
        $subgrid.jqGrid({
            idPrefix: rowid + "_",
            data: subgridData,
            colModel: [
                { name: "c1", label: "Col 1" },
                { name: "c2", label: "Col 2" },
                { name: "c3", label: "Col 3" }
            ],
            iconSet: "fontAwesome",
            autowidth: true,
            autoencode: true,
            sortname: "c1"
        });
        $subgrid.jqGrid("setGridWidth", $subgridDiv.width() - 1);
    }
}).jqGrid("hideCol", "subgrid");

The corresponding demo one can see here. After clicking of "+ Detailes" one will see the following:

enter image description here

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Aha, so basically we don't format subgrid column, but instead we add normal column (called Details in this example), format it, and then simulate behavior of subgrid column, which is now hidden. Great solution, I would never figure it out by myself. – Roman Apr 04 '16 at 09:41