如何在列标题上存储N个值?

时间:2017-01-14 01:44:17

标签: jquery jqgrid free-jqgrid

我有以下jsfiddle在pivot中生成一个jqgrid。 jsfiddle code 它使用以下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", 
    "shortsalesdate": "10-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",
    "shortsalesdate": "10-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",
    "shortsalesdate": "11-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",
    "shortsalesdate": "11-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",
    "shortsalesdate": "10-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",
    "shortsalesdate": "12-01-06",
    "groupheaderorder":"2",
    "childorder":"2"
}];

在json数据中,销售日期有两种格式salesdate属性的年份为四位数,shortsalesdate的年份为两位数。在网格动态销售日期列中,必须以yy-mm-dd格式显示销售日期(因此要显示它应使用shortsalesdate属性)。

在小提琴代码中,日期按降序排序。新修改还必须按降序排序日期。这次它应该使用属性salesdate中的销售日期进行排序,但它应该使用列标题上的属性' shortsalesdate`显示销售日期。

其次,有两个按钮来显示和隐藏列。在隐藏/显示之前,方法getYColumnName中的销售日期进行了比较。在当前的jsfiddle代码中,它使用salesdate属性中的值进行比较。这应该保持不变。

所以基本上有两件事: 1.要显示销售日期,应使用属性shortsalesdate中的值 2.对于所有日期的比较和排序,它必须使用属性salesdate

中的值

我如何实现这一目标?

旁注: 我正在考虑在列标题中存储两个值,其中一个隐藏值来自属性shortsalesdate,一个可见值来自属性shortsalesdate。那么基本上可以在列中存储两个值吗?

感谢

1 个答案:

答案 0 :(得分:1)

我可以建议你如何在当前版本的免费jqGrid版本4.13.6中实现你的要求,但我认为你的问题的解决方案应该很容易。

您要做的只是自定义文本,该文本将显示在列标题中。因此我在free jqGrid的代码中做了一些更改(见这里)并将结果发布到GitHub。

The demo从GitHub加载最新代码,并使用

yDimension: [
    { dataName: "salesdate", sortorder: "desc",
        label: function (options) {
            // options has the following properties:
            //    yData, yIndex, yLevel, pivotOptions
            var date = String(options.yData).split("-");
            return date[0].substr(2) + "-" + date[1] + "-" + date[2];
        }}
]

新的label回调允许根据数据构造列标题。我将日期分割为-,从年份中删除前两个符号并构建新字符串。人们可以看到如下图所示的结果

enter image description here

我的演示版不使用shortsalesdate属性,只使用原始salesdate

yDimension中使用多个元素时,同样适用。演示https://jsfiddle.net/OlegKi/e1rvyczh/3/使用

yDimension: [
    { dataName: "salesYear", sorttype: "integer", sortorder: "desc",
        label: function (options) {
            return "(" + options.yData + ")";
        }},
    { dataName: "salesdate", sortorder: "desc",
        label: function (options) {
            // options has the following properties:
            //    yData, yIndex, yLevel, pivotOptions
            var date = String(options.yData).split("-");
            return date[0].substr(2) + "-" + date[1] + "-" + date[2];
        }}
]

年份的虚拟格式:我只是使用(将其括在大括号中)return "(" + options.yData + ")";。结果如下所示

enter image description here

GitHub(来自here

刷新免费jqGrid的来源非常重要
相关问题