2

I am trying to utilize the column header group functionality of jqgrid's jqpivot to have two column headers (A header for day of the week, and another header for the actually date).

    var crewAttendance = { "total": 1, "page": 0, "records": 7,
            "rows": [
                { "ID": "1", "UserID": "10", "AttendanceTypeID": "6", "AttendanceDate": "04/05/15", "AttendanceDay": "SUN" },
                { "ID": "21", "UserID": "10", "AttendanceTypeID": "12", "AttendanceDate": "04/06/15", "AttendanceDay": "MON" },
                { "ID": "41", "UserID": "10", "AttendanceTypeID": "19", "AttendanceDate": "04/07/15", "AttendanceDay": "TUE" },
                { "ID": "61", "UserID": "10", "AttendanceTypeID": "17", "AttendanceDate": "04/08/15", "AttendanceDay": "WED" },
                { "ID": "81", "UserID": "10", "AttendanceTypeID": "17", "AttendanceDate": "04/09/15", "AttendanceDay": "THU" },
                { "ID": "101", "UserID": "10", "AttendanceTypeID": "19", "AttendanceDate": "04/10/15", "AttendanceDay": "FRI" },
                { "ID": "121", "UserID": "10", "AttendanceTypeID": "19", "AttendanceDate": "04/11/15", "AttendanceDay": "SAT" }
            ], "userID": ["10"], "firstName": ["Christopher"]
        }
        // create jqPivot Grid
    var grid = $("#pvtCrewAttendance");
    grid.jqGrid("jqPivot",
        crewAttendance.rows,
        {
            xDimension: [
                {
                    isGroupField: false,
                    width: 70,
                    dataName: 'UserID',
                    label: 'UserID'
                }
            ],
            yDimension: [
                {
                    dataName: 'AttendanceDay'
                },
                {
                    dataName: 'AttendanceDate'
                }
            ],
            aggregates: [
                {
                    aggregator: 'max',
                    width: 80,
                    member: 'AttendanceTypeID',
                    summarytype: 'count',
                    sortable: true,
                    resizable: false
                }
            ],
            groupSummary: false,
            colTotals: true
        },
    // grid options
        {
        height: 'auto',
        pager: '#nav',
        caption: 'Crew Attendance'
    });

    var cm = grid.getGridParam('colModel');
    for (var iCol = 0; iCol < cm.length; iCol++) {
        var cmi = cm[iCol];
        if (cmi.label.length == 3 || cmi.label.length == 0 || cmi.label == '\&nbsp\;') {
            grid.jqGrid('hideCol', cmi.name);
        }
    }
    grid.trigger('reloadGrid');

and so far it is working for SUN and MON but for some reason, jqPivot's column header grouping is not working properly for the rest of the days (TUE and WED combines, and then THU, FRI, and SAT also combines but they should not because they are at different dates). See jsFiddle here

How can I make it work for the rest of the days?

NOTE: I cannot combine the two headers together because I am going to rotate the date by 90 degrees. I've already done this part, but removed it in the fiddle to lessen the clutter.

1 Answers1

1

Thank you for the bug report!

After long debugging I found and fixed the bug. Your demo uses the latest sources from GitHub directly. So the the last changes are applied to the demo too. You can verify that it displays now the correct results.

UPDATED: I implemented more fixes in the commit and in this one for jsPivot module. There should fix the old bugs, which come from jqGrid 4.7. Now your JSFiddle demo https://jsfiddle.net/OlegKi/4en1b68c/7/ produce correct results (I made some cosmetic changes in the demo). I hope that the code works correct for more complex input data too. Moreover you can remove the last block with hiding of columns having cmi.label.length == 3 || cmi.label.length == 0 || cmi.label == '\&nbsp\;'. I interpret the existence of the columns as one more bug and so I modified the code so that the columns don't exist more in the grid.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks @Oleg for responding, but I think the bug is not yet fixed. I changed the javascript references to your repository but there wasn't any changes in the output (the column headers that shouldn't combine still combines). See JSFiddle [here](https://jsfiddle.net/4en1b68c/5/) – RoyceBautista May 14 '15 at 03:27
  • 1
    @Gelo32k: I made more changes (see **UPDATED** part) which should fix the problem. Please try once more in more complex case as the demo which you posted. – Oleg May 16 '15 at 11:09
  • Hi @Oleg, Thank you very much for helping me out, the jqPivot now works perfectly! Here is the final product: https://jsfiddle.net/ngrL712u/1/ The column totals may look inconsistent but that is intentional :) – RoyceBautista May 18 '15 at 09:18
  • 1
    @Gelo32k: You are welcome! Please report me all problems which you could find in the future. I'm still not sure that jqPivot works perfect, but at least I've fixed all bugs which I found. By the way I use rotated headers active too. Look at the modified demo https://jsfiddle.net/OlegKi/ngrL712u/2/ where I show how to calculate the height of rotated columns. I thought to add property `rotate: true` to `colModel`, but I still didn't find time for it. I want to concentrate on publishing free jqGrid 4.9 and just to make the existing changes till the logical end and to fix the current bugs. – Oleg May 18 '15 at 09:27
  • @Gelo32k: I want inform you that I implemented some new features in jqPivot described in [the issue thread](https://github.com/free-jqgrid/jqGrid/issues/63). The features could be interesting for you too. Additionally it would be important to test whether the latest changes do break some other existing functionality of jqPivot. I hope not, but more testing is required. – Oleg May 19 '15 at 21:47
  • Thank you for the info. I checked everything's fine. I don't think I will be upgrading to jqGrid 4.9.0. right now I just edited 4.8.0 to include this change in jqPivot because I am having trouble with formediting on 4.9.0 and jQuery UI Layout compatibility. my jqGrid is contained inside a div with position: absolute; z-index: 0; so when 4.8.0 came out (and dialogs are rendered inside the gbox) I just modified the value of toTop in jqgrid source and it works fine, but when I do this in the latest version of jqgrid, the dialog gets shown on the top left corner of the browser. – RoyceBautista May 20 '15 at 03:22
  • the Incompatibility is detailed [here](http://stackoverflow.com/questions/30339851/formediting-modals-shows-at-wrong-position) – RoyceBautista May 20 '15 at 03:38
  • 1
    @Gelo32k: I rewrote the `jqPivot` absolutely. [The wiki article](https://github.com/free-jqgrid/jqGrid/wiki/jqPivot-in-version-4.9) describes the changes. I tried to hold the compatibility, but the names of the columns are changes now. So you have to change `UserID` which you use in custom formatter and in `onInitGrid` to patch the `userdata` from `.UserID` to `.x0`. See http://jsfiddle.net/OlegKi/ngrL712u/3/. I recommend you to examine the names of `colModel` generated by new version of `jqPivot`. – Oleg Jun 03 '15 at 11:30
  • Hi @Oleg I tried on the latest codes of jqGrid and everything works fine. :) thank you. – RoyceBautista Jun 05 '15 at 02:08