如何更新jqgrid中数据的值 [英] How to update value of data in jqgrid

查看:13
本文介绍了如何更新jqgrid中数据的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在加载时永久更新 jqgrid 中的单元格.我知道我可以使用 setCell 但这只会更新该页面的值.如果我没有明确地为单元格执行另一个 setCell 回到页面,则会显示旧值.我也试过 setRowData 但它似乎在做同样的事情.我正在使用 loadonce 因为我的方法是 1)加载数据 2)根据某些标准修改数据的一些值 3)显示修改后的值.由于我正在使用 loadonce 不应该有办法在此会话中永久修改单元格吗?

I'm trying to update a cell in jqgrid permanently upon loading. I know I can use setCell but that only updates the value for that page. If I come back to the page if I don't explicit perform another setCell for the cell the old value is shown. I've also tried setRowData but it appears to be doing the same thing. I'm using the loadonce as my approach is to 1) load the data 2) modify a few values of the data based on some criteria 3) show the modified values. As I'm using loadonce shouldn't there be a way to modify a cell permanently in this session?

更新:

将我的代码放入没有给出错误但未能遍历所有数据的代码:

Putting in my code that isn't giving an error but failing to iterate through all data:

var set = 0;

....

gridComplete: function(data){
    setData();
},

....

beforeRefresh: function(data){
    set = 0;
},

....

function setData(){

if(set == 1) return;
... //create hash up here
  var dataArray = jQuery("#grid").jqGrid('getGridParam', 'data');
  var j = 1;
  for (var rows in dataArray) {
    var key = dataArray[rows].name;
    dataArray[rows].level = hashTable[key];
    j++;
  }
  alert(j);
}

这不是循环遍历数组中本地加载的所有项目.例如,如果页面大小设置为 30,则 alert(j) 将返回 30,尽管我在本地加载了多少项目.但是,如果我刷新图表 j 是正确的数字.为什么 getGridParam 在每种情况下的行为都不同?

This is not cycling through all items in the array that are locally loaded. For example, if page size is set to 30, the alert(j) returns 30, despite how many items I have locally loaded. However, if I refresh the graph the j is the correct number. Why is the behavior of getGridParam different in each case?

推荐答案

如果你使用 loadonce: true 你应该知道 jqGrid 将保存本地数据的哪里.jqGrid 有两个选项:data_index.data 是项目数组,其中每个项目都具有 name 属性作为 colModel 列的 name 属性.如果您需要通过 id (rowid) 查找项目,您可以使用 _index[rowid]data 数组中具有 rowid 的项目.因此,要更改列 'myColumn' 中的数据,您应该执行以下操作:

If you use loadonce: true you should know where the local data will be hold by jqGrid. jqGrid has two options: data and _index. The data is array of items where every item has the name property as the name property of the columns from colModel. If you need find the item by id (rowid) you can use _index[rowid] to the the item with the rowid in the data array. So to change the data in the column 'myColumn' you should do the following:

// first change the cell in the visible part of grid
myGrid.jqGrid('setCell', rowid, 'myColumn', newValue);

// now change the internal local data
var dataArray = myGrid.jqGrid('getGridParam', 'data'),
    indexes = myGrid.jqGrid('getGridParam', '_index');
dataArray[indexes[rowid]].myColumn = newValue;

更新:您可以使用记录在案的 getLocalRow 改变本地数据的方法:

UPDATED: You can use documented getLocalRow method to change the local data:

// first change the cell in the visible part of grid
myGrid.jqGrid('setCell', rowid, 'myColumn', newValue);

// now change the internal local data
myGrid.jqGrid('getLocalRow', rowid).myColumn = newValue;

这篇关于如何更新jqgrid中数据的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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