如何用函数更改ColModel,使用clearGridData,setGridParam和reloadGrid? JAVASCRIPT / HTML [英] How to Change the ColModel with a function, using clearGridData,setGridParam and reloadGrid? JAVASCRIPT/HTML

查看:215
本文介绍了如何用函数更改ColModel,使用clearGridData,setGridParam和reloadGrid? JAVASCRIPT / HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

well ....首先,我需要如何改变colmodel的whith函数,这个函数需要一个SELECT,因为我的网格有周期并且有一个select(cost和tons)的变化。



这是我的网格

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

其中NAMESCOL和MODEL为:

  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}];
$ b $ 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}];

我的功能是:

<$ p $ ('#grid')。jqGrid('')$ j $($'$'$) clearGridData');
jQuery('#grid')。jqGrid('setGridParam',{colModel:MODEL2});
jQuery('#grid')。trigger('reloadGrid');
}

并尝试使用jQuery('#grid')。jqGrid('setGridParam' ,{data:MODEL2});但只刷新相同的信息。



:(


解决方案

您不包括从服务器返回的JSON数据的格式,数据的一个例子可以清除很多事情,而且还有一些其他参数的知识,比如 datatype loadonce jsonReader 也是非常重要的。我可以给你一些建议,帮助解决你的问题。

如果没有 index 指定的属性,那么jqGrid使指数 colModel 通过复制名称属性的值,属性不同的值。 index 和< code $ name code $> $ <$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ',width:50}

通常是一个错误。



秒,t他将使用名称属性 colModel 项来构造 id 内部jqGrid结构的属性以及jqGrid使用的一些内部JavaScript对象的属性。 name 属性的值只需不同(唯一)。要从JSON输入读取列的数据,jqGrid使用 jsonmap 属性(有一些例外)。只有在 colModel 中没有定义 jsonmap 属性,那么名称 property用来代替 jsonmap

所以如果JQGrid输入的是jqGrid(部分通常)主要由像

这样的对象组成: $ b

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

您可以使用

  jsonReader:{repeatitems:false},//可以跳过从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},// colModel项目的默认属性
...

或例如

  jsonReader:{repeatitems:false}, //它可以跳过从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},// colModel项目的默认属性
...

重要的是要明白,改变 colModel 属性的 code>(例如在 beforeProcessing callback中使用 setColProp 方法),但是您可以随时更改值的 jsonmap 属性或将 jsonmap 属性的值指定为函数。例如, colModel 列的 tot_ton 的以下定义可以解决您当前的问题:

  {
名称:tot_ton,
jsonmap:function(obj){
return obj.tot_ton || obj.tot_cost;


它会尝试读取 tot_ton 来自输入数据项目。如果 tot_ton 的值未定义(或 null 0 >),那么将使用 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数据调整一些网格选项。如果您在处理回调之前在中进行调整,则服务器响应的处理将使用新设置完成。以这种方式,你可以使jqGrid真正动态。


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.

:(

解决方案

You don't included the format of "JSON" data returned from the server. An example of the data could clear many things. Moreover the knowledge of some other parameters like datatype, loadonce and jsonReader could be very important too. In any way I can give you some advises which should help to solve your problem.

First of all I recommend you don't specify any index property in the colModel. If no index property specified then jqGrid makes index property by copying of the value of the name property. Different values for index and name properties like

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

are typically an error.

Seconds, the value of name property of colModel items will be used to construct id attributes of internal jqGrid structures and the properties of some internal JavaScript objects used by jqGrid. The values of name property need be just different (unique). To read the data for the column from the JSON input jqGrid uses jsonmap property (with some exceptions). Only if jsonmap property is not defined in colModel then the name property is used instead of jsonmap.

So if JSON input of jqGrid (the rows part typically) consists mostly from objects like

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

you can use

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
...

or for example

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
...

It's important to understand that it's really difficult to change values of name properties of colModel (for example using setColProp method inside of beforeProcessing callback), but you can any time change the value of jsonmap property or specify the value of jsonmap property as a function. For example the following definition of the column tot_ton of colModel can solve your current problem:

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

It try to read tot_ton from the item of input data. If the value of tot_ton is undefined (or null or 0) then the value of obj.tot_cost will be used. Depend of the type of the data of the tot_ton (for example if it's value is integer which can be 0) one can better use the following jsonmap instead:

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

I would recommend you to read this and this answers which show how one can adjust some grid options based on the JSON data returned from the server. If you place such adjustment in beforeProcessing callback then the processing of the server response will be done using new settings. In the way you can make jqGrid really dynamical.

这篇关于如何用函数更改ColModel,使用clearGridData,setGridParam和reloadGrid? JAVASCRIPT / HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆