1

Is there a way to tell the jqGrid to ignore the case while grouping? I don't want to change the data as some will be upper case, some lower case and others mixed case.

I'm using jqGrid 4.4.4

Jonas Stawski
  • 6,682
  • 6
  • 61
  • 106

1 Answers1

3

The question is good, but ... the current implementation of grouping in jqGrid allow grouping only exact values. I remember close requirements when one wanted to group by month instead by exact date.

After some analyse of the source code of jqGrid I hope that I found very simple and very flexible way to do more flexible grouping. I suggest to modify the line

if( typeof v !== "object" && grp.lastvalues[i] !== v ) {

to

if (typeof v !== "object" &&
        ($.isFunction(grp.isInTheSameGroup) ?
            !grp.isInTheSameGroup(grp.lastvalues[i], v) :
            grp.lastvalues[i] !== v)) {

After this one can define isInTheSameGroup function inside of groupingView:

grouping: true,
groupingView: {
    ...
    groupField: ["name"], // the column by which we group
    isInTheSameGroup: function (x, y) {
        return String(x).toLowerCase() === String(y).toLowerCase();
    }
}

The demo display the following results:

enter image description here

I used in the demo modified version of jquery.jqGrid.src.js of jqGrid 4.4.5. The version of jquery.jqGrid.src.js of jqGrid 4.4.4 modified in the same way you can find here.

I will post later my suggestion to trirand. I hope that the next version of jqGrid will contain the feature.

UPDATED: As promised, I posted the corresponding feature request to trirand.

UPDATED 2: I posted the pull request with a little more changes of grouping module of jqGrid. The demo demonstrates how new features can be used. It uses 2-level grouping and displays the following results:

enter image description here

UPDATED 3: The pull request which I sent to trirand is merged now to the main code of jqGrid. So the next version of jqGrid (after 4.4.5) will supports isInTheSameGroup and formatDisplayField arrays of callbacks inside of groupingView. If your case it would look like

groupingView: {
    groupField: ['name'],
    formatDisplayField: [
        function (displayValue) { //, value, cm, index, grp) {
            return String(displayValue).toLowerCase();
        }
    ],
    groupColumnShow: [true],
    groupDataSorted: true,
    isInTheSameGroup: [
        function (x, y) {
            return String(x).toLowerCase() === String(y).toLowerCase();
        }
    ]
}

The callbacks isInTheSameGroup[0] and formatDisplayField[0] will be used by grouping by groupField[0]. Because jqGrid support multilevel grouping the isInTheSameGrou and formatDisplayField properties are arrays of callback functions instead of just callback function.

At the beginning of grouping jqGrid sort data by grouping column. One can use sorttype defined as function to customize the first step (see the answer). I don't though about the step during writing of your answer. Probably usage of sorttype: function (cellvalue) {return String(cellvalue).toLowerCase();} could already solve your problem.

Then the function isInTheSameGroup[level] will be used consequently to compare the value of grouping column from of previous row with the corresponding value of the current row. The function isInTheSameGroup[level] will be called with the values. If your callback returns true then the row will be grouped with the previous one.

The callback formatDisplayField[level] allows to customize the information displayed in the grouping header. In the example above I convert the data to low case.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Oleg, thank you very much for the detailed response and the feature request. I don't like forking code as it then becomes very hard to update to newer versions. Thus, I solved the problem by adding another hidden column with the data in uppercase and grouping by that column. – Jonas Stawski Apr 22 '13 at 17:53
  • @JonasStawski: You are welcome! I understand your workaround and your point of view. Nevertheless I see the problem as common. I suggested before many features which are now the part of jqGrid and it helps other people. I hope that Tony (trirand) will do implement the feature which I suggested (it doesn't matter whether it will be implemented in the way in which I suggest it or in some other way). – Oleg Apr 22 '13 at 18:45
  • I agree. Hopefully it does and I won't have to fork it and I can remove my hacks – Jonas Stawski Apr 22 '13 at 19:56
  • @JonasStawski: The code of jqGrid on github is now changed to allows very flexible grouping. See **UPDATE 2** and **UPDATE 3** parts of my answer. Additionally I recommend you to take a look in [the answer](http://stackoverflow.com/a/16221518/315935). The trick with the usage of `sorttype` defined as function could be helpful for you. – Oleg Apr 29 '13 at 09:35