jqGrid 導航列點擊「新增」按鈕,彈出的編輯對話框**空白無任何輸入欄位**;但「檢視記錄」卻能正常顯示欄位內容。
jqGrid 的 **內嵌新增/編輯表單** 欄位定義**不自動從 `colModel` 繼承**,必須在 **`navGrid` 配置中明確指定 `edit`/`add` 參數**,或對每個欄位設定 `editable: true` 並配置 `editoptions`/`formoptions`。
$("#grid").jqGrid('navGrid', '#pager',
{ view: true, edit: true, add: true, del: true, search: true }, // 按鈕顯示控制
// 編輯對話框參數
{
width: 400,
closeAfterEdit: true,
recreateForm: true, // 關鍵:每次重新建立表單,避免殘留狀態
beforeShowForm: function(form) { /* 自訂邏輯 */ }
},
// 新增對話框參數 (最關鍵!)
{
width: 400,
closeAfterAdd: true,
recreateForm: true, // 必須設 true,否則欄位可能不渲染
beforeShowForm: function(form) { /* 自訂邏輯 */ }
},
// 刪除參數
{
width: 300,
msg: "確定刪除?"
},
// 搜尋參數
{
multipleSearch: true
},
// 檢視參數
{
width: 400
}
);
colModel: [
{ name: 'id', index: 'id', editable: false, hidden: true }, // 主鍵通常不可編輯
{ name: 'name', index: 'name', editable: true, editrules: { required: true }, formoptions: { label: '姓名' } },
{ name: 'email', index: 'email', editable: true, editrules: { email: true }, editoptions: { size: 30 } },
{ name: 'status', index: 'status', editable: true, edittype: 'select', editoptions: { value: '1:啟用;0:停用' }, formoptions: { label: '狀態' } }
// ...
]
關鍵屬性:
jqGrid 內嵌新增表單空白,**核心修復是 `navGrid` 的 `add` 參數加上 `recreateForm: true`**,並確保 `colModel` 對應欄位設定 `editable: true` 及完整的 `editoptions`/`formoptions`。檢視記錄正常是因為 `view` 參數預設行為不同,不依賴編輯配置。