javascriptjqgrideditmode

jqgrid cascading dropdown lists in pop up edit mode


I have two drop down lists, the first one gets populated from the database values and the second one(PriceCode) depends on what the user selects in the first one(CurrCd). My question is how do I populate that second list dynamically? I am trying to do it in dataEvents but have no way of referencing the second drop down list in edit mode. Is there a correct and logical way of doing so?

Here is my code so far:

 grid.jqGrid({
            datatype: 'local',
            jsonReader: als.common.jqgrid.jsonReader('EntityCd'),
            mtype: 'POST',
            pager: '#billingPager',
            loadonce: true,
            colNames: ['Currency', 'Status', 'Bill Curr', 'Price Code'],
            colModel: [
                {
                               { name: 'DefaultCurr', index: 'DefaultCurr', width: 20, sortable: false },
                { name: 'BillStatus', index: 'BillStatus', width: 13, sortable: false },
                {
                    name: 'CurrCd', index: 'CurrCd', width: 20, sortable: false,
                    editable: true,
                    edittype: 'select', stype: 'select',
                    editoptions: {
                        dataUrl: als.common.getServerPath() + 'LegalEntitiesAjax/GetCurrencies',
                        dataEvents:[{
                            type:"change",
                            fn: function (e) {

                                //populate price code list here                           
                                //below does not work to populate PriceCode

                                $.ajax({
                                    type: 'POST',
                                    traditional: true,
                                    contentType: 'application/json; charset=utf-8',
                                    url: als.common.getServerPath() + 'LegalEntitiesAjax/GetPriceList',
                                    data: JSON.stringify({ curcd: this.value }),
                                    async: false,
                                    success: function (data) {                                       

                                        for(i=0; i < data.length; i++) {
                                            $('select#PriceCode').append('<option val=\"' + data[i].PriceCode + '">' + data[i].Description + '</option>');
                                        }


                                    },
                                    error: function (val) { }
                                });


                            }
                        }],

                        buildSelect: function (data) {
                            var currSelector = $("<select id='selCurr' />");
                            $(currSelector).append($("<option/>").val('').text('---Select Currency---'));
                            var currs = JSON.parse(data);
                            $.each(currs, function () {
                                var text = this.CurName;
                                var value = this.CurCode;
                                $(currSelector).append($("<option />").val(value).text(text));
                            });

                            return currSelector;
                        }
                    }
                },
                { name: 'PriceCode', index: 'PriceCode', width: 20, sortable: false, editable: true, edittype: 'select', stype: 'select'}
            ],
            loadtext: 'Loading...',
            caption: "Bill Entities",
            scroll: true,
            hidegrid: false,
            height: 300,
            width: 650,
            rowNum: 1000,
            altRows: true,
            altclass: 'gridAltRowClass',
            onSelectRow: webview.legalentities.billing.onBillingSelected,
            loadComplete: function (data) {
                if (data.length > 0)
                {

                }
                var rowIds = $('#billingGrid').jqGrid('getDataIDs');
                $("#billingGrid").jqGrid('setSelection', rowIds[0]);
            },
            rowNum: 1000
        });
        grid.jqGrid('navGrid', '#billingPager', {
            add: ($("#dev").text() == "True" || $("#globalqc").text() == "True"),
            edit: false,
            del: false,
            search: false,
            refresh: false
        },
        { // Edit Options  
        },
        { // Add Options
            addCaption: 'Add New Billing Entity',
            beforeInitData: function (formid) {

            },
            url: als.common.getServerPath() + 'LegalEntitiesAjax/AddBillingEntity/',
            recreateForm: true,
            closeAfterAdd: true,
            reloadAfterSubmit: true
        },
        { // Del Options
        });

Solution

  • The old answer shows how one can implement the dependent selects. You use form editing. Thus the <select> element of PriceCode column of the form editing will have the id="PriceCode". You can use $("#PriceCode").html(/*new options*/); to change the options of <select> editing field of PriceCode column inside of change event handler of CurrCd column.