开发者

Array (json) not passing into jqGrid's editoptions parameters

I've been trying to pass an array from a global variable (codata) to an option array of editoptions (jqGrid). My code stands as follows:

--------- countries_list.php throws the following json array -----------

["ABU","AD","AE","AF" .... "ZA","ZM","ZW"]

--------- PHP script with jqGrid Code ----------

jQuery(document).ready(function(){

            var codata = new Array();

            $.getJSON('countries_list.php', function(list){
                $.each(list, function(val) {
                    codata.push("'"+val+"'");
                    # --- Here alert() displays 'codata' with all the elements ---
                });
            });

            $("#datatable").jqGrid({
                 ......
                 // some code until colMode specs
                 ......
                 {   name:'guco',
                        index:'guco',
                        edittype:'select',
                        width:90,
                        editable: true,
                        editoptions: {
                            formatter:'select',
                            value: codata # --- array is not passed, it comes empty ---
                        },
                        sortable: true,
                        resizable: false
                    },
        .....

--------- PHP script with jqGrid Code ----------

Any hint how to 开发者_JS百科get this fixed?, thanx in advance.

Mario Benitez.-


Thanx a lot to all you guys, I learnt a lot with your contributions. Problem was fixed as follows:

(reading about) I found that getjson works an 'async mode' (I'm a jQuery newbie) and the code to fix the problem was:

    jQuery(document).ready(function(){

        var codata = (function () {
            var list = null;

            $.ajax({
                'async': false,
                'global': false,
                'url': 'countries_list.php',
                'dataType': 'json',
                'success': function (data) {
                    list = data;
                }
            });

            return list;
        })();

        $("#datatable").jqGrid({
             ... jqGrid settings ...
            colModel: [
                ....
                {   name:'guco',
                    index:'guco',
                    edittype:'select',
                    width:90,
                    editable: true,
                    editoptions: {
                        value: codata
                    },
                    sortable: true,
                    resizable: false
                },
                ....

Thanx a lot once again, I hope this helps to someone else.

Mario Benitez.


From what i see your php script is passing valid js array, not json object. The most logic thing to do is to call jqGrid inside the ajax callback assigning $("#datatable").jqGrid({ ... editoptions: { value: list } ... });


I would recommend you to use dataUrl together with buildSelect instead of value of the editoptions. You can find the corresponding code example of buildSelect in the "UPDATED" part of the answer.


I tried both ways and the Answer given in Olegs links helped. Using the buildSelect param allows full control of the value. When I used value method it assigned simple integer values of 0 - n. You can also assign your own CSS classes in this paradigm too. +1 for buildSelect/dataurl.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜