Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multisort not maintaining column order #583

Closed
azghanvi opened this issue Mar 5, 2014 · 10 comments
Closed

Multisort not maintaining column order #583

azghanvi opened this issue Mar 5, 2014 · 10 comments

Comments

@azghanvi
Copy link

azghanvi commented Mar 5, 2014

http://www.trirand.com/blog/phpjqgrid/examples/sorting/multiple_sorting/default.php

Referring above example,
i) first remove all sorts.
ii) Sort with customerid (asc)
iii) Sort with orderid

Expected result was to make sort like 'customerid asc, orderid'
But, the result is 'orderid asc, customerid'

Tony, Please tag it for next build.

@oyiadom
Copy link

oyiadom commented Nov 6, 2014

Tony,

My team has been making extensive use of the jqGrid library. We thank you and your colleagues for developing and maintaining it. We are, however, running into the multi-sorting issue described in this thread. I see that the problem (or feature request...depending on your point of view) is well documented on the triand.com Website. (See the references below.) Has a fix been implemented? We are running version 4.6.0 of the library.

Osei

References
http://www.trirand.com/blog/?page_id=393/bugs/cant-sortgrid-with-multiple-columns-when-multisorttrue
http://www.trirand.com/blog/?page_id=393/bugs/order-ignored-in-multicolumn-sort
http://www.trirand.com/blog/?page_id=393/bugs/i-have-taken-liberty-to-consider-this-a-bug

@oyiadom
Copy link

oyiadom commented Nov 11, 2014

Tony,

The code for a preliminary solution to my problem is below. I added a multiSortOrder variable, a getMultiSortColumnIndex() function, and a getMultiSortParams() function. I also updated the multiSort() function. If you are available, please share your feedback on whether or not what I have written fits well with the other parts of jqGrid's code base.

Osei

...

$.fn.jqGrid = function( pin ) {
        if (typeof pin === 'string') {

            ...

        }
        return this.each( function() {

            ...

            var p = $.extend(true,{

                    ...

            }, $.jgrid.defaults, pin || {});
            var ts= this,
            multiSortOrder = [
                    {
                        sortname: "growScore", // TODO: Dynamcally set the value of this object's properties.  Osei, 2014-11-10.
                        sortorder: "desc"
                    }
            ],
            getMultiSortColumnIndex = function (columnName) {
                    var i = 0,
                        numMultiSortColumns = multiSortOrder.length,
                        column = {},
                        index = -1
                        ;

                    for (i; i < numMultiSortColumns; i += 1) {
                        column = multiSortOrder[i];

                            if (column.sortname === columnName) {
                                index = i;
                                break;
                        }
                    }

                    return index;
            },
            getMultiSortParams = function () {
                    var i = 0,
                        numSortColumns = multiSortOrder.length,
                        column = {},
                        sortParams = {
                                sortname: "",
                                sortorder: ""
                        } // sortname, sortorder
                        ;

                    for (i; i < numSortColumns; i += 1) {
                        column = multiSortOrder[i];

                            sortParams.sortname += (i > 0 ? ", " : "") + column.sortname;

                        if (i < (numSortColumns-1)) {
                                sortParams.sortname += " " + column.sortorder;
                        } else {
                                sortParams.sortorder = column.sortorder;
                        }
                    }

                    return sortParams;
            },

            ...

            multiSort = function(iCol, obj ) {
                var cm = ts.p.colModel,
                        selTh = ts.p.frozenColumns ?  obj : ts.grid.headers[iCol].el,
                        so=""
                        ;

                $("span.ui-grid-ico-sort",selTh).addClass('ui-state-disabled');
                $(selTh).attr("aria-selected","false");

                            if(cm[iCol].lso) {
                    if(cm[iCol].lso==="asc") {
                        cm[iCol].lso += "-desc";
                        so = "desc";
                    } else if(cm[iCol].lso==="desc") {
                        cm[iCol].lso += "-asc";
                        so = "asc";
                    } else if(cm[iCol].lso==="asc-desc" || cm[iCol].lso==="desc-asc") {
                        cm[iCol].lso="";
                    }
                } else {
                    cm[iCol].lso = so = cm[iCol].firstsortorder || 'asc';
                }
                if( so ) {
                    $("span.s-ico",selTh).show();
                    $("span.ui-icon-"+so,selTh).removeClass('ui-state-disabled');
                    $(selTh).attr("aria-selected","true");
                } else {
                    if(!ts.p.viewsortcols[0]) {
                        $("span.s-ico",selTh).hide();
                    }
                }
                ts.p.sortorder = "";
                            var column = {},
                                columnCode = "",
                                multiSortColumnIndex = -1,
                                lastSortOrder = "",
                                nextSortOrder = "",
                                multiSortParams = {}
                                ;

                            for (var j = 0; j < cm.length; j += 1) {
                                column = cm[j];
                                columnCode = column.index || column.name;
                                multiSortColumnIndex = getMultiSortColumnIndex(columnCode);

                                // Check if the user is sorting on this column
                                if (column.lso) {
                                        lastSortOrder = column.lso.split("-");
                                        nextSortOrder = lastSortOrder[lastSortOrder.length-1];

                                        // Add or update values 
                                        if (multiSortColumnIndex > -1) {
                                                multiSortOrder[multiSortColumnIndex] = {
                                                    sortname: columnCode,
                                                    sortorder: nextSortOrder
                                                };
                                        } else {
                                                multiSortOrder.push({
                                                    sortname: columnCode,
                                                    sortorder: nextSortOrder
                                                });
                                        }
                                } else if (multiSortColumnIndex > -1) {
                                        // Remove sort entry from the multiSortOrder array
                                        multiSortOrder.splice(multiSortColumnIndex, 1);
                                }
                            }

                            multiSortParams = getMultiSortParams();
                            ts.p.sortname = multiSortParams.sortname;
                            ts.p.sortorder = multiSortParams.sortorder;
            }

            ...
        });
};

...

@tonytomov
Copy link
Owner

Hello,

I will try to implement this in the next release 4.8.1

@tiritas
Copy link

tiritas commented May 26, 2015

Any progress on this? I'm using version 4.8.2 and the column order is still left to right, rather than being based on the order the column headers were clicked.

@tonytomov
Copy link
Owner

Hello,
I hope it will be available in the next release.

Kind Regards

@tonytomov
Copy link
Owner

Hello,

I have added this feature : eb8cb6a

You can download the latest build from GitHub.
Please let us know if this is working as expected, so that the requierment will be closed.

Thank you

@tiritas
Copy link

tiritas commented May 29, 2015

I downloaded the latest version, but the combined jquery.jqgrid.js does not contain the latest changes, even though the grid.base.js file does. Is it possible to provide an updated combined file so that we can test?

@tonytomov
Copy link
Owner

ined version.
I have downloaded the last build and everything is here.
When open the zip look in the js folder - here is the combined version - jquery.jqGrid.js
js folder contain minified folder with minified version

@tiritas
Copy link

tiritas commented May 30, 2015

I just re-downloaded and the combined file (jquery.jqGrid.js) does contain the sort changes. I noticed that the date in the header is different from the one I downloaded yesterday after your post (5/29 vs 5/30). Multi-column sorting with updated code works exactly as expected!

Thanks!

@tonytomov
Copy link
Owner

Thanks for the feedback. Glad to hear that this work as expected. I will close the requirement.

Kind Regards,
Tony

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants