【问题标题】:jqGrid - hide specific column or row in pivotjqGrid - 隐藏枢轴中的特定列或行
【发布时间】:2015-07-02 10:18:56
【问题描述】:

如果我们要隐藏列(第7个月,范围:边框黑色)

或隐藏列(2015 年,边框绿色)

有没有我们可以使用的解决方案或 jqGrid 选项?

【问题讨论】:

    标签: jqgrid pivot free-jqgrid


    【解决方案1】:

    jqPivot 方法没有允许您直接隐藏某些列的特殊选项,但是可以使用beforeInitGrid 回调对colModel 进行任何修改网格将被创建之前。唯一的问题是:必须了解jqPivot 使用的列的确切名称转换才能编写beforeInitGrid 回调的正确代码。所以我首先描述了jqPivot的一些内部结构,然后beforeInitGrid回调的代码就很清楚了。我根据示例解释问题。我也建议大家阅读the wiki article,它提供了有关在free jqGrid 4.9.0 中实现的jqPivot 的更多信息。

    首先我必须提醒jqPivot 获取作为输入数据,这些数据将根据xDimensionyDimension 选项进行索引,然后计算具有相同x 和y 值的所有项目的聚合函数。聚合函数将由aggregates 参数指定。换句话说,jqPivot 是输入数据的“预处理器”。它分析数据并生成新的datacolModel,它们显示有关原始数据的更紧凑的信息。

    要实现您的要求,您需要了解jqPivot 将使用哪些列名来生成colModel。此外,需要了解如何获取列对应的y 值。

    例如我们有以下输入数据:

    var data = [{
            CategoryName: "Baby", ProductName: "Baby Oil",
            Price: "193.81", Quantity: "1",
            sellmonth: "7",  sellyear: "2011", week: "first"
        }, {
            CategoryName: "Mom",  ProductName: "Shampoo",
            Price: "93.81",  Quantity: "1",
            sellmonth: "12", sellyear: "2011", week: "first"
        }, {
            CategoryName: "none", ProductName: "beauty",
            Price: "93.81",  Quantity: "1",
            sellmonth: "12", sellyear: "2011", week: "second"
        }, {
            CategoryName: "none", ProductName: "beauty",
            Price: "93.81",  Quantity: "1",
            sellmonth: "12", sellyear: "2011", week: "third"
        }, {
            CategoryName: "none", ProductName: "Shampoo",
            Price: "105.37", Quantity: "2",
            sellmonth: "12", sellyear: "2011", week: "third"
        }, {
            CategoryName: "none", ProductName: "beauty",
            Price: "93.81",  Quantity: "1",
            sellmonth: "12", sellyear: "2015", week: "second"
        }];
    

    我们使用 jqPivot 选项

    $("#pvtCrewAttendance").jqGrid("jqPivot",
        data,
        {
            footerTotals: true,
            footerAggregator: "sum",
            totals: true,
            totalHeader: "Grand Total",
            totalText: "<span style='font-style: italic'>Grand {0} {1}</span>",
            xDimension: [
                { dataName: "CategoryName", label: "Category Name", sortorder: "desc" },
                { dataName: "ProductName", label: "Product Name", footerText: "Total:" }
            ],
            yDimension: [
                { dataName: "sellyear",  sorttype: "integer", totalHeader: "Total in {0}" },
                { dataName: "sellmonth", sorttype: "integer" }//,
                //{ dataName: "week" }
            ],
            aggregates: [
                { member: "Price",    aggregator: "sum", summaryType: "sum", label: "{1}" },
                { member: "Quantity", aggregator: "sum", summaryType: "sum", label: "{1}" }
            ]
        },
        {/* jqGrid options ...*/});
    

    生成的枢轴网格将显示在the demo

    上述选项表示输入 dat build x-values 的 CategoryNameProductName 属性的 qnique 值 - 网格的第一行。这是

    [["Baby", "Baby Oil"], ["Mom", "Shampoo"], ["none", "beauty"], ["none", "Shampoo"]]
    

    上面的数组是xIndex。同样,唯一的y-values 是

    [["2011", "7"], ["2011", "12"], ["2015", "12"]]
    

    这些值构建了colModel 的列。如果在某些yDimension 中使用totalHeadertotalHeadertotalTexttotals: true 属性,则将包括总和超过该组的其他列。在上面的示例中,使用totalHeader 表示dataName: "sellyear"。这意味着将在具有sellyear“2011”和“2015”的列的末尾插入另外两列同时包含aggregatesPriceQuantity 总和)的列。

    网格列的名字将是"x0""x1"(对应于xDimension 中的项目数)。然后是名称以y 开头和以a0a1 结尾的列(对应于aggregates 中的项目数)。最后两个“总计”列的名称为 "ta0""ta1"(对应于 aggregates 中的项目数)。如果aggregates 仅包含一个元素,则以yt 开头的列中将缺少后缀(结尾)a0a1。分组总列的名称以y 开头,中间有t,末尾有a(如y1t0a0)。我在上面的示例中包含了一个关于列名的示例

    我希望大家能看到我用红色写的列名。这是所有 14 列的name 值:x0x1y0a0y0a1y1a0y1a1y1t0a0y1t0a1y1t0a1、@987654534 、y2t0a0y2t0a1ta0ta1

    现在重要的是要提到jqPivot 包括xIndexyIndex 用于在内部构建数据透视表。确切地说,可以获取pivotOptions 参数jqGrid 并检查xIndex.itemsyIndex.items 属性。你会看到我上面包含的项目数组。

    现在终于有了足够的信息来理解the demo 中使用的以下代码,它隐藏了您询问的列:

    演示使用以下beforeInitGrid 隐藏了所需的列:

    beforeInitGrid: function () {
        var $self = $(this), p = $self.jqGrid("getGridParam"),
            yItems = p.pivotOptions.yIndex.items, matches, iy, y,
            colModel = p.colModel, i, cm, l = colModel.length, cmName;
    
        for (i = 0; i < l; i++) {
            cm = colModel[i];
            cmName = cm.name;
            if (cmName.charAt(0) === "y") { // x, y, t
                // aggregation column
                matches = /^([x|y])(\d+)(t(\d+))?(a)?(\d+)/.exec(cmName);
                if (matches !== null && matches.length > 1) {
                    // matches[2] - iy - index if y item
                    // matches[4] - undefined or total group index
                    // matches[6] - ia - aggregation index
                    iy = parseInt(matches[2], 10);
                    y = yItems[iy];
                    if (y != null && (y[0] === "2015" || (y[0] === "2011" && y[1] === "7"))) {
                        cm.hidden = true;
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 谢谢!!这正是我想要的。
    • @mrjumpy:不客气!在stackoverflow上格式化代码和图片更容易,我想更多人会在issues of free jqGrid中找到stackoverflow的信息@
    猜你喜欢
    • 2014-06-14
    • 2023-03-20
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    • 2016-05-22
    • 1970-01-01
    相关资源
    最近更新 更多