亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

編輯框中不正確的選擇下拉選項值

編輯框中不正確的選擇下拉選項值

MMTTMM 2019-07-05 15:28:12
編輯框中不正確的選擇下拉選項值我正在使用表單編輯。表單中有兩個復選框。一個復選框是國家,另一個復選框是國家。狀態復選框取決于選定的國家,并將動態填充。例如:國家:美國(期權價值=1)英國(期權價值=2)國家代表美國:阿拉巴馬州(期權價值=1)加利福尼亞(期權價值=2)佛羅里達(期權價值=3)夏威夷(期權價值=4)聯合王國國家:倫敦(期權價值=5)牛津(期權價值=6)如上文所示,英國的id以5開頭。Country id=2 (UK)和State id=6 (Oxford),編輯表格將正確顯示-國家是英國和國家是牛津。但是,如果您下拉狀態復選框,選項文本是正確的(它顯示倫敦、牛津),但是選項值將從0開始。正確的是,選項值應該從5開始。如果選擇并將“國家”下拉框更改為“US”,然后再次更改為“英國”,則選項值將被正確填充(從5開始)。我的問題是,在加載編輯表單時,如何使用基于編輯框中國家的正確選項值填充狀態的選擇框?
查看完整描述

3 回答

?
富國滬深

TA貢獻1790條經驗 獲得超9個贊

你的問題的答案在一定程度上取決于你收到的“美國國家”和“英國國家”下顯示的信息的來源。jqGrid支持的兩種可能性:1)使用value參數編輯選項2)使用dataUrlbuildSelect參數的編輯選項..第一種方法是在本地編輯的情況下最好的方法,或者如果可能的選項列表是靜態的。在這種情況下,將使用第二個選擇,即每個Ajax請求將從數據庫獲取有關國家、國家和國家的信息。我在示例中描述了解決方案-使用value參數不依賴于服務器組件。的情況下,實現的大部分是相同的。dataUrlbuildSelect.

我做了活生生的例子這證明了你所需要的。

主要問題是value.的.editoptions使用只有一次在初始化的時候。在.內部dataInit函數一可以覆蓋value但是,在第一個“選擇/下拉”框中的值隨國家更改后,必須手動重建帶有狀態的第二個“選擇/下拉”框。要做到這一點,我們必須理解,SELECT HTML元素的id是由行id‘_’和列名:rowId+“_State”構造的。而且重要的是value.的.editoptions必須將其重置為初始值,以便可以將任何狀態id解碼為狀態名稱。

這是來自例子:

var countries = { '1': 'US', '2': 'UK' };var states = { '1': 'Alabama', '2': 'California', '3': 'Florida', '4': 'Hawaii', '5': 'London', 
6': 'Oxford' };var statesOfCountry = {
    1: { '1': 'Alabama', '2': 'California', '3': 'Florida', '4': 'Hawaii' },
    2: { '5': 'London', '6': 'Oxford' }};var 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" }];var lastSel = -1;var grid = jQuery("#list");var resetStatesValues =
     function () {
    grid.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.setColProp('State', { editoptions: { value: statesOfCountry[v]} });
                },
                dataEvents: [
                    {
                        type: 'change',
                        fn: function(e) {
                            // To be able to save the results of the current selection
                            // the value of the column property must contain at least
                            // the current selected 'State'. So we have to reset
                            // the column property to the following
                            //grid.setColProp('State', { editoptions:{value: statesOfCountry[v]} });
                            //grid.setColProp('State', { editoptions: { value: states} });
                            resetStatesValues();

                            // build 'State' options based on the selected 'Country' value
                            var v = parseInt($(e.target).val(), 10);
                            var sc = statesOfCountry[v];
                            var newOptions = '';
                            for (var stateId in sc) {
                                if (sc.hasOwnProperty(stateId)) {
                                    newOptions += '<option role="option" value="' +
                                                  stateId + '">' +
                                                  states[stateId] + '</option>';
                                }
                            }

                            // populate the new
                            if ($(e.target).is('.FormElement')) {
                                // form editing
                                var form = $(e.target).closest('form.FormGrid');
                                $("select#State.FormElement", form[0]).html(newOptions);
                            } else {
                                // inline editing
                                var row = $(e.target).closest('tr.jqgrow');
                                var rowId = row.attr('id');
                                $("select#" + rowId + "_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.restoreRow(lastSel);
            }
            lastSel = id;
        }
    },
    ondblClickRow: function (id, ri, ci) {
        if (id && id !== lastSel) {
            grid.restoreRow(lastSel);
            lastSel = id;
        }
        resetStatesValues();
        grid.editRow(id, true, null, null, 'clientArray', null,
                        function (rowid, response) {  // aftersavefunc
                            grid.setColProp('State', { editoptions: { value: states} });
                        });
        return;
    },
    editurl: 'clientArray',
    sortname: 'Name',
    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: false },
          { // edit options
              recreateForm:true,
              onClose:function() {
                  resetStatesValues();
              }
          },
          { // add options
              recreateForm:true,
              onClose:function() {
                  resetStatesValues();
              }
          });

更新:我更新了上面的代碼,使它在表單編輯的情況下也可以使用。你可以看到它的實況這里..因為jqGrid不支持表單編輯的本地編輯,所以我無法測試代碼。盡管如此,我還是希望我能充分利用所需的改變。

更新2*我擴展了上述代碼以支持

  1. 內聯編輯、表單編輯、搜索工具欄和高級搜索
  2. 編輯窗體中的前一個或下一個導航按鈕。
  3. 改進在SELECT中的鍵盤支持(在某些瀏覽器中更新依賴的SELECT問題是固定的)

演示的新版本是這里..演示的修改代碼如下:

var countries = { '1': 'US', '2': 'UK' },
    //allCountries = {'': 'All', '1': 'US', '2': 'UK'},
    // we use string form of allCountries to have control on the order of items
    allCountries = ':All;1:US;2:UK',
    states = { '1': 'Alabama', '2': 'California', '3': 'Florida', '4': 'Hawaii', '5': 'London', '6': 'Oxford' },
    allStates = ':All;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 = { '': states, '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"),
    removeAllOption = function (elem) {
        if (typeof elem === "object" && typeof elem.id === "string" && elem.id.substr(0, 3) !== "gs_") {
            // in the searching bar
            $(elem).find('option[value=""]').remove();
        }
    },
    resetStatesValues = function () {
        // set 'value' property of the editoptions to initial state
        grid.jqGrid('setColProp', 'state', { editoptions: { value: states} });
    },
    setStateValues = function (countryId) {
        // 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[countryId]} });
    },
    changeStateSelect = function (countryId, countryElem) {
        // build 'state' options based on the selected 'country' value
        var stateId, stateSelect, parentWidth, $row,
            $countryElem = $(countryElem),
            sc = statesOfCountry[countryId],
            isInSearchToolbar = $countryElem.parent().parent().parent().hasClass('ui-search-toolbar'),
                              //$(countryElem).parent().parent().hasClass('ui-th-column')
            newOptions = isInSearchToolbar ? '<option value="">All</option>' : '';

        for (stateId in sc) {
            if (sc.hasOwnProperty(stateId)) {
                newOptions += '<option role="option" value="' + stateId + '">' +
                    states[stateId] + '</option>';
            }
        }

        setStateValues(countryId);

        // populate the subset of contries
        if (isInSearchToolbar) {
            // searching toolbar
            $row = $countryElem.closest('tr.ui-search-toolbar');
            stateSelect = $row.find(">th.ui-th-column select#gs_state");
            parentWidth = stateSelect.parent().width();
            stateSelect.html(newOptions).css({width: parentWidth});
        } else if ($countryElem.is('.FormElement')) {
            // form editing
            $countryElem.closest('form.FormGrid').find("select#state.FormElement").html(newOptions);
        } else {
            // inline editing
            $row = $countryElem.closest('tr.jqgrow');
            $("select#" + $.jgrid.jqID($row.attr('id')) + "_state").html(newOptions);
        }
    },
    editGridRowOptions = {
        recreateForm: true,
        onclickPgButtons: function (whichButton, $form, rowid) {
            var $row = $('#' + $.jgrid.jqID(rowid)), countryId;
            if (whichButton === 'next') {
                $row = $row.next();
            } else if (whichButton === 'prev') {
                $row = $row.prev();
            }
            if ($row.length > 0) {
                countryId = grid.jqGrid('getCell', $row.attr('id'), 'country');
                changeStateSelect(countryId, $("#country")[0]);
            }
        },
        onClose: function () {
            resetStatesValues();
        }
    };grid.jqGrid({
    data: mydata,
    datatype: 'local',
    colModel: [
        { name: 'name', width: 200, editable: true },
        { name: 'country', width: 100, editable: true, formatter: 'select', stype: 'select', edittype: 'select',
            searchoptions: {
                value: allCountries,
                dataInit: function (elem) { removeAllOption(elem); },
                dataEvents: [
                    { type: 'change', fn: function (e) { changeStateSelect($(e.target).val(), e.target); } },
                    { type: 'keyup', fn: function (e) { $(e.target).trigger('change'); } }
                ]
            },
            editoptions: {
                value: countries,
                dataInit: function (elem) { setStateValues($(elem).val()); },
                dataEvents: [
                    { type: 'change', fn: function (e) { changeStateSelect($(e.target).val(), e.target); } },
                    { type: 'keyup', fn: function (e) { $(e.target).trigger('change'); } }
                ]
            }},
        { name: 'state', width: 100, formatter: 'select', stype: 'select',
            editable: true, edittype: 'select', editoptions: { value: states },
            searchoptions: { value: allStates, dataInit: function (elem) { removeAllOption(elem); } } }
    ],
    onSelectRow: function (id) {
        if (id && id !== lastSel) {
            if (lastSel !== -1) {
                $(this).jqGrid('restoreRow', lastSel);
                resetStatesValues();
            }
            lastSel = id;
        }
    },
    ondblClickRow: function (id) {
        if (id && id !== lastSel) {
            $(this).jqGrid('restoreRow', lastSel);
            lastSel = id;
        }
        resetStatesValues();
        $(this).jqGrid('editRow', id, {
            keys: true,
            aftersavefunc: function () {
                resetStatesValues();
            },
            afterrestorefunc: function () {
                resetStatesValues();
            }
        });
        return;
    },
    editurl: 'clientArray',
    sortname: 'name',
    ignoreCase: true,
    height: '100%',
    viewrecords: true,
    rownumbers: true,
    sortorder: "desc",
    pager: '#pager',
    caption: "Demonstrate dependend select/dropdown lists (inline editing on double-click)"});grid.jqGrid('navGrid', '#pager', 
    { del: false }, editGridRowOptions, editGridRowOptions);grid.jqGrid('filterToolbar', {stringResult: true, searchOnEnter: t
    rue, defaultSearch : "cn"});

更新3:演示代碼的最后一個版本這里.


查看完整回答
反對 回復 2019-07-05
?
蝴蝶刀刀

TA貢獻1801條經驗 獲得超8個贊

我正在使用表單編輯。表單中有三個復選框。一個復選框是國家,一個復選框是城市,另一個復選框是街道?!俺鞘小睆瓦x框取決于所選國家,并將動態填充。“街道”復選框取決于所選城市,并將動態填充。我拯救了Mysql的Country、City、Street。如果選擇“國家”,如何從MySQL表中更改“城市”復選框?


查看完整回答
反對 回復 2019-07-05
?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

實際上,我已經得到了ON更改,選擇了國家并正確填充了狀態。沒有問題。您的示例顯示了行編輯,但我對表單編輯有問題(選擇突出顯示一行并單擊鉛筆圖標)。我遇到的問題是,當表單出現時,選中的國家和選定的狀態將正確顯示在復選框中。狀態的下拉框也正確填充。問題是,選項值的值從0開始,這是錯誤的

查看完整回答
反對 回復 2019-07-05
  • 3 回答
  • 0 關注
  • 593 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號