Skip to content

Commit

Permalink
docs: fix disabled draggable columns examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mgadewoll committed Dec 12, 2024
1 parent 351bc8f commit cff1f95
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default () => {
columnVisibility={{
visibleColumns: visibleColumns,
setVisibleColumns: setVisibleColumns,
canDragAndDropColumns: true,
}}
rowCount={data.length}
renderCellValue={({ rowIndex, columnId }) => data[rowIndex][columnId]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,105 @@ export default () => {

```

## Draggable columns

To reorder columns directly instead of via the actions menu popover, you can enable draggable EuiDataGrid header columns via the `columnVisibility.canDragAndDropColumns` prop.

This will allow you to reorder the column by dragging them.

```tsx interactive
import React, { useState, useCallback } from 'react';
import { faker } from '@faker-js/faker';

import { EuiDataGrid, EuiAvatar } from '@elastic/eui';

const columns = [
{
id: 'avatar',
initialWidth: 40,
isResizable: false,
},
{
id: 'name',
initialWidth: 100,
},
{
id: 'email',
},
{
id: 'city',
},
{
id: 'country',
},
{
id: 'account',
},
];

const data = [];

for (let i = 1; i < 5; i++) {
data.push({
avatar: (
<EuiAvatar
size="s"
name={`${faker.person.lastName()}, ${faker.person.firstName()}`}
/>
),
name: `${faker.person.lastName()}, ${faker.person.firstName()} ${faker.person.suffix()}`,
email: faker.internet.email(),
city: faker.location.city(),
country: faker.location.country(),
account: faker.finance.accountNumber(),
});
}

export default () => {
const [pagination, setPagination] = useState({ pageIndex: 0 });

const [visibleColumns, setVisibleColumns] = useState(
columns.map(({ id }) => id)
);

const setPageIndex = useCallback(
(pageIndex) =>
setPagination((pagination) => ({ ...pagination, pageIndex })),
[]
);
const setPageSize = useCallback(
(pageSize) =>
setPagination((pagination) => ({
...pagination,
pageSize,
pageIndex: 0,
})),
[]
);

return (
<EuiDataGrid
aria-label="DataGrid demonstrating column sizing constraints"
columns={columns}
columnVisibility={{
visibleColumns: visibleColumns,
setVisibleColumns: setVisibleColumns,
canDragAndDropColumns: true,
}}
rowCount={data.length}
renderCellValue={({ rowIndex, columnId }) => data[rowIndex][columnId]}
pagination={{
...pagination,
onChangeItemsPerPage: setPageSize,
onChangePage: setPageIndex,
}}
/>
);
};
```



## Control columns

Control columns can be used to include ancillary cells not based on the dataset, such as row selection checkboxes or action buttons. These columns can be placed at either side of the data grid, and users are unable to resize, sort, or rearrange them.
Expand Down

0 comments on commit cff1f95

Please sign in to comment.