Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
831 views
in Technique[技术] by (71.8m points)

jquery - jqGrid dynamic select option

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:

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

and then in the beforeCellEdit event I have:

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.

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?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use the dataInit option, like this:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...