Skip to content

Commit

Permalink
Add a getClearFilterRange method on IrisGridModel
Browse files Browse the repository at this point in the history
- Returns the range of columns that should be cleared when clearing the filter on this column
  • Loading branch information
mofojed committed Nov 13, 2023
1 parent 5f4d8f3 commit 65174d6
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 26 deletions.
14 changes: 9 additions & 5 deletions packages/grid/src/GridRange.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
export type GridRangeIndex = number | null;
export type RequiredGridRangeIndex = number;
type LeftIndex = GridRangeIndex;
type RightIndex = GridRangeIndex;
type TopIndex = GridRangeIndex;
type BottomIndex = GridRangeIndex;

export type GridCell = { column: number; row: number };
export type GridCell = {
column: RequiredGridRangeIndex;
row: RequiredGridRangeIndex;
};

export interface BoundedGridRange extends GridRange {
startColumn: number;
startRow: number;
endColumn: number;
endRow: number;
startColumn: RequiredGridRangeIndex;
startRow: RequiredGridRangeIndex;
endColumn: RequiredGridRangeIndex;
endRow: RequiredGridRangeIndex;
}

// Also exported via GridRange.SELECTION_DIRECTION
Expand Down
43 changes: 28 additions & 15 deletions packages/iris-grid/src/IrisGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,21 @@ import { ChartBuilderSettings } from './sidebar/ChartBuilder';
import AggregationOperation from './sidebar/aggregations/AggregationOperation';
import { UIRollupConfig } from './sidebar/RollupRows';
import {
ReadonlyAdvancedFilterMap,
ColumnName,
ReadonlyQuickFilterMap,
ReadonlyAggregationMap,
ReadonlyOperationMap,
Action,
OptionItem,
UITotalsTableConfig,
InputFilter,
PendingDataMap,
AdvancedFilterMap,
AdvancedFilterOptions,
ColumnName,
InputFilter,
OperationMap,
OptionItem,
PendingDataErrorMap,
PendingDataMap,
QuickFilterMap,
OperationMap,
ReadonlyAdvancedFilterMap,
ReadonlyAggregationMap,
ReadonlyOperationMap,
ReadonlyQuickFilterMap,
UITotalsTableConfig,
} from './CommonTypes';
import ColumnHeaderGroup from './ColumnHeaderGroup';

Expand Down Expand Up @@ -1605,19 +1606,31 @@ export class IrisGrid extends Component<IrisGridProps, IrisGridState> {
});
}

removeColumnFilter(modelColumn: ModelIndex): void {
removeColumnFilter(modelRange: ModelIndex | BoundedAxisRange): void {
this.startLoading('Filtering...', true);

const clearRange: BoundedAxisRange = Array.isArray(modelRange)
? modelRange
: [modelRange, modelRange];

this.setState(
({ advancedFilters, quickFilters }: Partial<IrisGridState>) => {
const newAdvancedFilters = advancedFilters
const newAdvancedFilters: AdvancedFilterMap = advancedFilters
? new Map(advancedFilters)
: new Map();
const newQuickFilters = quickFilters
const newQuickFilters: QuickFilterMap = quickFilters
? new Map(quickFilters)
: new Map();
newQuickFilters.delete(modelColumn);
newAdvancedFilters.delete(modelColumn);
newAdvancedFilters.forEach((_, column) => {
if (column >= clearRange[0] && column <= clearRange[1]) {
newAdvancedFilters.delete(column);
}
});
newQuickFilters.forEach((_, column) => {
if (column >= clearRange[0] && column <= clearRange[1]) {
newQuickFilters.delete(column);
}
});

return {
quickFilters: newQuickFilters,
Expand Down
16 changes: 15 additions & 1 deletion packages/iris-grid/src/IrisGridModel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable class-methods-use-this */
import type { Event, EventTarget } from 'event-target-shim';
import {
BoundedAxisRange,
DataBarGridModel,
DataBarOptions,
GridCell,
Expand Down Expand Up @@ -160,7 +161,8 @@ abstract class IrisGridModel<
abstract getColumnIndexByName(name: string): ModelIndex | undefined;

/**
* Retrieve the source cell for a given cell. Returns something different if the cell is a proxied cell that retrieves data from another cell.
* Retrieve the source cell for a given cell. Returns something different if the cell is a proxied cell
* that retrieves data from another cell.
* @param column Column to get the source for
* @param row Row to get the source for
* @returns Source cell where the data is coming from
Expand All @@ -169,6 +171,18 @@ abstract class IrisGridModel<
return { column, row };
}

/**
* Retrieve the range of columns to clear filters on for a given column.
* @param column Column to get the range of filters to clear.
* @returns Axis range of the column filters to clear, or `null` if this should not have a clear filter option.
*/
getClearFilterRange(column: ModelIndex): BoundedAxisRange | null {
if (this.isFilterable(column)) {
return [column, column];
}
return null;
}

/** List of column movements defined by the model. Used as initial movements for IrisGrid */
get initialMovedColumns(): readonly MoveOperation[] {
return EMPTY_ARRAY;
Expand Down
3 changes: 3 additions & 0 deletions packages/iris-grid/src/IrisGridProxyModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,9 @@ class IrisGridProxyModel extends IrisGridModel {
sourceForCell: IrisGridModel['sourceForCell'] = (...args) =>
this.model.sourceForCell(...args);

getClearFilterRange: IrisGridModel['getClearFilterRange'] = (...args) =>
this.model.getClearFilterRange(...args);

get description(): string {
return this.model.description;
}
Expand Down
16 changes: 15 additions & 1 deletion packages/iris-grid/src/IrisGridTreeTableModel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/* eslint class-methods-use-this: "off" */
import memoize from 'memoize-one';
import { GridCell, GridRange, ModelIndex } from '@deephaven/grid';
import {
BoundedAxisRange,
GridCell,
GridRange,
ModelIndex,
} from '@deephaven/grid';
import type {
dh as DhType,
Column,
Expand Down Expand Up @@ -191,6 +196,15 @@ class IrisGridTreeTableModel extends IrisGridTableModelTemplate<
return { column: column + depth, row };
}

getClearFilterRange(column: ModelIndex): BoundedAxisRange | null {
if (column >= this.virtualColumns.length) {
return super.getClearFilterRange(column);
}
// Source for the proxied column could be any of the grouped columns.
// Return the range of columns matching the grouped columns.
return [this.virtualColumns.length, this.groupedColumns.length];
}

get hasExpandableRows(): boolean {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -743,18 +743,27 @@ class IrisGridContextMenuHandler extends GridMouseHandler {
}
}

if (model.isFilterable(sourceColumn)) {
const clearFilterRange = model.getClearFilterRange(modelColumn);
if (clearFilterRange != null && clearFilterRange.length > 0) {
// Clear column filter should still be available after last row
// And should be available in both header and body context menus
actions.push({
title: 'Clear Column Filter',
title:
clearFilterRange[1] - clearFilterRange[0] > 0
? 'Clear Group Filter'
: 'Clear Column Filter',
group: IrisGridContextMenuHandler.GROUP_FILTER,
order: 30,
action: () => {
this.irisGrid.removeColumnFilter(sourceColumn);
this.irisGrid.removeColumnFilter(clearFilterRange);
},
disabled: !(
quickFilters.has(sourceColumn) || advancedFilters.has(sourceColumn)
Array.from(quickFilters.keys()).some(
col => col >= clearFilterRange[0] && col <= clearFilterRange[1]
) ||
Array.from(advancedFilters.keys()).some(
col => col >= clearFilterRange[0] && col <= clearFilterRange[1]
)
),
});
}
Expand Down

0 comments on commit 65174d6

Please sign in to comment.