[FIX] 員工卡編輯彈窗所屬公司仍未回填:修正 Preline getInstance 取錯物件

1. 接續前一個 hotfix:員工卡編輯彈窗的「所屬公司」下拉雖已部署字串轉型,仍顯示空白「選擇公司」。根因為 Preline v3 的 HSSelect.getInstance(target, true) 回傳的是 collection 包裝物件({id, element})而非元件實例,其上沒有 setValue,呼叫即丟 TypeError 並讓 $nextTick 回填中斷。
2. 移除錯誤的第二個參數 true,改以 window.HSSelect.getInstance(el) 取得真正實例(對齊全站 ads / welcome-gifts / pickup-codes 既有可運作寫法)。
3. 一併沿用 welcome-gifts 的穩健流程:先設原生 <select>.value 再呼叫 setValue,並保留數字轉字串、空值以 ' ' 對應 placeholder 的處理。
4. 影響範圍:僅 resources/views/admin/data-config/staff-cards/index.blade.php 之 openStaffModal 回填邏輯,無後端與資料異動。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
sky121113 2026-06-23 11:39:56 +08:00
parent 09d669cc7c
commit 76409c5f21

View File

@ -444,15 +444,19 @@
} }
this.isStaffModalOpen = true; this.isStaffModalOpen = true;
// Re-init Preline select or set value after modal opens // 回填所屬公司下拉:
// 1. Preline v3 getInstance(target, true) 回傳的是 collection wrapper 而非實例,
// 其上沒有 setValue 會丟錯導致回填失靈,故不可帶第二個參數。
// 2. Preline 的 option value 為字串company_id 為數字時嚴格比對會失配,統一轉字串;空值用 ' ' 對應 placeholder。
// 同步先設原生 <select>.value 再呼叫 setValue對齊 welcome-gifts / pickup-codes 既有可運作寫法)。
this.$nextTick(() => { this.$nextTick(() => {
const select = HSSelect.getInstance('#modal-company-id', true); const el = document.getElementById('modal-company-id');
if (select) { if (el) {
// Preline 的 option value 為字串company_id 為數字時嚴格比對會失配導致回填失敗,
// 故統一轉成字串;空值則以 ' ' 對應 placeholder 選項
const cid = this.staffFormFields.company_id; const cid = this.staffFormFields.company_id;
const valStr = (cid !== undefined && cid !== null && cid.toString().trim() !== '') ? cid.toString() : ' '; const valStr = (cid !== undefined && cid !== null && cid.toString().trim() !== '') ? cid.toString() : ' ';
select.setValue(valStr); el.value = valStr;
const inst = window.HSSelect && window.HSSelect.getInstance(el);
if (inst) inst.setValue(valStr);
} }
}); });
}, },