Search code examples
javascriptjqueryjqgridsubgrid

How do I set the header alignment in a jqGrid subgrid?


Specifically the simple subgrid, not "grid as subgrid".

I've tried a variety of methods, but none seem to work.

If I hook into subGridBeforeExpand, the table isn't ready for me to select the headers and set the css.

If I hook into subGridRowExpanded the subGrid won't even render.

The align property in the subGridModel only affects the cell value.

Here is my model for reference:

subGrid: true,
subGridUrl: myUrl,                    
subGridModel: [{ 
     name: ["Item", "Qty"],
     width: ["200", "100"],
     align: ["right", "right"],
     param: ["Id"]
}]

Solution

  • You are right, that there are too few callbacks in Subgrids and Treegrids. Nevertheless because I found your question very interesting challenge (+1 from me) I do found a workaround.

    You can do the following:

    var $grid = $("#grid"), sid;
    $grid.jqGrid({
        //... your other settings
        subGrid: true,
        serializeSubGridData: function(p) {
            sid = p.id; // save id from the last request
            return p;
        },
        ajaxSubgridOptions: {
            complete: function (sxml) {
                var ts = $grid[0], $subgridHeaders;
                if (ts.p.subgridtype === "xml") {
                    ts.subGridXml (sxml.responseXML, sid);
                } else {
                    ts.subGridJson($.jgrid.parse(sxml.responseText), sid);
                }
                // now the subgrid is completed and we can modify
                // style of subgrid headers
                $subgridHeaders = $('#' + $.jgrid.jqID(ts.id + '_' + sid))
                    .find("th.ui-th-subgrid");
                // now we can do some custom actions:
                $($subgridHeaders[0]).css("text-align", "left");
                $($subgridHeaders[1]).css("text-align", "right");
            }
        }
    });
    

    You can here the demo which looks like the following after the expanding of subgrids:

    enter image description here