grin0048
grin0048

Reputation: 574

Modifying sort columns in jqGrid

I'm having some difficulty figuring out how to programatically modify the sort definition that is sent to the server when a user clicks on a column to sort it. I have added a onSortCol function to my grid configuration. In that function, I need to check whether the "Id" column is in any sort position other than the last position. If it is, it should be removed.

Here is what I have tried:

onSortCol: function (index, iCol, sortOrder) {
    var grid = $(this);
    var rawSorts = index.split(",");
    if (rawSorts.length > 1) {
        var idFieldIndex = -1;
        var processedSorts = [];
        for (i = 0; i < rawSorts.length; i++) {
            var currentSort = rawSorts[i].match(/[^ ]+/g);
            if (idFieldIndex === -1 && currentSort[0].toUpperCase() === "ID") {
                idFieldIndex = i;
            }
            processedSorts.push({
                field: currentSort[0],
                direction: currentSort[1] || sortOrder
            })
        }
        if (idFieldIndex !== -1) {
            processedSorts.splice(idFieldIndex, 1);
            for (i = 0; i < processedSorts.length; i++) {
                if (i + 1 < processedSorts.length) {
                    grid.sortGrid(processedSorts[i].field + " " + processedSorts[i].direction);
                }
                else {
                    grid.setGridParam("sortorder", processedSorts[i].direction);
                    grid.sortGrid(processedSorts[i].field + " ", true);
                }
            }
            return "stop";
        }
    }
}

Upvotes: 0

Views: 99

Answers (1)

Oleg
Oleg

Reputation: 221997

The most simple implementation seems to me the following: you don't use any sortname in the grid initially and you sort by Id on the server side if sidx is empty. It seems the only what you need to do to implement your requirements.

Upvotes: 1

Related Questions