使用"sortGrid"来进行排序.方法以降序对列进行排序? [英] Using the "sortGrid" method to sort a column in descending order?

查看:273
本文介绍了使用"sortGrid"来进行排序.方法以降序对列进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对客户端进行排序的jqGrid,所以我使用的是 sortGrid 方法.但是,此方法仅接受两个参数:列名和是否应重新加载网格.

我想有条件地使用此方法对升序和降序进行排序..但是,由于排序顺序不是参数,因此方法默认为升序排序.

我目前有一种解决方法,如果列应按降序排序,我将两次调用 sortGrid 方法:

if (sortCol) {
    $("#gridID").sortGrid(sortCol);
    //If descending, need to apply the sort a 2nd time
    if (sortOrder === "desc") {
        $("gridID").sortGrid(sortCol);
    }
}  

至少在我的本地主机上测试时,这似乎可以解决问题.但是,当移至现场环境时,它似乎有时不会工作一次.

是否有更好的方法可以做到这一点?

解决方案

我遇到了同样的问题,sortGrid()不能按降序排列.

但是,问题不在于它不起作用,实际上是loadonce:true属性.

如果使用loadonce:true,则jqGrid在第一次从网格加载数据后将数据类型参数更改为"local".接下来的所有网格重新加载(排序,分页,过滤)都在本地进行.如果要再次从服务器刷新网格数据,则应将数据类型设置为其原始值("json"或"xml").

因此,我们需要克服一些困难,并在loadComplete之后执行sortGrid():

这是在加载网格后可以完美地选择页面和排序选项的代码.

    $('#myGrid').trigger("reloadGrid");

//SORTED_COLUMN is Global
var  DEFAULT_SORTED = false;
if (SORTED_COLUMN.index != undefined)
    DEFAULT_SORTED = true;

$("#myGrid").jqGrid({
    datatype : "local",
    autowidth : true,
    altclass : "jqgridAltRow",
    altRows :true,
    shrinkToFit :true,
    loadonce: true,
    data :data,
    editurl: 'clientArray', // to give a successful call after inline editing,
    page: CURRENT_PAGE,
    //method : 'post',
    cellEdit : 'true',
    cellsubmit : 'remote',
    beforeRequest: function () {
        responsive_jqgrid($(".ui-jqgrid"));
    },
    onSortCol: function (index, columnIndex, sortOrder) {
        SORTED_COLUMN.index = index;
        SORTED_COLUMN.columnIndex = columnIndex;
        SORTED_COLUMN.sortOrder = sortOrder;
    },
    loadComplete: function(){
        $.fn.jqm = undefined;



        setTimeout(function(){
            if(DEFAULT_SORTED){
                DEFAULT_SORTED = false;
                $('#myGrid').jqGrid('sortGrid', SORTED_COLUMN.index, false, SORTED_COLUMN.sortOrder);
                $('#myGrid').trigger('reloadGrid', [{page: CURRENT_PAGE}]);
            }
        }, 100);

        ...

if(DEFAULT_SORTED)此检查很重要,否则sortGrid()将以递归方式执行.

希望对您有所帮助,谢谢:)

I have a jqGrid that I'm sorting client-side, so I'm using the sortGrid method. However, this method only accepts two parameters: column name and if the grid should be reloaded.

I want to conditionally use this method to sort ascending AND descending. However, since sort order isn't a parameter, the method defaults to ascending sort.

I currently have a work-around where I call the sortGrid method twice if the column should be sorted in descending order:

if (sortCol) {
    $("#gridID").sortGrid(sortCol);
    //If descending, need to apply the sort a 2nd time
    if (sortOrder === "desc") {
        $("gridID").sortGrid(sortCol);
    }
}  

This seems to do the trick, at least when testing on my localhost. However, when moved to a live environment, it seems to not work every once in a while.

Is there a better way to do this?

解决方案

I had the same issue, sortGrid() doesn't work in descending order.

But, the issue is not that it's not working, it's actually the loadonce:true property.

If you use loadonce:true jqGrid change the datatype parameters to 'local' after the first load of data from the grid. All next grid reloading (sorting, paging, filtering) works local. If you want refresh the grid data from the server one more time you should set datatype to its original value ('json' or 'xml').

Hence, we need to be a little tricky to overcome this and execute the sortGrid() after loadComplete:

Here is the code which works perfectly to select the page and sort option after the grid is loaded.

    $('#myGrid').trigger("reloadGrid");

//SORTED_COLUMN is Global
var  DEFAULT_SORTED = false;
if (SORTED_COLUMN.index != undefined)
    DEFAULT_SORTED = true;

$("#myGrid").jqGrid({
    datatype : "local",
    autowidth : true,
    altclass : "jqgridAltRow",
    altRows :true,
    shrinkToFit :true,
    loadonce: true,
    data :data,
    editurl: 'clientArray', // to give a successful call after inline editing,
    page: CURRENT_PAGE,
    //method : 'post',
    cellEdit : 'true',
    cellsubmit : 'remote',
    beforeRequest: function () {
        responsive_jqgrid($(".ui-jqgrid"));
    },
    onSortCol: function (index, columnIndex, sortOrder) {
        SORTED_COLUMN.index = index;
        SORTED_COLUMN.columnIndex = columnIndex;
        SORTED_COLUMN.sortOrder = sortOrder;
    },
    loadComplete: function(){
        $.fn.jqm = undefined;



        setTimeout(function(){
            if(DEFAULT_SORTED){
                DEFAULT_SORTED = false;
                $('#myGrid').jqGrid('sortGrid', SORTED_COLUMN.index, false, SORTED_COLUMN.sortOrder);
                $('#myGrid').trigger('reloadGrid', [{page: CURRENT_PAGE}]);
            }
        }, 100);

        ...

if(DEFAULT_SORTED) This check is important, otherwise the sortGrid() will be executed in recursively.

Hope it help, thanks :)

这篇关于使用"sortGrid"来进行排序.方法以降序对列进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆