From 9c64217b720cb4d59a0299563ea14296aa9e73c6 Mon Sep 17 00:00:00 2001 From: Hein Date: Fri, 23 Jan 2026 10:09:59 +0200 Subject: [PATCH] =?UTF-8?q?fix(Computer):=20=E2=9C=A8=20update=20selectFir?= =?UTF-8?q?stRowOnMount=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Introduce selectFirstRowOnMount to manage row selection on component mount. * Update useEffect dependencies to include selectFirstRowOnMount. * Ensure first row selection logic handles cases where keyField is not defined. --- src/Gridler/components/Computer.tsx | 16 +++++++++++++--- .../adaptors/GlidlerAPIAdaptorForGoLangv2.tsx | 7 +++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Gridler/components/Computer.tsx b/src/Gridler/components/Computer.tsx index 4ef1167..e74550c 100644 --- a/src/Gridler/components/Computer.tsx +++ b/src/Gridler/components/Computer.tsx @@ -25,9 +25,10 @@ export const Computer = React.memo(() => { scrollToRowKey, searchStr, selectedRowKey, + selectFirstRowOnMount, setState, setStateFN, - values, + values } = useGridlerStore((s) => ({ _glideref: s._glideref, _gridSelectionRows: s._gridSelectionRows, @@ -44,6 +45,7 @@ export const Computer = React.memo(() => { scrollToRowKey: s.scrollToRowKey, searchStr: s.searchStr, selectedRowKey: s.selectedRowKey, + selectFirstRowOnMount:s.selectFirstRowOnMount, setState: s.setState, setStateFN: s.setStateFN, uniqueid: s.uniqueid, @@ -268,18 +270,25 @@ export const Computer = React.memo(() => { //Logic to select first row on mount useEffect(() => { const _events = getState('_events'); + const loadPage = () => { const selectFirstRowOnMount = getState('selectFirstRowOnMount'); + const ready = getState('ready'); + if (ready && selectFirstRowOnMount) { + const scrollToRowKey = getState('scrollToRowKey'); + + if (scrollToRowKey && scrollToRowKey >= 0) { return; } + const keyField = getState('keyField') ?? 'id'; const page_data = getState('_page_data'); const firstBuffer = page_data?.[0]?.[0]; - const firstRow = firstBuffer?.[keyField]; + const firstRow = firstBuffer?.[keyField] ?? -1; const currentValues = getState('values') ?? []; if ( @@ -290,6 +299,7 @@ export const Computer = React.memo(() => { ) { const values = [firstBuffer, ...(currentValues as Array>)]; + const onChange = getState('onChange'); //console.log('Selecting first row:', firstRow, firstBuffer, values); if (onChange) { @@ -308,7 +318,7 @@ export const Computer = React.memo(() => { return () => { _events?.removeEventListener('loadPage', loadPage); }; - }, [ready]); + }, [ready, selectFirstRowOnMount]); /// logic to apply the selected row. // useEffect(() => { diff --git a/src/Gridler/components/adaptors/GlidlerAPIAdaptorForGoLangv2.tsx b/src/Gridler/components/adaptors/GlidlerAPIAdaptorForGoLangv2.tsx index 672f96e..3a75af3 100644 --- a/src/Gridler/components/adaptors/GlidlerAPIAdaptorForGoLangv2.tsx +++ b/src/Gridler/components/adaptors/GlidlerAPIAdaptorForGoLangv2.tsx @@ -36,6 +36,7 @@ function _GlidlerAPIAdaptorForGoLangv2(props: GlidlerAPIAdaptorForG const searchStr = getState('searchStr'); const searchFields = getState('searchFields'); const _active_requests = getState('_active_requests'); + const keyField = getState('keyField'); setState('loadingData', true); try { //console.log('APIAdaptorGoLangv2', { _active_requests, index, pageSize, props }); @@ -113,6 +114,12 @@ function _GlidlerAPIAdaptorForGoLangv2(props: GlidlerAPIAdaptorForG col_ids?.push(props.hotfields.join(',')); } + if (keyField) { + if (!col_ids.includes(keyField)) { + col_ids.push(keyField); + } + } + if (col_ids && col_ids.length > 0) { ops.push({ type: 'select-fields',