如果我们要隐藏列(第 7 个月,范围:边框黑色)
或隐藏列(2015 年,边框绿色)
有没有我们可以使用的解决方案或 jqGrid 选项?
jqPivot
方法没有允许您直接隐藏某些列的特殊选项,但是可以使用回调在创建网格之前beforeInitGrid
进行任何修改。唯一的问题是:必须了解用于编写正确回调代码的列的确切名称转换。所以我首先描述了一些内部结构,然后回调的代码将清晰易懂。我根据示例解释问题。我也建议大家阅读wiki 文章,该文章提供了有关在免费 jqGrid 4.9.0 中实现的更多信息。colModel
jqPivot
beforeInitGrid
jqPivot
beforeInitGrid
jqPivot
首先,我必须提醒jqPivot
get 作为输入数据,它将基于xDimension
和yDimension
选项进行索引,然后计算具有相同 x 和 y 值的所有项目的聚合函数。聚合函数将由aggregates
参数指定。换句话说jqPivot
,是输入数据的“预处理器”。它分析数据并生成新的data
并colModel
显示有关原始数据的更紧凑的信息。
要实现您的要求,需要了解哪些列名将用于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 ...*/});
生成的枢轴网格将显示在演示中:
上述选项表示输入 dat build -values 的 qnique 值CategoryName
和属性- 网格的第一行。它是ProductName
x
[["Baby", "Baby Oil"], ["Mom", "Shampoo"], ["none", "beauty"], ["none", "Shampoo"]]
上面的数组是xIndex
. 以同样的方式唯一y
值是
[["2011", "7"], ["2011", "12"], ["2015", "12"]]
这些值构建 的列colModel
。如果一个 use totalHeader
, totalHeader
, totalText
ortotals: true
某些属性,yDimension
则将包括具有该组总和的附加列。一个totalHeader
用于dataName: "sellyear"
上面的例子。这意味着将在具有“2011”和“2015”的列的末尾插入另外两列aggregates
(sum byPrice
和 sum by )。Quantity
sellyear
网格列的名字将是"x0"
和"x1"
(对应于 中的项目数xDimension
)。然后是名称以y
和结尾a0
的列a1
(对应于 中的项目数aggregates
)。最后两个“总计”列具有名称"ta0"
和"ta1"
(对应于 中的项目数aggregates
)。如果aggregates
仅包含后缀(结尾)的一个元素,a0
并且a1
将在以y
or开头的列中丢失t
。分组总计列的名称以y
have开头,t
中间和a
结尾(如y1t0a0
)。我在上面的示例中包含了一个关于列名的示例
我希望人们会看到我用红色写的列名。这是name
所有 14 列的值:x0
, x1
, y0a0
, y0a1
, y1a0
, y1a1
, y1t0a0
, y1t0a1
, y2a0
, y2a1
, y2t0a0
, y2t0a1
, ta0
, ta1
.
现在重要的是要提到其中包括jqPivot
用于构建数据透视表。确切地说是可以获取jqGrid的参数并查看属性。人们会看到我上面包含的一系列项目。xIndex
yIndex
pivotOptions
xIndex.items
yIndex.items
最后,现在有足够的信息来理解演示中使用的以下代码,该代码隐藏了您询问的列:
该演示使用以下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;
}
}
}
}
}