【问题标题】:jqGrid how to apply extra classes to header columnsjqGrid如何将额外的类应用于标题列
【发布时间】:2013-11-27 07:32:25
【问题描述】:

我想在特定列上应用一个额外的类,我知道通过在 colModel 中指定它可以用于行。但这些类仅适用于“结果行”中的列,而不适用于标题。

我想要达到的是通过一个简单的类名(用于 Twitter Bootstrap)隐藏较小视口的特定列。

【问题讨论】:

    标签: javascript class jqgrid header


    【解决方案1】:

    似乎将类属性应用于整个列(包括标题行)的唯一方法是自己将 colModel 类条目应用于标题。正如您所提到的,将值放入 colModel 已经将其应用于数据行,但会保持标题不变。

    幸运的是,您可以进行设置,以便您应用到 colModel 规范的任何类都将使用单个函数调用自动应用到适当的标题列。

    下面是一个例子:

        //Takes css classes assigned to each column in the jqGrid colModel 
        //and applies them to the associated header.
        var applyClassesToHeaders = function (grid) {
            // Use the passed in grid as context, 
            // in case we have more than one table on the page.
            var trHead = jQuery("thead:first tr", grid.hdiv);
            var colModel = grid.getGridParam("colModel");
    
            for (var iCol = 0; iCol < colModel.length; iCol++) {
                var columnInfo = colModel[iCol];
                if (columnInfo.class) {
                    var headDiv = jQuery("th:eq(" + iCol + ") div", trHead);
                    headDiv.addClass(columnInfo.class);
                }
            }
        };
    
        //Example grid configuration just to illustrate
        var grid = jQuery('#list');
        grid.jqGrid({
            data: myData,
            datatype: 'local',
            caption: 'Order Details',
            height: 'auto',
            gridview: true,
            rownumbers: true,
            viewrecords: true,
            pager: '#pager',
            rownumbers: true,
            colNames: ['Order ID', 'Order', 'Shipment', 'Details', 'Status'],
            colModel: [
                { name: 'orderID', index: 'orderID', key:true, width: 50, 
    sorttype: 'int', class: 'alwaysShow' },
                { name: 'orderDate', index: 'orderDate', width: 120, 
    sorttype: 'date', formatter: 'date', class: 'alwaysShow' },
                { name: 'shipmentDate', index: 'shipmentDate', width: 120, 
    sorttype: 'date', formatter: 'date', class: 'alwaysShow' },
                { name: 'productDetails', index: 'productDetails', width: 250, 
    sorttype: 'string', formatter: 'string', class: 'sometimesShow'  },
                { name: 'orderStatus', index: 'orderStatus', width: 50, hidden: true, 
    class: 'neverShow' }
            ]
        });
    
        //Applies the classes to the headers once the grid configuration is complete.
        applyClassesToHeaders(grid);
    

    请注意,此方法会将 class 属性应用于 TH 中包含的 div。如果需要申请整个 TH,请使用“th:eq(” + iCol + “)”,而不是“th:eq(” + iCol + “) div”。

    感谢 Oleg 提供了一个很棒的先前答案,其中包含一个很好的方法来解析 jqGrid 表头。很高兴不必摆弄以使其正常工作。 https://stackoverflow.com/a/3979490/2548115

    【讨论】:

    • grid.getGridParam("colModel") 可能是grid.jqGrid("getGridParam", "colModel")
    【解决方案2】:

    上面的答案几乎可以工作,但需要一些调整。 columnInfo.class 应该是 columnInfo.classes

        //Takes css classes assigned to each column in the jqGrid colModel 
        //and applies them to the associated header.
        var applyClassesToHeaders = function (grid) {
            // Use the passed in grid as context, 
            // in case we have more than one table on the page.
            var trHead = jQuery("thead:first tr", grid.hdiv);
            var colModel = grid.getGridParam("colModel");
    
            for (var iCol = 0; iCol < colModel.length; iCol++) {
                var columnInfo = colModel[iCol];
                if (columnInfo.classes) {
                    var headDiv = jQuery("th:eq(" + iCol + ")", trHead);
                    headDiv.addClass(columnInfo.classes);
                }
            }
        };
    
        //Example grid configuration just to illustrate
        var grid = jQuery('#list');
        grid.jqGrid({
            data: myData,
            datatype: 'local',
            caption: 'Order Details',
            height: 'auto',
            gridview: true,
            rownumbers: true,
            viewrecords: true,
            pager: '#pager',
            rownumbers: true,
            colNames: ['Order ID', 'Order', 'Shipment', 'Details', 'Status'],
            colModel: [
                { name: 'orderID', index: 'orderID', key:true, width: 50, 
    sorttype: 'int', classes: 'hidden-xs' },
                { name: 'orderDate', index: 'orderDate', width: 120, 
    sorttype: 'date', formatter: 'date' },
                { name: 'shipmentDate', index: 'shipmentDate', width: 120, 
    sorttype: 'date', formatter: 'date' },
                { name: 'productDetails', index: 'productDetails', width: 250, 
    sorttype: 'string', formatter: 'string', classes: 'hidden-xs'  },
                { name: 'orderStatus', index: 'orderStatus', width: 50, hidden: true, 
    classes: 'hidden-xs' }
            ]
        });
    
        //Applies the classes to the headers once the grid configuration is complete.
        applyClassesToHeaders(grid);
    

    【讨论】:

      【解决方案3】:

      您可以将headercss 的属性添加到您正在使用的特定字段或列中

      例如在字段案例中

      fields: [
          { name: "age", type: "number", headercss: "assignment_role" },
          { type: "control" }
      ],  
      

      所以现在“assignment_role”将出现在年龄字段的标题中

      来源:JSGrid field documentation

      【讨论】:

        猜你喜欢
        • 2021-11-19
        • 2020-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多