mirror of
https://github.com/apache/superset.git
synced 2026-05-29 20:29:34 +00:00
feat(ag-grid): Server Side Filtering for Column Level Filters (#35683)
This commit is contained in:
@@ -42,6 +42,7 @@ import TimeComparisonVisibility from './AgGridTable/components/TimeComparisonVis
|
||||
import { useColDefs } from './utils/useColDefs';
|
||||
import { getCrossFilterDataMask } from './utils/getCrossFilterDataMask';
|
||||
import { StyledChartContainer } from './styles';
|
||||
import type { FilterState } from './utils/filterStateManager';
|
||||
|
||||
const getGridHeight = (height: number, includeSearch: boolean | undefined) => {
|
||||
let calculatedGridHeight = height;
|
||||
@@ -88,6 +89,15 @@ export default function TableChart<D extends DataRecord = DataRecord>(
|
||||
|
||||
const [searchOptions, setSearchOptions] = useState<SearchOption[]>([]);
|
||||
|
||||
// Extract metric column names for SQL conversion
|
||||
const metricColumns = useMemo(
|
||||
() =>
|
||||
columns
|
||||
.filter(col => col.isMetric || col.isPercentMetric)
|
||||
.map(col => col.key),
|
||||
[columns],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
const options = columns
|
||||
.filter(col => col?.dataType === GenericDataType.String)
|
||||
@@ -101,6 +111,29 @@ export default function TableChart<D extends DataRecord = DataRecord>(
|
||||
}
|
||||
}, [columns]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!serverPagination || !serverPaginationData || !rowCount) return;
|
||||
|
||||
const currentPage = serverPaginationData.currentPage ?? 0;
|
||||
const currentPageSize = serverPaginationData.pageSize ?? serverPageLength;
|
||||
const totalPages = Math.ceil(rowCount / currentPageSize);
|
||||
|
||||
if (currentPage >= totalPages && totalPages > 0) {
|
||||
const validPage = Math.max(0, totalPages - 1);
|
||||
const modifiedOwnState = {
|
||||
...serverPaginationData,
|
||||
currentPage: validPage,
|
||||
};
|
||||
updateTableOwnState(setDataMask, modifiedOwnState);
|
||||
}
|
||||
}, [
|
||||
rowCount,
|
||||
serverPagination,
|
||||
serverPaginationData,
|
||||
serverPageLength,
|
||||
setDataMask,
|
||||
]);
|
||||
|
||||
const comparisonColumns = [
|
||||
{ key: 'all', label: t('Display all') },
|
||||
{ key: '#', label: '#' },
|
||||
@@ -121,6 +154,52 @@ export default function TableChart<D extends DataRecord = DataRecord>(
|
||||
[onChartStateChange],
|
||||
);
|
||||
|
||||
const handleFilterChanged = useCallback(
|
||||
(completeFilterState: FilterState) => {
|
||||
if (!serverPagination) return;
|
||||
// Sync chartState immediately with the new filter model to prevent stale state
|
||||
// This ensures chartState and ownState are in sync
|
||||
if (onChartStateChange && chartState) {
|
||||
const filterModel =
|
||||
completeFilterState.originalFilterModel &&
|
||||
Object.keys(completeFilterState.originalFilterModel).length > 0
|
||||
? completeFilterState.originalFilterModel
|
||||
: undefined;
|
||||
const updatedChartState = {
|
||||
...chartState,
|
||||
filterModel,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
onChartStateChange(updatedChartState);
|
||||
}
|
||||
|
||||
// Prepare modified own state for server pagination
|
||||
const modifiedOwnState = {
|
||||
...serverPaginationData,
|
||||
agGridFilterModel:
|
||||
completeFilterState.originalFilterModel &&
|
||||
Object.keys(completeFilterState.originalFilterModel).length > 0
|
||||
? completeFilterState.originalFilterModel
|
||||
: undefined,
|
||||
agGridSimpleFilters: completeFilterState.simpleFilters,
|
||||
agGridComplexWhere: completeFilterState.complexWhere,
|
||||
agGridHavingClause: completeFilterState.havingClause,
|
||||
lastFilteredColumn: completeFilterState.lastFilteredColumn,
|
||||
lastFilteredInputPosition: completeFilterState.inputPosition,
|
||||
currentPage: 0, // Reset to first page when filtering
|
||||
};
|
||||
|
||||
updateTableOwnState(setDataMask, modifiedOwnState);
|
||||
},
|
||||
[
|
||||
setDataMask,
|
||||
serverPagination,
|
||||
serverPaginationData,
|
||||
onChartStateChange,
|
||||
chartState,
|
||||
],
|
||||
);
|
||||
|
||||
const filteredColumns = useMemo(() => {
|
||||
if (!isUsingTimeComparison) {
|
||||
return columns;
|
||||
@@ -206,6 +285,8 @@ export default function TableChart<D extends DataRecord = DataRecord>(
|
||||
...serverPaginationData,
|
||||
currentPage: pageNumber,
|
||||
pageSize,
|
||||
lastFilteredColumn: undefined,
|
||||
lastFilteredInputPosition: undefined,
|
||||
};
|
||||
updateTableOwnState(setDataMask, modifiedOwnState);
|
||||
},
|
||||
@@ -218,6 +299,8 @@ export default function TableChart<D extends DataRecord = DataRecord>(
|
||||
...serverPaginationData,
|
||||
currentPage: 0,
|
||||
pageSize,
|
||||
lastFilteredColumn: undefined,
|
||||
lastFilteredInputPosition: undefined,
|
||||
};
|
||||
updateTableOwnState(setDataMask, modifiedOwnState);
|
||||
},
|
||||
@@ -230,6 +313,8 @@ export default function TableChart<D extends DataRecord = DataRecord>(
|
||||
...serverPaginationData,
|
||||
searchColumn: searchCol,
|
||||
searchText: '',
|
||||
lastFilteredColumn: undefined,
|
||||
lastFilteredInputPosition: undefined,
|
||||
};
|
||||
updateTableOwnState(setDataMask, modifiedOwnState);
|
||||
}
|
||||
@@ -243,6 +328,8 @@ export default function TableChart<D extends DataRecord = DataRecord>(
|
||||
serverPaginationData?.searchColumn || searchOptions[0]?.value,
|
||||
searchText,
|
||||
currentPage: 0, // Reset to first page when searching
|
||||
lastFilteredColumn: undefined,
|
||||
lastFilteredInputPosition: undefined,
|
||||
};
|
||||
updateTableOwnState(setDataMask, modifiedOwnState);
|
||||
},
|
||||
@@ -255,6 +342,8 @@ export default function TableChart<D extends DataRecord = DataRecord>(
|
||||
const modifiedOwnState = {
|
||||
...serverPaginationData,
|
||||
sortBy,
|
||||
lastFilteredColumn: undefined,
|
||||
lastFilteredInputPosition: undefined,
|
||||
};
|
||||
updateTableOwnState(setDataMask, modifiedOwnState);
|
||||
},
|
||||
@@ -288,6 +377,8 @@ export default function TableChart<D extends DataRecord = DataRecord>(
|
||||
onSearchColChange={handleChangeSearchCol}
|
||||
onSearchChange={handleSearch}
|
||||
onSortChange={handleSortByChange}
|
||||
onFilterChanged={handleFilterChanged}
|
||||
metricColumns={metricColumns}
|
||||
id={slice_id}
|
||||
handleCrossFilter={toggleFilter}
|
||||
percentMetrics={percentMetrics}
|
||||
|
||||
Reference in New Issue
Block a user