Glide Data Grid
  • 👋Welcome to Glide Data Grid
  • 🚀Extended QuickStart Guide
    • ✏️Editing Data
    • 🖱️Working with selections
    • 🔨Grid Columns
    • 📎Copy and paste support
  • 📒FAQ
  • 📚API
    • DataEditor
      • Required Props
      • Important Props
      • Row Markers
      • Editing
      • Input Interaction
      • Selection Handling
      • Custom Cells
      • Drag and Drop
      • Search
      • Styling
    • DataEditorCore
    • Cells
      • BaseGridCell
      • TextCell
      • BooleanCell
      • NumberCell
      • UriCell
      • ProtectedCell
      • RowIDCell
      • LoadingCell
      • ImageCell
      • MarkdownCell
      • BubbleCell
      • DrilldownCell
    • Common Types
    • DataEditorRef
  • Guides
    • Implementing Custom Cells
Powered by GitBook
On this page

Was this helpful?

  1. Extended QuickStart Guide

Working with selections

PreviousEditing DataNextGrid Columns

Last updated 1 year ago

Was this helpful?

Glide Data Grid selections are controlled via the and properties. To respond to selection changes the gridSelection must be controlled.

const [selection, setSelection] = React.useState<GridSelection>({
    columns: CompactSelection.empty(),
    rows: CompactSelection.empty(),
});

return <DataEditor
    {...otherProps}
    gridSelection={selection}
    onGridSelectionChange={setSelection}
/>

The contains three different elements. The current selection, the selected rows, and the selected columns.

interface GridSelection {
    readonly current?: {
        readonly cell: Item;
        readonly range: Readonly<Rectangle>;
        readonly rangeStack: readonly Readonly<Rectangle>[];
    };
    readonly columns: CompactSelection;
    readonly rows: CompactSelection;
}

rows and columns are both a CompactSelection. A compact selection is a sparse array designed to efficiently hold spans of selected elements. They have the following interface:

export class CompactSelection 
    static empty(): CompactSelection;
    static fromSingleSelection(selection: number | Slice): CompactSelection;
    
    public offset(amount: number): CompactSelection;
    public add(selection: number | Slice): CompactSelection;
    public remove(selection: number | Slice): CompactSelection;
    public first(): number | undefined;
    public last(): number | undefined;
    public hasIndex(index: number): boolean;
    public hasAll(index: Slice): boolean;
    public some(predicate: (index: number) => boolean): boolean;
    public equals(other: CompactSelection): boolean;
    public toArray(): number[];
    get length(): number;
    *[Symbol.iterator]()
}

In general, it is advisable not to call toArray. Iterating over the CompactSelection using a for ... of loop is significantly more efficient.

The current part of the selection contains the selected cell. This is the item which will be drawn with a focus ring. The range represents the selected region around the cell that may be edited by certain commands such as delete. Finally, the rangeStack are additional ranges that the user has selected when selection mode is enabled.

🚀
🖱️
multi-rect
gridSelection
onGridSelectionChanged
GridSelection