Refactor ObjectTable component to utilize refs for table filter and sorter management
- Replaced state management for tableFilter and tableSorter with refs for improved performance and reduced re-renders. - Introduced tableData state to manage flattened data for display. - Updated data fetching logic to reference current filter and sorter values from refs. - Enhanced useEffect to flatten pages array for table display.
This commit is contained in:
parent
2879b8648d
commit
f7532338b6
@ -88,8 +88,8 @@ const ObjectTable = forwardRef(
|
|||||||
const [, contextHolder] = message.useMessage()
|
const [, contextHolder] = message.useMessage()
|
||||||
const tableRef = useRef(null)
|
const tableRef = useRef(null)
|
||||||
const model = getModelByName(type)
|
const model = getModelByName(type)
|
||||||
const [tableFilter, setTableFilter] = useState({})
|
const tableFilterRef = useRef({})
|
||||||
const [tableSorter, setTableSorter] = useState({})
|
const tableSorterRef = useRef({})
|
||||||
const [initialized, setInitialized] = useState(false)
|
const [initialized, setInitialized] = useState(false)
|
||||||
|
|
||||||
// Table state
|
// Table state
|
||||||
@ -98,6 +98,7 @@ const ObjectTable = forwardRef(
|
|||||||
const [hasMore, setHasMore] = useState(true)
|
const [hasMore, setHasMore] = useState(true)
|
||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
const [lazyLoading, setLazyLoading] = useState(false)
|
const [lazyLoading, setLazyLoading] = useState(false)
|
||||||
|
const [tableData, setTableData] = useState([])
|
||||||
|
|
||||||
const subscribedIdsRef = useRef([])
|
const subscribedIdsRef = useRef([])
|
||||||
// const [typeSubscribed, setTypeSubscribed] = useState(false)
|
// const [typeSubscribed, setTypeSubscribed] = useState(false)
|
||||||
@ -159,17 +160,17 @@ const ObjectTable = forwardRef(
|
|||||||
const fetchData = useCallback(
|
const fetchData = useCallback(
|
||||||
async (pageNum = 1, filter = null, sorter = null) => {
|
async (pageNum = 1, filter = null, sorter = null) => {
|
||||||
if (filter == null) {
|
if (filter == null) {
|
||||||
filter = tableFilter
|
filter = tableFilterRef.current
|
||||||
} else {
|
} else {
|
||||||
setTableFilter(filter)
|
tableFilterRef.current = filter
|
||||||
}
|
}
|
||||||
if (sorter == null) {
|
if (sorter == null) {
|
||||||
sorter = tableSorter
|
sorter = tableSorterRef.current
|
||||||
} else {
|
} else {
|
||||||
setTableSorter({
|
tableSorterRef.current = {
|
||||||
field: sorter.field,
|
field: sorter.field,
|
||||||
order: sorter.order
|
order: sorter.order
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
console.log('Fetching data...')
|
console.log('Fetching data...')
|
||||||
try {
|
try {
|
||||||
@ -213,15 +214,7 @@ const ObjectTable = forwardRef(
|
|||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[
|
[type, masterFilter, pageSize, onDataChange, fetchObjects]
|
||||||
type,
|
|
||||||
masterFilter,
|
|
||||||
pageSize,
|
|
||||||
tableFilter,
|
|
||||||
tableSorter,
|
|
||||||
onDataChange,
|
|
||||||
fetchObjects
|
|
||||||
]
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const loadNextPage = useCallback(() => {
|
const loadNextPage = useCallback(() => {
|
||||||
@ -480,8 +473,8 @@ const ObjectTable = forwardRef(
|
|||||||
|
|
||||||
if (hasChanged) {
|
if (hasChanged) {
|
||||||
setPages([])
|
setPages([])
|
||||||
setTableFilter({})
|
tableFilterRef.current = {}
|
||||||
setTableSorter({})
|
tableSorterRef.current = {}
|
||||||
setInitialized(false)
|
setInitialized(false)
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
setLazyLoading(false)
|
setLazyLoading(false)
|
||||||
@ -560,6 +553,12 @@ const ObjectTable = forwardRef(
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
pagesRef.current = pages
|
pagesRef.current = pages
|
||||||
}, [pages])
|
}, [pages])
|
||||||
|
|
||||||
|
// Flatten pages array for table display
|
||||||
|
useEffect(() => {
|
||||||
|
setTableData(pages.flatMap((page) => page.items))
|
||||||
|
}, [pages])
|
||||||
|
|
||||||
// Add columns in the order specified by model.columns
|
// Add columns in the order specified by model.columns
|
||||||
model.columns.forEach((colName) => {
|
model.columns.forEach((colName) => {
|
||||||
const prop = modelProperties.find((p) => p.name === colName)
|
const prop = modelProperties.find((p) => p.name === colName)
|
||||||
@ -601,9 +600,8 @@ const ObjectTable = forwardRef(
|
|||||||
const isSortable = model.sorters && model.sorters.includes(prop.name)
|
const isSortable = model.sorters && model.sorters.includes(prop.name)
|
||||||
|
|
||||||
const columnConfig = {
|
const columnConfig = {
|
||||||
sorter: isSortable,
|
sorter: isSortable ? { multiple: 1 } : undefined,
|
||||||
title: prop.label,
|
title: prop.label,
|
||||||
dataIndex: prop.name,
|
|
||||||
width: prop.columnWidth || width,
|
width: prop.columnWidth || width,
|
||||||
fixed: isMobile ? undefined : fixed,
|
fixed: isMobile ? undefined : fixed,
|
||||||
key: prop.name,
|
key: prop.name,
|
||||||
@ -663,9 +661,6 @@ const ObjectTable = forwardRef(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flatten pages array for table display
|
|
||||||
const tableData = pages.flatMap((page) => page.items)
|
|
||||||
|
|
||||||
// Card view rendering
|
// Card view rendering
|
||||||
const cardsContainerRef = useRef(null)
|
const cardsContainerRef = useRef(null)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user