jqGrid动态选择选项

我正在使用下拉列创建一个jqgrid,我正在使用单元格编辑。 我需要下拉列的选项来动态更改,我已经尝试通过将列设置为:

{ name: "AccountLookup", index: "AccountLookup", width: 90, editable: true, resizable: true, edittype: "select", formatter: "select" }, 

然后在beforeCellEdit事件中,我有:

 beforeEditCell: function(id, name, val, iRow, iCol) { if(name=='AccountLookup') { var listdata = GetLookupValues(id, name); if (listdata == null) listdata = "1:1"; jQuery("#grid").setColProp(name, { editoptions: { value: listdata.toString()} }) } }, 

GetLookupValues只返回一个字符串,格式为“1:One; 2:Two”等。这样可以正常填充选项后点击一下 – 即我点击第1行中的AccountID,下拉列表为空,但是当我然后单击第3行中的AccountID,我在行1中单击设置的选项将显示在第3行中。 等等。 所以总是一次点击后面。

有没有其他方法可以实现我的需求? 显然,显示的下拉选项总是在变化,我需要在用户进入单元格进行编辑时加载它们。 也许我可以以某种方式获取beforeEditCell事件中的select控件并手动输入其值而不是使用setColProp调用? 如果是这样,我可以得到一个这样做的例子吗?

另一件事 – 如果下拉列表为空且用户未取消单元格编辑,则网格脚本会抛出错误。 我正在使用clientarray编辑,如果这有所作为。

您可以使用dataInit选项,如下所示:

 { name: "AccountLookup", index: "AccountLookup", width: 90, editable: true, resizable: true, edittype: "select", formatter: "select", editoptions: { dataInit: function( elem ) { $(elem).empty() .append("") .append(""); } } } 

只需用GetLookupValues()函数替换静态Apples和Oranges选项即可。