jqGrid 編輯表單中的相關選擇框選項
jqGrid 可讓您建立編輯表單的動態選擇框,其中選項取決於在相關選擇框中進行的選擇。這對於選擇國家/地區並相應填充州選項等場景很有用。
問題:選項值不正確
但是,使用動態填滿的狀態選擇框。狀態選擇框的選項值從 0 開始,而不是預期的 5。
解決方案:重置 Editoptions 和手動重建
要解決此問題,關鍵是要了解 jqGrid 在初始化時僅使用一次 editoptions 。若要根據所選國家/地區正確填入州選擇框,必須重設編輯選項並手動重新建立選擇框。
解決方案的實施方式如下:
重置編輯選項:
資料變更:
重建選擇Box:
維護狀態值:
代碼片段:
// Reset the editoptions for the state select box
var resetStatesValues = function () {
grid.setColProp('State', { editoptions: { value: states}});
};
// Build new options for the state select box based on the selected country
var changeStateSelect = function (countryId, countryElem) {
var sc = statesOfCountry[countryId];
var newOptions = ''; // If needed
for (stateId in sc) { newOptions = ''; }
grid.setColProp('State', { editoptions: { value: statesOfCountry[countryId]} });
if ($(countryElem).is('.FormElement')) {
// Form editing
$(countryElem).closest('form.FormGrid').find("select#state.FormElement").html(newOptions);
} else {
// Inline editing
var row = $(countryElem).closest('tr.jqgrow');
var rowId = row.attr('id');
$("select#" rowId "_State", row[0]).html(newOptions);
}
};
結論
透過重置editoptions,手動重建狀態選擇框,維護選取的狀態值,可以確保jqGrid中的編輯表單顯示相關選擇框的正確選項值。
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3