Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQgrid: multiple column row headers

I am trying to extend my jQGrid to have multiple rows for the header.

It will look something like this

               -----------------------
Level 1 - >    | Application         |
               -----------------------  
Level 2 - >    |Code    | Name       |  
               -----------------------
               | 0002827| Mobile Phone1
               | 0202827| Mobile Phone2
               | 0042827| Mobile Phon3e
               | 0005827| Mobile Phone4
               | 0002627| Mobile Phon5e
               | 0002877| Mobile Phone6
               | 0002828| Mobile Phone7

I am wondering how to do this with jQGrid 3.8.2? Any ideas?

like image 974
Jonathan Avatar asked Jan 17 '11 11:01

Jonathan


4 Answers

The problem is really not so easy as it looks like at first glance. I tried to find a simple solution, but the best result which I found you can see here: enter image description here

The order of the headers (level 1 and level 2) is not as one would like to have. Other attempts like this or this are buggy because the sorting and column resizing works not more correct.

For the understanding: the grid moves the <thead> outside of the table and places it inside of separate which are placed above the table (see here for more information). It allows including some additional information like searching toolbar between the table header and the table body. Other places in the jqGrid code like column resizing and column sorting works incorrect if there are exist other headers (one more <tr> with <th> elements) over the main column headers. So only the inserting of additional column headers under the main columns headers (which looks not nice of course) not breaks the sorting and the resizing of columns.

UPDATED: See the answer which do provide the solution of the problem under some restrictions.

like image 174
Oleg Avatar answered Nov 06 '22 02:11

Oleg


I know this is a late answer but for future reference header grouping is now included in version 4.2.0 of jqGrid

like image 33
Raggi Steinsen Avatar answered Nov 06 '22 00:11

Raggi Steinsen


Modified the original idea by Oleg and made it into a function that can make several "spanned" headers:

function head_groups(mygrid, settings){

    var colModel, header, config, ths;

    if (typeof mygrid == 'string') mygrid = $(mygrid);

    colModel = mygrid[0].p.colModel;
    ths = mygrid[0].grid.headers;
    header = mygrid.closest("div.ui-jqgrid-view").find("table.ui-jqgrid-htable > thead");

    if (!header.children("tr.head_group").length) {
        header.find("th").attr('rowspan', 2);
        header.append('<tr class="head_group"></tr>');
    }

    for (c in settings) {

        config = settings[c]; // caption, col, span

        for(var i=0; i<colModel.length; i++) {

            if (colModel[i].name == config.col) {
                for(var s=0; s<config.span; s++) {
                    $(ths[i+s].el).removeAttr('rowspan');
                }
                i +=s; // skip unnecessary cycles
                header.children("tr.head_group").append('<th class="ui-state-default ui-th-ltr" id="span_'+config.col+'" colspan="'+config.span+'" role="columnheader">'+config.caption+'</th>');
            }
        }
    }
}

Usage sample:

head_groups("table#results", [
    {caption: 'Test 1', col: 'num', span: 2},
    {caption: 'Result', col: 'sta', span: 3},
    {caption: 'Bla bla bla', col: 'bl2', span: 2}
]);

It also adds a class for the header row and IDs for the header cells for some styling or special functionality.

In fact this can be easily integrated in the jqGrid core :)

like image 35
Exzo Avatar answered Nov 06 '22 01:11

Exzo


According to Help needed in Multiple column grouping (jQGrid 3.8.2), the jqGrid support team indicates:

This is currently not supported. Multiple column headers in a single column (subcolumns) are not currently a feature of jqGrid.

like image 36
Justin Ethier Avatar answered Nov 06 '22 00:11

Justin Ethier