【问题标题】:JQGrid Custom SortingJQGrid 自定义排序
【发布时间】:2011-12-16 13:39:05
【问题描述】:

我有一个 JQGrid,其中填充了正常工作的数据。默认排序功能按预期工作。但是,我想按单击的列和按名称列排序;每次。我认为onSortCol 是我应该开始的地方,但文档中没有太多关于如何对表格内容进行排序的内容。理想情况下,我希望不必编写自己的排序算法,只需以某种方式插入 JQGrid API。所有数据都在客户端上,如果可能的话,我希望避免访问服务器。

这是我用来创建网格的代码:

$jqGrid = $('#people_SelectedContacts').jqGrid({
    ajaxGridOptions: {
        type: "POST"
    },
    url: 'AJAX/GetContacts',
    datatype: "json",
    postData: JSON.stringify({ ID: $('#ID').val() }),
    loadonce: true,
    sortable: true,
    caption: "Selected Contacts",
    hidegrid: false,
    autowidth: true,
    rowNum: 10000,
    height: "100%",
    loadui: 'block',
    colNames: ['lecID', 'lrlID', 'mjID', 'Role', 'Name', 'Entity', 'Contact', 'D #', ''],
    colModel: [
        { name: 'LECID', hidden: true },
        { name: 'LRLID', hidden: true },
        { name: 'MJID', hidden: true },
        { name: 'RoleLookupName', index: 'RoleLookupName' },
        { name: 'FullName', index: 'FullName' },
        { name: 'Entity', index: 'Entity' },
        { name: 'ContactInformation', index: 'ContactInformation' },
        { name: 'DNumber', index: 'DNumber' },
        { name: 'Remove', sortable: false, width: 25 }
    ],
    jsonReader: {
        root: 'ReturnValues.Contacts',
        repeatitems: false
    },
    beforeProcessing: function (data, status, xhr) {
        if (!data.ReturnValues.Contacts) {
            data.ReturnValues.Contacts = new Array();
        }
        $.each(data.ReturnValues.Contacts, function (index, value) {
            value.Entity = FormatAddress(value);
            value.ContactInformation = FormatContact(value);
            value.DNumber = FormatDocket(value);
        });
    },
    gridComplete: function () {
        var ids = $jqGrid.jqGrid('getDataIDs');
        for (var i = 0; i < ids.length; i++) {
            removeButton = $('<span>').addClass('remove-contact jqui-button-fix');
            $jqGrid.jqGrid('setRowData', ids[i], { Remove: $('<div>').append(removeButton).html() });
        }
    },
    loadComplete: function (data) {

    },
    onSortCol: function (index, iCol, sortorder) {

    }
});

【问题讨论】:

  • 您使用哪个datatype?你用loadonce: true吗?如果您包含网格定义的代码会很好。

标签: javascript jquery jqgrid


【解决方案1】:

在您的网格中,您有 5 列可见且可排序:“RoleLookupName”、“FullName”、“Entity”、“ContactInformation”、“DNumber”。从服务器加载网格数据后,数据类型将从'json' 更改为'local',对应于参数loadonce: true 的行为。从现在开始,排序将在本地进行。因为您没有在任何列中定义 sorttype 属性,所以将使用默认的 sorttype: 'text'

我如何理解“RoleLookupName”、“Entity”等列中的数据可能包含重复项,因此您希望通过主排序列(例如“RoleLookupName”)和第二列的组合对网格进行排序列(例如“全名”)。如果主排序列中有重复项,则网格仍将按第二列中的第二个条件进行排序。要实现该行为,您应该使用自定义排序。您可以通过使用sorttype 作为函数来实现它(参见the answer)。

sorttype 作为函数的想法很简单。 sorttype 应该返回字符串或整数,应该使用 而不是 主单元格包含。例如,您可以将“RoleLookupName”定义如下

{ name: 'RoleLookupName', index: 'RoleLookupName',
    sorttype: function (cell, obj) {
        return cell + '_' + obj.FullName;
    }}

Another answer,其中包括the demo,您可能会觉得理解起来也很有趣。它展示了更高级的技术,不仅实现了自定义排序,还实现了自定义搜索。

【讨论】:

  • 不,我还没有。我确实知道将sorttype 定义为一个函数。我曾希望我能够使用onSortCol 回调来做到这一点。但是,如果按照您建议的方式去做是唯一的方法,我想我只需要去做。感谢您提供我确信会有所帮助的代码。我希望今天晚些时候能解决这个问题,当它有效时,我一定会标记你的答案。
  • @Zero21xxx:不客气! onSortCol 有助于拒绝排序或在单击列标题时执行一些其他操作。要定义数据应如何排序,以便定义某些自定义排序行为,只能使用 sorttype 作为函数或 index 作为函数。 datatype: 'local' 的最佳选择是 sorttype 作为函数。在the answer 中,我在回答您的问题时引用了它,您将找到有关该主题的讨论链接。
  • @Zero21xxx:顺便说一下,如果你打算奖励赏金,你必须明确地这样做。请参阅“我如何奖励赏金?”在the answer.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-17
  • 2011-03-20
  • 1970-01-01
  • 2014-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多