以下jqpivot网格显示汽车租赁的销售信息。这方面的完整代码在小提琴中
var data = [{
"id": 1,
"make": "toyota",
"model": "corolla",
"fuelusagecity": "17",
"fuelusagehwy": "12",
"fuelmeasure":'Litre',
"salesaboveavg": false,
"totalnumberofsales": 120000.0000,
"highsalestext": null,
"salesdate": "2010-12-01"
}, {
"id": 2,
"make": "toyota",
"model": "corolla",
"fuelusagecity": "10",
"fuelusagehwy": "14",
"salesaboveavg": false,
"fuelmeasure":'Litre',
"totalnumberofsales": 100000.0000,
"highsalestext": "HIGH",
"salesdate": "2010-12-15"
}, {
"id": 3,
"make": "toyota",
"model": "belta",
"fuelusagecity": "15",
"fuelusagehwy": "10",
"salesaboveavg": true,
"fuelmeasure":'Litre',
"totalnumberofsales": 200000.0000,
"highsalestext": null,
"salesdate": "2011-01-10"
}, {
"id": 4,
"make": "toyota",
"model": "camry",
"fuelusagecity": "13",
"fuelusagehwy": "10",
"fuelmeasure":'Litre',
"salesaboveavg": false,
"totalnumberofsales": 300000.0000,
"highsalestext": "HIGH",
"salesdate": "2011-04-23"
}, {
"id": 5,
"make": "nissan",
"model": "skyline",
"fuelusagecity": "14",
"fuelusagehwy": "9",
"fuelmeasure":'Litre',
"salesaboveavg": true,
"totalnumberofsales": 500000.0000,
"highsalestext": "HIGH",
"salesdate": "2010-09-10"
}, {
"id": 6,
"make": "nissan",
"model": "zx300",
"fuelusagecity": "10",
"fuelusagehwy": "8",
"fuelmeasure":'Litre',
"salesaboveavg": false,
"totalnumberofsales": 400000.0000,
"highsalestext": null,
"salesdate": "2012-01-06"
}];
/* convert the salesdate in */
var i, item, dateParts;
for (i = 0; i < data.length; i++) {
item = data[i];
if (typeof item.salesdate === "string") {
dateParts = item.salesdate.split("-");
item.salesYear = dateParts[0];
item.salesMonth = dateParts[1];
item.salesDay = dateParts[2];
item.salesDateFormatted = dateParts[0];
}
}
var myIntTemplate = {
formatter: "currency",
align: "right", sorttype: "number",
searchoptions: { sopt: ["eq", "ne", "lt", "le", "gt", "ge"] },
formatoptions: { defaultValue: ""}
},
$grid = $("#list483");
$grid.jqGrid("jqPivot",
data,
{
frozenStaticCols: true,
skipSortByX: true,
useColSpanStyle: true,
//defaultFormatting: false,
xDimension: [
{ dataName: "make", width: 100, label: "Make", compareVectorsEx(x1,x2){
// how do i use x1, x2 parameters to stop auto sort
} },
{ dataName: "model", width: 100, label: "Model", align: "center", skipGrouping:true, compareVectorsEx(x1,x2){
} },
{ dataName: "fuelmeasure", width: 103, label: "Units", compareVectorsEx(x1,x2){
} },
],
yDimension: [
{ dataName: "salesdate", sortorder: "desc"}//,
//{ dataName: "salesYear", sorttype: "integer" },
//{ dataName: "salesMonth", sorttype: "integer" }
],
aggregates: [{
member: "totalnumberofsales",
template: myIntTemplate,
formatter:function(cellvalue, options, rowObject){
if(cellvalue=== undefined){
return '';
}
else{
var x = options.rowData.pivotInfos[options.colModel.name].rows[0].highsalestext;
if(x==="HIGH")
{
return x;
}
else
{
return cellvalue;
}
}
},
cellattr: function (rowId, cellValue, rawObject, cm, rdata) {
if (rawObject != null) {
var items = rawObject.pivotInfos[cm.name];
if (items != null && items.rows != null && items.rows.length > 0) {
var isHigh = true, i;
for (i = 0; i < items.rows.length; i++) {
if (items.rows[i].highsalestext !== "HIGH") {
isHigh = false;
break;
}
}
if (isHigh) {
return "class='high-marker'";
}
}
}
},
aggregator: "max"
}/*,
{
member: "totalnumberofsales",
aggregator: "count",
//template: "integer",
label: "{0}"
}*/]
},
// grid options
{
iconSet: "fontAwesome",
cmTemplate: { autoResizable: true, width: 75 },
shrinkToFit: false,
useUnformattedDataForCellAttr: false,
autoResizing: { compact: true },
groupingView: {
groupField: ["x0"],
groupColumnShow: [false],
groupText: ["<span class='group-text'>{0}</span>"]
},
//width: 450,
pager: true,
rowNum: 20,
caption: "<b>Car sales statistics</b>",
rowList: [5, 10, 20, 100, "10000:All"]
}
); 根据这个wiki 抑制自动排序,它在wiki底部的官方wiki上声明:
按x或y向量进行的自定义排序 选项compareVectorsByX和compareVectorsByY允许指定回调函数,该函数将用于整个x或y向量的自定义排序。 在这里可以找到按向量排序的默认实现。这是ArrayOfFieldsets的ArrayOfFieldsets方法。重要的是要了解该函数将用于两个目的: 1)比较向量,2)在比较向量存在差异的情况下,找出向量的索引。因此,方法compareVectorsEx返回具有两个属性的对象:索引和结果。性质结果是众所周知的值-1,这意味着第一个向量小于第二个向量,0表示向量等于,1,这意味着第一个向量大于第二个向量。属性索引返回向量不同的比较向量的元素的0基索引。
如前所述,我添加了函数compareVectorsEx,但是如何使用该函数来停止自动排序呢?
我必须停止所有x字段的自动排序。我需要停止排序的原因是为了使网格显示字段、生成和建模,顺序与原来的json相同。
更新:
我修改了原始的json数据源,使其在每个对象groupheaderorder和childorder上都有两个属性。属性groupheaderorder是json中对象的属性make的顺序,childorder属性是的model属性的顺序,的所有名称都是。
这里是json数据
var data = [{
"id": 1,
"make": "toyota",
"model": "corolla",
"fuelusagecity": "17",
"fuelusagehwy": "12",
"fuelmeasure":'Litre',
"salesaboveavg": false,
"totalnumberofsales": 120000.0000,
"highsalestext": null,
"salesdate": "2010-12-01",
"groupheaderorder":"1",
"childorder":"1"
}, {
"id": 2,
"make": "toyota",
"model": "corolla",
"fuelusagecity": "10",
"fuelusagehwy": "14",
"salesaboveavg": false,
"fuelmeasure":'Litre',
"totalnumberofsales": 100000.0000,
"highsalestext": "HIGH",
"salesdate": "2010-12-15",
"groupheaderorder":"1",
"childorder":"1"
}, {
"id": 3,
"make": "toyota",
"model": "belta",
"fuelusagecity": "15",
"fuelusagehwy": "10",
"salesaboveavg": true,
"fuelmeasure":'Litre',
"totalnumberofsales": 200000.0000,
"highsalestext": null,
"salesdate": "2011-01-10",
"groupheaderorder":"1",
"childorder":"2"
}, {
"id": 4,
"make": "toyota",
"model": "camry",
"fuelusagecity": "13",
"fuelusagehwy": "10",
"fuelmeasure":'Litre',
"salesaboveavg": false,
"totalnumberofsales": 300000.0000,
"highsalestext": "HIGH",
"salesdate": "2011-04-23",
"groupheaderorder":"1",
"childorder":"3"
}, {
"id": 5,
"make": "nissan",
"model": "skyline",
"fuelusagecity": "14",
"fuelusagehwy": "9",
"fuelmeasure":'Litre',
"salesaboveavg": true,
"totalnumberofsales": 500000.0000,
"highsalestext": "HIGH",
"salesdate": "2010-09-10",
"groupheaderorder":"2",
"childorder":"1"
}, {
"id": 6,
"make": "nissan",
"model": "zx300",
"fuelusagecity": "10",
"fuelusagehwy": "8",
"fuelmeasure":'Litre',
"salesaboveavg": false,
"totalnumberofsales": 400000.0000,
"highsalestext": null,
"salesdate": "2012-01-06",
"groupheaderorder":"2",
"childorder":"2"
}];下面是指向jsfiddle代码的链接 (除了添加了新的两个属性外,这是与我最初文章相同的代码)
让我通过一个示例来解释这一点
在josn中,有三辆丰田车和两辆日产车。如果查看groupheaderorder数字和相同的制造和建模,它们具有相同的值,但是对于不同的制造和模型,它们的值都不同E 235。所以groupheaderorder和D37>的组合是E 138总是唯一的E 239,对于E 140不同的组合e 241。我认为这是一个很好的选择,可以为grouptext和他们的孩子提供自定义排序顺序,因为他们保留并显示了他们的原始排序顺序。
原始数据来自存储过程,我无法在存储过程代码中修改该存储过程,而且令人胆怯的是,返回的结果集甚至没有用于排序的列。所以我的web应用程序是从ASP.mvc构建的,在它的控制器逻辑中,我唯一的选择是添加这两种属性,以便将它们包含在json.中。
还有其他更好的方法,如果是这样的话,我真的很想知道。)
但是,既然我添加了这两个属性来保留原始的排序顺序,那么是否可以在生成的枢轴网格中保留和显示这个顺序呢?
发布于 2017-01-05 18:17:17
您应该对代码进行一些小的更改。首先,应该删除skipSortByX: true选项,而不需要更多的选项。秒,我们应该用xVector和childorder属性扩展groupheaderorder和childorder属性,这是排序所需要的。我们将不显示列,因此可以对列使用additionalProperty: true属性。因此,我们可以使用以下xDimension
xDimension: [
{ /*x0*/ dataName: "make", width: 100, label: "Make" },
{ /*x1*/ dataName: "model", width: 100, label: "Model", align: "center", skipGrouping:true },
{ /*x2*/ dataName: "fuelmeasure", width: 103, label: "Units", skipGrouping:true },
{ /*x3*/ dataName: "groupheaderorder", additionalProperty: true },
{ /*x4*/ dataName: "childorder", additionalProperty: true }
]我包括注释,它描述了属性的名称(索引):make属性将在x-向量中有索引0,model属性将有索引1,.childorder属性将具有索引4。
最终的jqGrid将具有属性x0、x1、.、x4的输入data,这些属性对应于列。为了使列" make“和"model”(x0,x1)基于groupheaderorder和childorder进行排序,我们可以向第一列添加sorttype回调。结果我们得到了
xDimension: [
{ /*x0*/ dataName: "make", width: 100, label: "Make",
sorttype: function (cellValue, item) {
return parseInt(item.x3, 10);
} },
{ /*x1*/ dataName: "model", width: 100, label: "Model", align: "center", skipGrouping:true,
sorttype: function (cellValue, item) {
return parseInt(item.x4, 10);
}},
{ /*x2*/ dataName: "fuelmeasure", width: 103, label: "Units", skipGrouping:true },
{ /*x3*/ dataName: "groupheaderorder", additionalProperty: true },
{ /*x4*/ dataName: "childorder", additionalProperty: true }
]最后,我们需要实现compareVectorsByX回调,它可以用来对数据进行X向量排序。回调compareVectorsByX有两个参数,即来自源数据的x项,例如x1 = ["toyota", "corolla", "Litre", "1", "1"]和2 = ["toyota", "belta", "Litre", "1", "2"]。回调应该返回具有两个属性的对象:index和result。结果是-1,0或1,每个人在比较数字时都知道这一点。index是x数组中的索引,在x数组中可以找到x1和x2之间的差异,并进行比较。在上述示例中,可以找到x数组的第二个元素中的差异(第二个元素具有索引1)。根据最后一个( "corolla" )值("childorder"):通过比较"1"和"2"来比较"1"和"2"。因此,compareVectorsByX回调应该返回上述选项的{ index: 1, result: -1 }。通用代码如下所示:
compareVectorsByX: function (x1, x2) {
// x1 = ["toyota", "corolla", "Litre", "1", "1"],
// x2 = ["toyota", "belta", "Litre", "1", "2"]
var groupheaderorder1 = parseInt(x1[3], 10), childorder1 = parseInt(x1[4], 10),
groupheaderorder2 = parseInt(x2[3], 10), childorder2 = parseInt(x2[4], 10);
if (groupheaderorder1 !== groupheaderorder2) {
return { index: 0, result: groupheaderorder1 - groupheaderorder2 };
}
if (childorder1 !== childorder2) {
return { index: 1, result: childorder1 - childorder2 };
}
return {
index: -1,
result: 0
};
}仅此而已。修改后的代码演示程序是https://jsfiddle.net/OlegKi/cukLs23n/2/
https://stackoverflow.com/questions/41467746
复制相似问题