Ignore case while grouping

后端 未结 1 757
梦毁少年i
梦毁少年i 2020-12-04 00:39

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.

相关标签:
1条回答
  • 2020-12-04 01:34

    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.

    0 讨论(0)
提交回复
热议问题