【问题标题】:how to make sort icons visible in all column headers in jqgrid regardless on sort status无论排序状态如何,如何使排序图标在 jqgrid 的所有列标题中可见
【发布时间】:2012-01-16 20:36:04
【问题描述】:

jqGrid 列仅在列已排序时显示排序图标。

如何使排序图标在所有列中可见,以便用户有想法 可以单击列标题进行排序吗? 可能两个排序方向三角形都必须处于非活动状态。

Telerik radgrid 有这个:

http://www.telerik.com/community/forums/aspnet/grid/possible-to-show-sort-icon-regardless-sort-status.aspx

如何在 jqGrid 中实现这一点? 目前没有任何迹象表明列是可排序的。

更新

我尝试使用下面的 colmodel 从答案中解决。

问题:

  1. 对于窄和列排序图标不显示或部分显示。 列标题右侧有很大的空白区域。如何减少这个空白区域,以便列标题文本和排序图标可以出现在这个区域?

  2. 排序后,除已排序的列之外的所有列中的排序图标都将丢失。 如何持久化它们?

【问题讨论】:

    标签: jquery-ui jqgrid


    【解决方案1】:

    viewsortcols : [true,'vertical',true]

    【讨论】:

    【解决方案2】:

    我觉得这个想法不错,所以我创建了 the demo 来实现该行为:

    用代码实现this:

    var $grid = $("#list"), colModel;
    
    // create the grid
    $grid.jqGrid({
        // all typical jqGrid parameters
        onSortCol: function (index, idxcol, sortorder) {
            if (this.p.lastsort >= 0 && this.p.lastsort !== idxcol
                    && this.p.colModel[this.p.lastsort].sortable !== false) {
                // show the icons of last sorted column
                $(this.grid.headers[this.p.lastsort].el)
                    .find(">div.ui-jqgrid-sortable>span.s-ico").show();
            }
        }
    });
    
    // show sort icons of all sortable columns
    colModel = $grid.jqGrid('getGridParam', 'colModel');
    $('#gbox_' + $.jgrid.jqID($grid[0].id) +
        ' tr.ui-jqgrid-labels th.ui-th-column').each(function (i) {
        var cmi = colModel[i], colName = cmi.name;
    
        if (cmi.sortable !== false) {
            // show the sorting icons
            $(this).find('>div.ui-jqgrid-sortable>span.s-ico').show();
        } else if (!cmi.sortable && colName !== 'rn' && colName !== 'cb' && colName !== 'subgrid') {
            // change the mouse cursor on the columns which are non-sortable
            $(this).find('>div.ui-jqgrid-sortable').css({cursor: 'default'});
        }
    });
    

    更新:如果您需要在最紧凑的列中显示信息,您可以在列标题的 CSS 中进行一些自定义。例如,默认情况下,您在所有列标题中都有“居中”对齐。关于以下CSS

    .ui-jqgrid .ui-jqgrid-htable th.ui-th-column
    {
        text-align:left
    }
    .ui-jqgrid .ui-jqgrid-htable th.ui-th-column div.ui-jqgrid-sortable
    {
        margin-left:3px;margin-top:3px
    }
    

    您可以将其更改为左对齐。作为结果,您将拥有更紧凑的列标题外观:

    查看对应的demo。顺便说一下,我建议你测试一下列宽是否足够大,以便在 Webkit 浏览器(Google Chrome 或 Safari)中显示排序图标。

    【讨论】:

    • 非常感谢你。优秀的。我遇到了一些问题并更新了有关它们的问题。
    • @Andrus:我无法调试完整的代码以查看谁隐藏了列标题中的图标,但您可以让图标可见(我的回答中 // show sort icons of all sortable columns 之后的代码) beforeRequestloadComplete。我认为它应该可以解决问题。
    • @Andrus:我更新了一点代码对应the answer 以将光标也固定在未排序的列上。
    • @Andrus:条件this.p.lastsort >= 0 是必需的,因为在if 中将修改网格this.p.lastsort-st 列中的图标(并且只有该列)。我想将lastsort 设置为-1 是您自定义jqGrid 的一部分。不需要,因为如果sortname""(默认值),则不会进行排序。
    • @Andrus:谢谢!我可以重现问题并找到错误。我修复了the demo。问题是remapColumns 是用空数组myColumnsState.permutation 调用的。我只将if (isColState) 行更改为if (isColState && myColumnsState.permutation.length > 0) { 以调用remapColumns,前提是myColumnsState.permutation 不是空数组。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多