From 358d31c3841ff30703cfcff66a1fb0d683011613 Mon Sep 17 00:00:00 2001 From: manasa Date: Wed, 1 Jul 2026 15:51:54 +0530 Subject: [PATCH] Added fixes for SimpleTable sorting and filtering issues --- .../SimpleTableManual/SimpleTableManual.tsx | 155 +++++++++++------- 1 file changed, 93 insertions(+), 62 deletions(-) diff --git a/packages/react-sdk-components/src/components/template/SimpleTable/SimpleTableManual/SimpleTableManual.tsx b/packages/react-sdk-components/src/components/template/SimpleTable/SimpleTableManual/SimpleTableManual.tsx index 4ddb2e42..07fecb8f 100644 --- a/packages/react-sdk-components/src/components/template/SimpleTable/SimpleTableManual/SimpleTableManual.tsx +++ b/packages/react-sdk-components/src/components/template/SimpleTable/SimpleTableManual/SimpleTableManual.tsx @@ -102,9 +102,6 @@ let menuColumnId = ''; let menuColumnType = ''; let menuColumnLabel = ''; -const filterByColumns: any[] = []; -let myRows: any[]; - export default function SimpleTableManual(props: PropsWithChildren) { const classes = useStyles(); const { @@ -133,7 +130,7 @@ export default function SimpleTableManual(props: PropsWithChildren[]>([]); + const [rowData, setRowData] = useState([]); const [elements, setElementsData] = useState([]); const [order, setOrder] = useState('asc'); const [orderBy, setOrderBy] = useState(''); @@ -150,6 +147,8 @@ export default function SimpleTableManual(props: PropsWithChildren('notequal'); const [displayDialogDateValue, setDisplayDialogDateValue] = useState(''); const selectedRowIndex: any = useRef(null); + const myRowsRef = useRef([]); + const filterByColumnsRef = useRef([]); const localizedVal = PCore.getLocaleUtils().getLocaleValue; const localeCategory = 'SimpleTable'; const parameters = fieldMetadata?.datasource?.parameters; @@ -202,7 +201,9 @@ export default function SimpleTableManual(props: PropsWithChildren { - buildElementsForTable(); + if (!(readOnlyMode && dataPageName)) { + buildElementsForTable(); + } if (readOnlyMode || allowEditingInModal) { generateRowsData(); } @@ -230,7 +231,8 @@ export default function SimpleTableManual(props: PropsWithChildren item.name).map(item => item.name) + // Temporary filter for attachments to align with constellation payload behavior. + fieldDefs.filter(item => item.name && item.meta?.type !== 'Attachment').map(item => item.name) ); } else { // @ts-expect-error - An argument for 'fields' was not provided @@ -242,8 +244,6 @@ export default function SimpleTableManual(props: PropsWithChildren col !== 'DeleteIcon'); - const getFormattedValue = (val, key) => { const rawField = fieldsWithPropNames.find(item => item.propName === key); let options = {}; @@ -266,8 +266,10 @@ export default function SimpleTableManual(props: PropsWithChildren { if (!data) return {}; - return data.map(item => { - return dataColumns.reduce((dataForRow, colKey) => { - dataForRow[colKey] = getRowValue(item, colKey); - return dataForRow; - }, {}); + return data.map((item, idx) => { + const dataForRow = displayedColumns.reduce((acc, colKey) => { + acc[colKey] = getRowValue(item, colKey); + return acc; + }, {} as any); + dataForRow.__originalIndex = idx; + return dataForRow; }); }; function generateRowsData() { + myRowsRef.current = []; // if referenceList is empty and dataPageName property value exists then make a datapage fetch call and get the list of data. if (dataPageName) { getDataPage(dataPageName, parameters, context) .then(listData => { const data = formatRowsData(listData); - myRows = data; + myRowsRef.current = data || []; setRowData(data); + // Build elements from fetched data since referenceList may be empty for data page tables + if (readOnlyMode) { + buildElementsFromData(listData as any[]); + } }) .catch(e => { console.log(e); @@ -300,16 +309,19 @@ export default function SimpleTableManual(props: PropsWithChildren { + const dataForRow: any = {}; + for (const col of displayedColumns) { const colKey: string = col; const theVal = getRowValue(row, colKey); dataForRow[colKey] = theVal || ''; } + // Preserve the original position so stableSort can look up the right + // entry in `elements` even after filtering shrinks rowData. + dataForRow.__originalIndex = idx; data.push(dataForRow); - myRows = data; - } + }); + myRowsRef.current = data; setRowData(data); } } @@ -390,13 +402,14 @@ export default function SimpleTableManual(props: PropsWithChildren { + const data: any = []; + rawFields.forEach(item => { + if (!item.config.hide) { + const propName = item.config.value.replace('@P .', ''); + const val = getRowValue(element, propName); + data.push(createElement('span', { key: `${index}-${propName}` }, val ?? '---')); + } + }); + eleData.push(data); + }); + setElementsData(eleData); + } + const handleRequestSort = (event: React.MouseEvent, property: keyof any) => { const isAsc = orderBy === property && order === 'asc'; setOrder(isAsc ? 'desc' : 'asc'); @@ -448,7 +477,7 @@ export default function SimpleTableManual(props: PropsWithChildren el[0]); + return stabilizedThis.map(([el]) => el); } function _menuClick(event, columnId: string, columnType: string, labelValue: string) { @@ -474,7 +503,7 @@ export default function SimpleTableManual(props: PropsWithChildren @@ -668,7 +697,7 @@ export default function SimpleTableManual(props: PropsWithChildren - {row.map((item, childIndex) => { + {row?.map((item, childIndex) => { const theColKey = `data-${index}-${childIndex}`; return ( @@ -695,41 +724,43 @@ export default function SimpleTableManual(props: PropsWithChildren 0 && - stableSort(rowData, getComparator(order, orderBy)) - .slice(0) - .map((row, index) => { - return ( - - {Object.keys(row).map(colKey => { - return ( - - {row[colKey] || '---'} - - ); - })} - {showDeleteButton && ( - -
- { - editMenuClick(event, index); - }} - /> - - editRecord()}>Edit - deleteRecord()}>Delete - -
+ stableSort(rowData, getComparator(order, orderBy)).map((row: any, displayIndex) => { + const originalIndex = row.__originalIndex; + return ( + + {(elements as any[])[originalIndex]?.map((item, childIndex) => { + const theColKey = displayedColumns[childIndex]; + return ( + + {item} - )} - - ); - })} + ); + })} + {showDeleteButton && ( + +
+ { + editMenuClick(event, originalIndex); + }} + /> + + editRecord()}>Edit + deleteRecord()}>Delete + +
+
+ )} +
+ ); + })} - {((readOnlyMode && (!rowData || rowData?.length === 0)) || (editableMode && (!referenceList || referenceList?.length === 0))) && ( + {((readOnlyMode && (!rowData || rowData?.length === 0)) || + (editableMode && (!referenceList || referenceList?.length === 0)) || + (allowEditingInModal && (!rowData || rowData?.length === 0))) && (
{getGenericFieldsLocalizedValue('COSMOSFIELDS.lists', 'No records found.')}