JQGrid自定义排序

arb*_*arb 4 javascript jquery jqgrid

我有一个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) {

    }
});
Run Code Online (Sandbox Code Playgroud)

Ole*_*leg 8

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

我如何理解列'RoleLookupName','Entity'等中的数据可以包含重复项,因此您希望通过组合主排序列(例如'RoleLookupName')和第二列(' FullName'例如).如果主排序列中有重复项,则网格仍将按第二列中的第二个标准排序.要实现该行为,您应该使用自定义排序.您可以通过使用sorttypeas函数来实现它(请参阅答案).

sorttype功能的想法很简单.在sorttype应该使用应该返回字符串或整数,而不是 主细胞含有.例如,您可以将"RoleLookupName"定义如下

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

包括演示在内另一个答案你可能会发现也许有趣的理解.它演示了更高级的技术,它不仅实现了自定义排序,还实现了自定义搜索.