LyteNyte Grid logo for light mode. Links back to the documentation home page.
Columns

Column Moving

Columns in LyteNyte Grid may be reordered programmatically or through the grid's drag-and-drop header interactions. Column groups can also be reordered as a unit.

Move By Dragging

LyteNyte Grid supports column reordering through the movable property on each column definition. Set movable to true to allow users to drag a column header to a new position. Click and hold to drag a header in the demo below:

Move By Dragging

Fork code on stack blitzFork code on code sandbox

Pinned Columns & Drag Reordering

When the grid has both pinned and scrollable columns, drag-and-drop reordering updates the column’s pin state depending on where you drop it.

  • Moving an unpinned column over a pinned column causes the moved column to become pinned.
  • Moving a pinned column over an unpinned one unpins the moved column.

Crossing the pinned or scrollable boundary when moving columns changes the pin state. However, reordering columns within the same section without crossing the boundary leaves it unchanged. The demo below shows this boundary-based behavior.

Moving Pinned Columns

Fork code on stack blitzFork code on code sandbox

Dragging Column Groups

Columns in the same group can be dragged together. All columns in the group must be movable for group dragging to work. In the demo below, dragging the Inventory group moves both the Product and Price columns:

Moving Column Groups

Fork code on stack blitzFork code on code sandbox

Moving grouped columns can change the group hierarchy:

  • If you drag a column out of its group, LyteNyte splits the group.
  • If two columns belong to the same group but are separated in the header hierarchy, dragging them next to each other merges the header hierarchy.
  • Reordering columns within the same group behaves the same as reordering ungrouped columns.
  • If you move an ungrouped column between columns in a group, LyteNyte splits the group.

See the Column Groups guide for details on when groups split and how LyteNyte Grid handles column groups.

Drag Placeholder

When column dragging begins, the browser takes an image snapshot of the column being dragged. This is native browser behavior. LyteNyte Grid lets you customize the drag placeholder. The grid exposes two properties:

  • columnMoveDragPlaceholder: Determines the placeholder used when dragging column headers.
  • columnGroupMoveDragPlaceholder: Determines the placeholder used when dragging column group headers.

The placeholder can be one of the following:

  • String: A CSS selector that targets an element to snapshot as the drag image.
  • Query Object: An object that includes a CSS selector string and an (x,y) offset that positions the snapshot image.
  • Component Placeholder: A React component that renders the placeholder.

Note

When you use a Component Placeholder, you disable the browser’s native drag-image snapshot. LyteNyte Grid provides the client coordinates for the drag event, and your code must render the placeholder correctly at those coordinates.

LyteNyte Grid mounts the placeholder within the header component in the React tree, so you must use a React portal to render the placeholder into the target DOM container.

In the demo below, a custom React component renders the drag placeholder for column headers and column group headers.

Column Placeholder Dragging

Fork code on stack blitzFork code on code sandbox

Using a React component instead of a query selector or default drag behavior has differences to note, even if not obvious from the drag examples.

Native Placeholder (Query Selector)

  • Uses the browser’s drag-and-drop API.
  • Snapshots the dragged element when the drag starts.
  • Uses a static image for the entire drag that cannot be updated after the snapshot is taken.

React Component Placeholder

  • Re-renders every time the cursor moves.
  • You can update the placeholder’s visual output during the drag.
  • Runs significantly slower than native placeholder approach

Use a React component placeholder only when you need dynamic visuals. Otherwise, prefer query selectors and elements for drag placeholders.

Column Dropped Outside Viewport

LyteNyte Grid detects when a moved column or column group is dropped outside the grid. When this happens, the grid calls the onColumnMoveOutside handler (if provided). You can use this handler to determine whether a column move ended outside the grid.

If you want to hide a column when it is dropped outside the grid, attach a callback to onColumnMoveOutside to hide the moved columns, as demonstrated in the demo below.

Outside Drag-to-Hide

Fork code on stack blitzFork code on code sandbox

Tip

LyteNyte Grid supports hiding columns when users drag them outside the grid, but we do not recommend implementing this behavior. Our experience shows that users find this behavior unintuitive and easy to get wrong. A better approach is to use a dedicated component that manages column visibility, such as our Pill Manager component or our Column Manager component.

Programmatic Column Reordering

Use api.columnMove to reorder columns programmatically. Column order is determined by the sequence of columns in the grid state, and api.columnMove updates columns property accordingly. For example, the code below swaps the ID and Product columns:

api.columnMove({ moveColumns: ["product"], moveTarget: "id", before: false });

For more details, see the Grid API reference.

Next Steps

  • Column Base: Learn how to specify default configuration options for columns.
  • Column Resizing: Change column widths programmatically or via user interaction.
  • Column Pinning: Pin columns to the start or end of the viewport.
  • Column Groups: Create a column header hierarchy and associate columns with column groups.