jQuery jqGrid 动态选择选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1291290/
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-26 11:07:48  来源:igfitidea点击:

jqGrid dynamic select option

jquerydynamicjquery-pluginsjqgrid

提问by

I'm creating a jqgrid with drop down columns and I'm using cell editing. I need the options of the drop down columns to change dynamically and I've tried implementing this by setting the column to be:

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

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

and then in the beforeCellEdit event I have:

然后在 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 just returns a string in the format "1:One;2:Two" etc. That works fine however the options are populated one click behind - ie i click on AccountID in row 1, and the dropdown is empty, however when I then click on AccountID in row 3 the options I set in the row 1 click are shown in the row 3 click. And so on. So always one click behind.

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

Is there another way of achieving what I need? Bacially the dropdown options displayed are always changing and I need to load them as user enters the cell for editing. Perhaps I can somehow get at the select control in the beforeEditCell event and manually enter its values instead of using the setColProp call? If so could I get an example of doing that please?

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

Another thing - if the dropdown is empty and a user doesn't cancel the cell edit, the grid script throws an error. I'm using clientarray editing if that makes a difference.

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

回答by MikeWyatt

You could use the dataInit option, like this:

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

{ name: "AccountLookup", index: "AccountLookup", width: 90,
    editable: true, resizable: true, edittype: "select", formatter: "select",
    editoptions: { dataInit: function( elem )
    {
        $(elem).empty()
            .append("<option value='1'>Apples</option>")
            .append("<option value='2'>Oranges</option>");
    } } }

Just replace the static Apples & Oranges options with your GetLookupValues() function.

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