6

Sorry I can't post images, I'm too new.

In jqGrid add/edit dialogs I would like to load a list of selectable items based on a selection made earlier. In the picture above, the value selection should be loaded based on the value chosen in the criteria selection. I believe the route to go is using the dataurl in the editoptions object but I am having issues in that regards. The first issue that was troubling was based on the documentation here it doesn't seem like there is an event available to fire when the value of criteria changes that will allow me to update the values list.

Also I'm confused on how the data should be returned from the ajax request. In the documentation it says :

Setting the editoptions dataUrl parameter The editoptions dataUrl parameter is valid only for element of edittype:select. The dataUrl parameter represent the url from where the html select element should be get. When this option is set, the element will be filled with values from the AJAX request. The data should be a valid HTML select element with the desired options"

does this mean I will need to generate the html and return this as part of the response? Previously I had been passing all of my data using json.

Jess Stone
  • 677
  • 8
  • 21
GargantuanTezMaximus
  • 1,044
  • 1
  • 12
  • 19
  • On second thought I suppose I could use jQuery to add an onchange event to that selection. But I guess I am at a loss on how to select that element and then to get it to fire immediately (postback) to update the values list everytime the criteria is changed. – GargantuanTezMaximus Jun 27 '11 at 15:29

1 Answers1

7

jqGrid has no simple support of dependent selects in the editoptions. So to implement is one have to use change event on the main select to manually update the list of options of the second (dependent) select.

In the demo you will find how you can implement dependent selects. I used in the demo 'local' datatype and so set value property of the editoptions instead of dataUrl, but the main schema what should be done stay the same. Moreover in the demo I use not only form editing, but inline editing too. The code work in both cases. Because jqGrid don't support local editing in the form editing mode, the submitting of the forms not work. I could of cause use the tricks which I described here, but the code will be much longer and will contain many things which are far from your main question. So I decided to post the code in the form where submitting is not work.

Below you find the code from the demo:

var countries = { '1': 'US', '2': 'UK' },
    states = { '1': 'Alabama', '2': 'California', '3': 'Florida', '4': 'Hawaii', '5': 'London', '6': 'Oxford' },
    statesOfUS = { '1': 'Alabama', '2': 'California', '3': 'Florida', '4': 'Hawaii' },
    statesOfUK = { '5': 'London', '6': 'Oxford' },
    // the next maps contries by ids to states
    statesOfCountry = { '1': statesOfUS, '2': statesOfUK },
    mydata = [
        { id: '0', Country: '1', State: '1', Name: "Louise Fletcher" },
        { id: '1', Country: '1', State: '3', Name: "Jim Morrison" },
        { id: '2', Country: '2', State: '5', Name: "Sherlock Holmes" },
        { id: '3', Country: '2', State: '6', Name: "Oscar Wilde" }
    ],
    lastSel = -1,
    grid = $("#list"),
    resetStatesValues = function () {
        // set 'value' property of the editoptions to initial state
        grid.jqGrid('setColProp', 'State', { editoptions: { value: states} });
    };

grid.jqGrid({
    data: mydata,
    datatype: 'local',
    colModel: [
        { name: 'Name', width: 200 },
        {
            name: 'Country',
            width: 100,
            editable: true,
            formatter: 'select',
            edittype: 'select',
            editoptions: {
                value: countries,
                dataInit: function (elem) {
                    var v = $(elem).val();
                    // to have short list of options which corresponds to the country
                    // from the row we have to change temporary the column property
                    grid.jqGrid('setColProp', 'State', { editoptions: { value: statesOfCountry[v]} });
                },
                dataEvents: [
                    {
                        type: 'change',
                        fn: function (e) {
                            // build 'State' options based on the selected 'Country' value
                            var v = $(e.target).val(),
                                sc = statesOfCountry[v],
                                newOptions = '',
                                stateId,
                                form,
                                row;
                            for (stateId in sc) {
                                if (sc.hasOwnProperty(stateId)) {
                                    newOptions += '<option role="option" value="' + stateId + '">' +
                                        states[stateId] + '</option>';
                                }
                            }

                            resetStatesValues();

                            // populate the subset of contries
                            if ($(e.target).is('.FormElement')) {
                                // form editing
                                form = $(e.target).closest('form.FormGrid');
                                $("select#State.FormElement", form[0]).html(newOptions);
                            } else {
                                // inline editing
                                row = $(e.target).closest('tr.jqgrow');
                                $("select#" + $.jgrid.jqID(row.attr('id')) + "_State", row[0]).html(newOptions);
                            }
                        }
                    }
                ]
            }
        },
        {
            name: 'State',
            width: 100,
            editable: true,
            formatter: 'select',
            edittype: 'select',
            editoptions: { value: states }
        }
    ],
    onSelectRow: function (id) {
        if (id && id !== lastSel) {
            if (lastSel !== -1) {
                resetStatesValues();
                grid.jqGrid('restoreRow', lastSel);
            }
            lastSel = id;
        }
    },
    ondblClickRow: function (id) {
        if (id && id !== lastSel) {
            grid.jqGrid('restoreRow', lastSel);
            lastSel = id;
        }
        resetStatesValues();
        grid.jqGrid('editRow', id, true, null, null, 'clientArray', null,
            function () {  // aftersavefunc
                resetStatesValues();
            });
        return;
    },
    editurl: 'clientArray',
    sortname: 'Name',
    ignoreCase: true,
    height: '100%',
    viewrecords: true,
    rownumbers: true,
    sortorder: "desc",
    pager: '#pager',
    caption: "Demonstrate dependend select/dropdown lists (edit on double-click)"
}).jqGrid('navGrid', '#pager', { edit: true, add: true, del: false, search: false, refresh: true },
    { // edit options
        recreateForm: true,
        viewPagerButtons: false,
        onClose: function () {
            resetStatesValues();
        }
    },
    { // add options
        recreateForm: true,
        viewPagerButtons: false,
        onClose: function () {
            resetStatesValues();
        }
    });

UPDATED: See "UPDATED 2" part of the answer for the most recent version on the demo.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Hi Oleg, how can I set an event handler on click when im using this code to generate a drop down list on add/edit: $grid->setSelect("title", "SELECT DISTINCT name,name as TestingName FROM template", true, true, false, array(""=>"All")); – Grace Nov 09 '11 at 13:05
  • @Grace: Hi! I know no `$grid->setSelect` method and selecting twice the same column `name` from the `template` seems me strange too. Now about your main question: "how can I set an event handler on click". Where (on which control) you want set `click` handle? – Oleg Nov 09 '11 at 14:55
  • i have a field name "title" on my grid. on add/edit this field transforms to drop down list, on change of this drop down list i want to put an event listener, im doing this for the drop down list:$grid->setSelect("title", "SELECT DISTINCT name,name as TestingName FROM template", true, true, false, array(""=>"All"));(works fine), and this for the event listener $grid->setColProperty('title',array("editoptions"=>array("dataEvents"=>array("type"=>"keypress", "fn"=>"js: function(){alert(1);}")))); (doesn't work) on click i want to how another grid or custom dialog, but for now m going with alert – Grace Nov 09 '11 at 15:05
  • @Grace: I don't use PHP myself and don't know jqGrid for PHP. So it's difficult for me to answer whether your use correct syntax or not. The `editoptions` can has `dataEvents: [{type: 'keypress', fn: function () {alert("Hi!");}}]` and it's work. Moreover the current answer where you write comment is about `dataUrl` mostly. Probably it would be better if you ask new question where you describe the problem which you has? In the case you should better try to formulate all in JavaScript to increase the number of people who could help you. – Oleg Nov 09 '11 at 15:42
  • yes i think ill make all my codes with javascript, thanks alot !! – Grace Nov 09 '11 at 15:44
  • @Oleg: When using dataUrl can I do this without any ajax Request? – Mir Gulam Sarwar Dec 02 '13 at 11:23
  • @janina: Sorry, but I don't understand what you mean. Could you specify your problem more detailed? What you do exactly? – Oleg Dec 02 '13 at 18:10
  • I have two dataUrl for two dropdown.Can I filter the second dropdown with the selected value from the first one in client side without requesting anything from the server? Since data for both dropdown is already there.I just want to filter the Second one.I saw your example with local data.But I don't understand how to do it with Server side data which has dataUrl – Mir Gulam Sarwar Dec 02 '13 at 19:34
  • @janina: In "UPDATED 3" part of [the answer](http://stackoverflow.com/a/4480184/315935) I shown how dependent selects can be interpreted. The main stop of the solution is **manual** setting of content of dependent `` based on the option chosen in the first ` – Oleg Dec 02 '13 at 20:23
  • what should i use instead of this grid.jqGrid('setColProp', 'state', { editoptions: { value: statesOfCountry[countryId]} }); when I have dataUrl. Above line is in setStateValues() function – Mir Gulam Sarwar Dec 03 '13 at 05:28