CxJS

Column Resizing

Grid supports interactive column resizing. Users can drag column borders to adjust widths.

<div controller={PageController}>
  <Grid
    records={m.records}
    style="height: 250px; margin-bottom: 10px"
    scrollable
    border
    onRef={(el, instance) => {
      instance.getControllerByType(PageController).gridInstance = instance
    }}
    columns={[
      {
        header: {
          text: "Name",
          width: m.colWidth.fullName,
          resizable: true,
          defaultWidth: 200,
        },
        field: "fullName",
        sortable: true,
      },
      {
        header: "Continent",
        width: m.colWidth.continent,
        resizable: true,
        field: "continent",
        sortable: true,
      },
      {
        header: "Browser",
        width: m.colWidth.browser,
        resizable: true,
        field: "browser",
        sortable: true,
      },
      {
        header: "OS",
        width: m.colWidth.os,
        resizable: true,
        field: "os",
        sortable: true,
      },
      {
        header: "Visits",
        width: m.colWidth.visits,
        resizable: false,
        field: "visits",
        sortable: true,
        align: "right",
      },
    ]}
  />
  <Button
    text="Reset column widths"
    onClick={(e, instance) => {
      instance.getControllerByType(PageController).resetColumnWidths()
    }}
  />
</div>
Name
Continent
Browser
OS
Visits
Alice JohnsonEuropeChromeWindows45
Bob SmithAsiaFirefoxmacOS23
Carol WhiteNorth AmericaSafarimacOS67
David BrownEuropeChromeLinux12
Eva GreenAsiaEdgeWindows89

Drag the column borders to resize. Double-click a border to auto-fit the column to its content.

Enabling Resizing

Set resizable: true on a column to enable resizing:

{
  header: "Name",
  field: "fullName",
  resizable: true,
}

Persisting Column Widths

Use a two-way binding for the width property to store column widths. Use defaultWidth for the initial width:

{
  header: {
    text: "Name",
    width: m.colWidth.fullName,
    resizable: true,
    defaultWidth: 200,
  },
  field: "fullName",
}

When using complex headers, width, resizable, and defaultWidth should be defined in the header object.

Resetting Column Widths

To reset column widths to defaults, clear both the store binding and the Grid’s internal state:

<Button
  text="Reset column widths"
  onClick={(e, instance) => {
    // Get grid instance via controller
    controller.gridInstance.setState({ colWidth: {} });
    store.delete("colWidth");
  }}
/>

Use onRef to capture the Grid instance for state manipulation:

<Grid
  onRef={(el, instance) => {
    instance.getControllerByType(PageController).gridInstance = instance;
  }}
  // ...
/>

Configuration

PropertyTypeDescription
resizablebooleanEnable column resizing.
widthnumberCurrent column width in pixels. Can be bound to persist changes.
defaultWidthnumberInitial column width before any resizing.

See also: Column Reordering, Grid