jQuery 如何更新jqgrid中数据的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9145163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:23:55  来源:igfitidea点击:

How to update value of data in jqgrid

jqueryjqgrid

提问by WildBill

I'm trying to update a cell in jqgrid permanently upon loading. I know I can use setCellbut that only updates the value for that page. If I come back to the page if I don't explicit perform another setCellfor 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 loadonceas 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 loadonceshouldn't there be a way to modify a cell permanently in this session?

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

UPDATE:

更新:

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);
}

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?

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

回答by Oleg

If you use loadonce: trueyou should know wherethe local data will be hold by jqGrid. jqGrid has two options: dataand _index. The datais array of items where every item has the name property as the nameproperty 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 dataarray. So to change the data in the column 'myColumn'you should do the following:

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

// 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;

UPDATED: You can use documented getLocalRowmethod to change the local data:

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

// 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;

回答by fr4nk

For all who came here from google, here is an updated answer!

对于所有从谷歌来到这里的人,这里有一个更新的答案!

important is the index, which you can get by calling getInd-Method. Because rowID != index of localRowData. (eq. rowId: jqg204, Index: 5)

重要的是索引,您可以通过调用 getInd-Method 获取。因为 rowID != localRowData 的索引。(eq. rowId: jqg204, Index: 5)

EDIT:if set "key : true" in colmodel you can set your own rowId (diffrent from my example "jqg###", usually a PK from a database table)

编辑:如果在 colmodel 中设置“key : true”,您可以设置自己的 rowId(与我的示例“jqg###”不同,通常是数据库表中的 PK)

var ind = myGrid.getInd(rowId);
var localRowData  = myGrid.jqGrid('getLocalRow', ind);
localRowData.myColumn = newValue;

hope that helps!

希望有帮助!

回答by Alonzzo2

I had a similar issue:
I needed to update a row upon a certain user-action (irrelevant).
For that, I needed to know the exact row from the 'data' object, even after some filtering of the table.
So I found a way to identify which row was changed - I added a RowID property for each item of the 'data' object, at the 'loadComplete' event:

我有一个类似的问题:
我需要根据某个用户操作(不相关)更新一行。
为此,即使在对表进行了一些过滤之后,我也需要知道“数据”对象中的确切行。
所以我找到了一种方法来识别哪一行被更改 - 我在 'loadComplete' 事件中为 'data' 对象的每个项目添加了一个 RowID 属性:

loadComplete: function () {
            if (firstRun) {
                firstRun = false;
                var dataArray = $('#jqGrid').jqGrid('getGridParam', 'data');
                $(dataArray).each(function (index) {
                    dataArray[index].RowID = index + 1;
                });
            }
        }

And now, at the formatter code, I was able to access rData.RowID to identify the exact row on which the action was taken, and to get/set its 'data' representation

现在,在格式化程序代码中,我能够访问 rData.RowID 以识别执行操作的确切行,并获取/设置其“数据”表示