Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added option to emit a filter from a default column for a row in ag grid #220

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export default function CccsGrid({
tooltipShowDelay,
rowSelection,
emitFilter,
defaultEmitFilterColumn,
include_search,
page_length = 0,
enable_grouping = false,
Expand All @@ -88,6 +89,7 @@ export default function CccsGrid({
);

const [filters, setFilters] = useState(initialFilters);
const [defaultFilters, setDefaultFilters] = useState({});
const [searchValue, setSearchValue] = useState('');
const [pageSize, setPageSize] = useState<number>(page_length);

Expand Down Expand Up @@ -125,7 +127,7 @@ export default function CccsGrid({
},
});
},
[emitFilter, setDataMask],
[emitFilter, setDataMask, filters, defaultFilters],
); // only take relevant page size options

const getContextMenuItems = useCallback(
Expand All @@ -146,6 +148,13 @@ export default function CccsGrid({
// eslint-disable-next-line theme-colors/no-literal-colors
icon: '<svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" class=""><path fill-rule="evenodd" clip-rule="evenodd" d="M18.1573 17.864C21.2763 14.745 21.2763 9.66935 18.1573 6.5503C15.0382 3.43125 9.96264 3.43125 6.84359 6.5503L5.42938 5.13609C9.32836 1.2371 15.6725 1.2371 19.5715 5.13609C23.4705 9.03507 23.4705 15.3792 19.5715 19.2782C15.6725 23.1772 9.32836 23.1772 5.42938 19.2782L6.84359 17.864C9.96264 20.9831 15.0375 20.9838 18.1573 17.864ZM2.00035 11.5C2.00035 11.2239 2.2242 11 2.50035 11H5.00035L5.00035 10C5.00035 9.58798 5.47073 9.35279 5.80035 9.60001L9.00035 12C9.17125 12.1032 6.98685 13.637 5.77613 14.4703C5.44613 14.6975 5.00035 14.4601 5.00035 14.0595V13L2.50035 13C2.22421 13 2.00035 12.7761 2.00035 12.5L2.00035 11.5ZM9.67202 9.37873C11.2319 7.81885 13.7697 7.81956 15.3289 9.37873C16.888 10.9379 16.8887 13.4757 15.3289 15.0356C13.769 16.5955 11.2312 16.5948 9.67202 15.0356L8.2578 16.4498C10.5976 18.7896 14.4033 18.7896 16.7431 16.4498C19.0829 14.11 19.0829 10.3043 16.7431 7.96451C14.4033 5.6247 10.5976 5.6247 8.2578 7.96451L9.67202 9.37873Z" fill="#20A7C9"></path></svg>',
},
{
name: 'Emit Default Column Filter(s)',
disabled: ensureIsArray(defaultEmitFilterColumn).length === 0,
action: () => handleChange(defaultFilters),
// eslint-disable-next-line theme-colors/no-literal-colors
icon: '<svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" class=""><path fill-rule="evenodd" clip-rule="evenodd" d="M18.1573 17.864C21.2763 14.745 21.2763 9.66935 18.1573 6.5503C15.0382 3.43125 9.96264 3.43125 6.84359 6.5503L5.42938 5.13609C9.32836 1.2371 15.6725 1.2371 19.5715 5.13609C23.4705 9.03507 23.4705 15.3792 19.5715 19.2782C15.6725 23.1772 9.32836 23.1772 5.42938 19.2782L6.84359 17.864C9.96264 20.9831 15.0375 20.9838 18.1573 17.864ZM2.00035 11.5C2.00035 11.2239 2.2242 11 2.50035 11H5.00035L5.00035 10C5.00035 9.58798 5.47073 9.35279 5.80035 9.60001L9.00035 12C9.17125 12.1032 6.98685 13.637 5.77613 14.4703C5.44613 14.6975 5.00035 14.4601 5.00035 14.0595V13L2.50035 13C2.22421 13 2.00035 12.7761 2.00035 12.5L2.00035 11.5ZM9.67202 9.37873C11.2319 7.81885 13.7697 7.81956 15.3289 9.37873C16.888 10.9379 16.8887 13.4757 15.3289 15.0356C13.769 16.5955 11.2312 16.5948 9.67202 15.0356L8.2578 16.4498C10.5976 18.7896 14.4033 18.7896 16.7431 16.4498C19.0829 14.11 19.0829 10.3043 16.7431 7.96451C14.4033 5.6247 10.5976 5.6247 8.2578 7.96451L9.67202 9.37873Z" fill="#20A7C9"></path></svg>',
},
{
name: 'Clear Emitted Filter(s)',
disabled: crossFilterValue === undefined,
Expand All @@ -159,12 +168,14 @@ export default function CccsGrid({
return result;
},
[
crossFilterValue,
dispatch,
emitFilter,
defaultEmitFilterColumn,
crossFilterValue,
handleChange,
filters,
defaultFilters,
dispatch,
formData.slice_id,
handleChange,
],
);

Expand All @@ -177,6 +188,10 @@ export default function CccsGrid({
customTooltip: CustomTooltip,
};

function getEmitTarget(col: string) {
return formData.column_config?.[col]?.emitTarget || col;
}

const onGridReady = (params: any) => {
if (column_state) {
params.columnApi.applyColumnState({
Expand All @@ -202,6 +217,8 @@ export default function CccsGrid({
const cellRanges = gridApi.getCellRanges();

const updatedFilters = {};
const updatedDefaultFilters = {};

cellRanges.forEach((range: any) => {
range.columns.forEach((column: any) => {
const col = getEmitTarget(column.colDef?.field);
Expand All @@ -211,7 +228,7 @@ export default function CccsGrid({
range.endRow.rowIndex,
);
const endRow = Math.max(range.startRow.rowIndex, range.endRow.rowIndex);
for (let rowIndex = startRow; rowIndex <= endRow; rowIndex++) {
for (let rowIndex = startRow; rowIndex <= endRow; rowIndex += 1) {
const value = gridApi.getValue(
column,
gridApi.getModel().getRow(rowIndex),
Expand All @@ -221,15 +238,30 @@ export default function CccsGrid({
}
}
});
ensureIsArray(defaultEmitFilterColumn).forEach((column: any) => {
const col = getEmitTarget(column);
updatedDefaultFilters[col] = updatedDefaultFilters[col] || [];
const startRow = Math.min(
range.startRow.rowIndex,
range.endRow.rowIndex,
);
const endRow = Math.max(range.startRow.rowIndex, range.endRow.rowIndex);
for (let rowIndex = startRow; rowIndex <= endRow; rowIndex += 1) {
const defaultValue = gridApi.getValue(
column,
gridApi.getModel().getRow(rowIndex),
);
if (!updatedDefaultFilters[col].includes(defaultValue)) {
updatedDefaultFilters[col].push(defaultValue);
}
}
});
});

setFilters(updatedFilters);
setDefaultFilters(updatedDefaultFilters);
};

function getEmitTarget(col: string) {
return formData.column_config?.[col]?.emitTarget || col;
}

function autoSizeFirst100Columns(params: any) {
// Autosizes only the first 100 Columns in Ag-Grid
const allColumnIds = params.columnApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
ControlPanelState,
ControlState,
formatSelectOptions,
ColumnMeta,
} from '@superset-ui/chart-controls';
import { StyledColumnOption } from 'src/explore/components/optionRenderers';

Expand Down Expand Up @@ -401,6 +402,49 @@ const config: ControlPanelConfig = {
}
: null,
],
[
isFeatureEnabled(FeatureFlag.DASHBOARD_CROSS_FILTERS)
? {
name: 'defaultEmitFilterColumn',
config: {
type: 'SelectControl',
label: t('Default Emit Filter Column(s)'),
description: t(
'Select columns that the emit filter button will use by default',
),
multi: true,
freeForm: true,
allowAll: true,
default: [],
canSelectAll: true,
optionRenderer: (c: ColumnMeta) => (
// eslint-disable-next-line react/react-in-jsx-scope
<StyledColumnOption showType column={c} />
),
// eslint-disable-next-line react/react-in-jsx-scope
valueRenderer: (c: ColumnMeta) => (
// eslint-disable-next-line react/react-in-jsx-scope
<StyledColumnOption column={c} />
),
valueKey: 'column_name',
mapStateToProps: (
state: ControlPanelState,
controlState: ControlState,
) => {
const originalMapStateToProps =
sharedControls?.columns?.mapStateToProps;
const newState =
originalMapStateToProps?.(state, controlState) ?? {};
return newState;
},
visibility: ({ controls }) =>
// TODO properly emsure is Bool
Boolean(controls?.emitFilter?.value),
canCopy: true,
},
}
: null,
],
[
{
name: 'column_state',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export default function transformProps(chartProps: CccsGridChartProps) {
headerFontSize,
headerText,
emitFilter,
defaultEmitFilterColumn,
page_length,
query_mode,
include_search,
Expand Down Expand Up @@ -328,6 +329,7 @@ export default function transformProps(chartProps: CccsGridChartProps) {
headerFontSize,
headerText,
emitFilter,
defaultEmitFilterColumn,
include_search,
page_length,
enable_grouping,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export interface CccsGridTransformedProps extends CccsGridStylesProps {
setControlValue: HandlerFunction;
selectedValues: Record<number, string>;
emitFilter: boolean;
defaultEmitFilterColumn: any;
data: TimeseriesDataRecord[];
columnDefs: any;
rowData: any;
Expand Down