0

well.... first i need how change the colmodel whith a function that is call for a SELECT because my grid have periods and have a change with a select (cost and tons).

This is my GRID

        $("#grid").jqGrid({
                            .......
                            .......
                            url:'example1.json',
                            datatype: "json",
                            colNames: NAMESCOL,
                            colModel: MODEL,
                            .......
                            .......
                            caption:"Example 1"         
        });

where NAMESCOL and MODEL is:

var NAMESCOL = ["plant","town","name plant","Total","period1","period2","period3"];



var MODEL = [{name:'id_plant',index:'id_plant',width: 40},
          {name:'id_town',index:'id_town',width: 80},
              {name:'name_plant',index:'name_plant',width: 110},
              {name:'tot_ton',index:'tot_ton',width: 50},
              {name:'ton_per1',index:'ton_per1',width: 50},
              {name:'ton_per2',index:'ton_per2',width: 50},
              {name:'ton_per3',index:'ton_per3',width: 50}];

var MODEL2 = [{name:'id_plant',index:'id_plant',width: 40},
          {name:'id_town',index:'id_town',width: 80},
              {name:'name_plant',index:'name_plant',width: 110},
              {name:'tot_cost',index:'tot_ton',width: 50},
              {name:'cost_per1',index:'cost_per1',width: 50},
              {name:'cost_per2',index:'cost_per2',width: 50},
              {name:'cost_per3',index:'cost_per3',width: 50}];

and my function is:

 function CHANGE(){
           .......
           .......
           jQuery('#grid').jqGrid('clearGridData');
           jQuery('#grid').jqGrid('setGridParam', {colModel: MODEL2});
           jQuery('#grid').trigger('reloadGrid');       
 }

and try using jQuery('#grid').jqGrid('setGridParam', {data: MODEL2}); but only refreshes the same information.

:(

4

1 回答 1

0

您没有包括"JSON"从服务器返回的数据格式。数据的一个例子可以清楚很多事情。此外,了解其他一些参数,如datatype,loadoncejsonReader可能非常重要。无论如何,我可以给你一些建议,这将有助于解决你的问题。

首先,我建议您不要indexcolModel. 如果没有index指定属性,则 jqGridindex通过复制属性值来生成name属性。indexname属性的不同值,例如

{name:'tot_cost',index:'tot_ton',width: 50}

通常是一个错误。

name其次,items的property的值colModel会被用来构造idjqGrid内部结构的属性以及jqGrid使用的一些内部JavaScript对象的属性。属性的值name需要不同(唯一)。要从 JSON 输入中读取列的数据,jqGrid 使用jsonmap属性(有一些例外)。仅当jsonmap未定义属性时,colModelname使用属性而不是jsonmap.

因此,如果 jqGrid 的 JSON 输入(rows通常是部分)主要来自对象,例如

{"id_plant": ..., "id_town": ..., "name_plant": ..., "tot_ton": ..., ...}

您可以使用

jsonReader: {repeatitems: false}, // it can be skipped starting with jqGrid 4.4.5
colModel: [
    { name: "id_plant", width: 40 },
    { name: "id_town", width: 80 },
    { name: "name_plant", width: 110 },
    { name: "tot_ton" },
    { name: "ton_per1" },
    { name: "ton_per2" },
    { name: "ton_per3" }
]
cmTemplate: { width: 50 }, // default properties for colModel items
...

或者例如

jsonReader: {repeatitems: false}, // it can be skipped starting with jqGrid 4.4.5
colModel: [
    { name: "c1", jsonmap: "id_plant", width: 40 },
    { name: "c2", jsonmap: "id_town", width: 80 },
    { name: "c3", jsonmap: "name_plant", width: 110 },
    { name: "c4", jsonmap: "tot_ton" },
    { name: "c5", jsonmap: "ton_per1" },
    { name: "c6", jsonmap: "ton_per2" },
    { name: "c7", jsonmap: "ton_per3" }
]
cmTemplate: { width: 50 }, // default properties for colModel items
...

重要的是要了解更改name属性的值非常困难colModel(例如setColProp在回调中使用方法beforeProcessing),但是您可以随时更改属性的值jsonmap或将属性的值指定jsonmap为函数。例如以下列的定义tot_ton可以colModel解决您当前的问题:

{
    name: "tot_ton",
    jsonmap: function (obj) {
        return obj.tot_ton || obj.tot_cost;
    }
}

它尝试tot_ton从输入数据项中读取。如果 的 值tot_ton未定义(或null0),obj.tot_cost则将使用 的值。取决于数据的类型tot_ton(例如,如果它的值是整数,可以是0),可以更好地使用以下内容jsonmap

{
    name: "tot_ton",
    jsonmap: function (obj) {
        return obj.tot_ton !== undefined ? obj.tot_ton : obj.tot_cost;
    }
}

我建议您阅读这个这个答案,这些答案展示了如何根据从服务器返回的 JSON 数据调整一些网格选项。如果您在beforeProcessing回调中进行此类调整,则服务器响应的处理将使用新设置完成。以这种方式,您可以使 jqGrid 真正动态化。

于 2013-11-08T16:18:25.277 回答