ExtJS Grid 使用 `RowExpander` 外掛時,預設所有行摺疊。業務需求:資料載入完成後,自動展開符合條件的行(如 `StatusID === '3'`)。
// 1. 定義展開邏輯
function expandMatchingRows() {
const store = ProdRequireInfoStore; // Grid 的 Store
const rowsToExpand = [];
store.each(function (record, index) {
if (record.get("StatusID") === "3") {
rowsToExpand.push(index);
}
});
// 批次展開
rowsToExpand.forEach(function (rowIndex) {
expander.toggleRow(rowIndex); // expander 為 RowExpander 實例
});
}
// 2. 掛載在 Store 載入完成回呼
ProdRequireInfoStore.load({
params: {
dir: "DESC",
start: 0,
limit: 20,
KeyWord: ""
},
callback: expandMatchingRows, // 載入完成後自動執行
scope: this
});
store.each(fn):遍歷所有 record,回傳 `(record, index)`,比 `for` 迴圈更符合 ExtJS 風格。record.get("field"):取得欄位值,而非 `record.data.field`(雖然也可用,但不建議直接存取內部屬性)。expander.toggleRow(index):RowExpander 公開方法,切換指定行展開/摺疊狀態。callback:`store.load()` 選項,在資料**完全載入並處理完畢**後觸發,確保行已渲染可操作。function expandMatchingRows() {
const store = ProdRequireInfoStore;
store.each(function (record, index) {
const shouldExpand = record.get("StatusID") === "3";
const isExpanded = expander.state[index] === true; // 內部狀態記錄
if (shouldExpand && !isExpanded) {
expander.toggleRow(index);
} else if (!shouldExpand && isExpanded) {
expander.toggleRow(index); // 可選:摺疊不符合條件的行
}
});
}
關鍵在於 **Store `load` 回呼中遍歷 record、收集索引、呼叫 `expander.toggleRow(index)`**。注意取得 plugin 實例時機、大量資料效能優化,以及與 ExtJS 版本 API 差異對應。