Data Grid
Allows users to interact with data in a tabular format.
import { registerDataGrid } from '@vonage/vivid';
registerDataGrid('your-prefix');
Data Grid Row and Data Grid Cell sub-components are registered automatically in the same function.
Use the no-tabbing attribute to remove the component from the tab order.
Use the focusRowIndex and focusColumnIndex to determine which row or cell to set focus on when the Data Grid component receieves focus.
In the example below, change the value of the row / column index and then tab into the Data Grid.
The cell-click event is fired when a cell is clicked on or when the enter or space key is pressed on a focused cell.
Event details: { cell, row, isHeaderCell, columnDataKey }
Use the rowsData property to provide the component with the data (an array or objects) to be displayed.
If not used in conjuction with column defintions (title), text displayed in the column headers will be the data keys of the object.
The rowsData property provides an alternative way to populate the data grid with content.
However, we recommend using the Data Grid Row and Data Grid Cell sub-components to construct the content declaratively. This approach offers greater flexibility, allowing you to incorporate non-text-based elements such as other components or HTML elements (see Select in grid use case).
Use the columnDefinitions property to programmatically configure the column headers that are generated when using rows-data. See the ColumnDefinition interface for more information.
The sortable feature doesn't actually sort the data, it only changes the visual representation of the column header. See the sorting use case for more information.
Use the generate-header property to programmatically define the type of grid header that is generated when using rows-data.
The ViewTemplates used to render rows, cells and header cells can be customised using the following properties:
rowItemTemplatecellItemTemplateheaderCellItemTemplate
You need to use html from fast-element.
<vwc-data-grid class="data-grid"></vwc-data-grid>
<script>
import { html } from '@microsoft/fast-element';
const grid = document.querySelector('.data-grid');
grid.rowItemTemplate = html`<div>All rows will look like me!</div>`;
grid.cellItemTemplate = html`<div>All cells will look like me!</div>`;
grid.headerCellItemTemplate = html`<div>All header cells will look like me!</div>`;
grid.rowsData = [
{ data1: 'data11', data2: 'data12' },
{ data1: 'data21', data2: 'data22' },
];
</script>
Use the rowElementTag to set the element tag for header row cells. If not set, the default tag vwc-data-grid-cell will be used.
When Row is set to sticky there's a default canvas background-color.
Use --data-grid-row-background to change the sticky row background-color.
When a grid has the fixed-columns attribute, fixed columns cells have a default canvas background-color.
Use --data-grid-cell-background to overwrite the default background-color.
Use --data-grid-cell-block-size to change the cell's block-size.
By default, header cells have a fixed height while data cells have a dynamic height based on content.
Use --data-grid-cell-white-space to change the cell's white-space.
By default, header cells will not wrap text (nowrap), while data cells will wrap text (normal).
| Property | Type | Default | Description |
|---|---|---|---|
| cellItemTemplate | @microsoft/fast-element#ViewTemplate<any, any> |
Templete to use when rendering cells | |
| columnDefinitions | {columnDataKey: string; gridColumn?: string; title?: string; headerCellTemplate?: @microsoft/fast-element#ViewTemplate<any, any>; headerCellInternalFocusQueue?: false | true; headerCellFocusTargetCallback?: ((cell: @vonage/vivid#VwcDataGridCellElement) => HTMLElement); cellTemplate?: @microsoft/fast-element#ViewTemplate<any, any>; cellInternalFocusQueue?: false | true; cellFocusTargetCallback?: ((cell: @vonage/vivid#VwcDataGridCellElement) => HTMLElement); isRowHeader?: false | true}[] |
Configure the grid header columns | |
| fixedColumns | number |
Sets the number of columns fixed to the left when horizontal scrolling | |
| focusColumnIndex | number |
Index of column to be focused on when the Data Grid receives focus | |
| focusRowIndex | number |
Index of row to be focused on when the Data Grid receives focus | |
| generateHeader | 'none' | 'default' | 'sticky' |
Whether the grid should automatically generate a header row and its type | |
| gridTemplateColumns | string |
String that gets applied to the css gridTemplateColumns attribute of child rows | |
| headerCellItemTemplate | @microsoft/fast-element#ViewTemplate<any, any> |
Templete to use when rendering grid header cells | |
| noTabbing | boolean |
Remove the grid from the tab order | |
| rowElementTag | string |
Element tag for header rows | |
| rowItemTemplate | @microsoft/fast-element#ViewTemplate<any, any> |
Templete to use when rendering rows | |
| rowsData | object[] |
Content of the grid in data format | |
| selectionMode | 'none' | 'single-row' | 'multi-row' | 'single-cell' | 'multi-cell' |
Set the selection mode |
| Property | Type | Default | Description |
|---|---|---|---|
|
cellItemTemplate
(property only) |
@microsoft/fast-element#ViewTemplate<any, any> |
Templete to use when rendering cells | |
|
columnDefinitions
(property only) |
{columnDataKey: string; gridColumn?: string; title?: string; headerCellTemplate?: @microsoft/fast-element#ViewTemplate<any, any>; headerCellInternalFocusQueue?: false | true; headerCellFocusTargetCallback?: ((cell: @vonage/vivid#VwcDataGridCellElement) => HTMLElement); cellTemplate?: @microsoft/fast-element#ViewTemplate<any, any>; cellInternalFocusQueue?: false | true; cellFocusTargetCallback?: ((cell: @vonage/vivid#VwcDataGridCellElement) => HTMLElement); isRowHeader?: false | true}[] |
Configure the grid header columns | |
| fixed-columns | number |
Sets the number of columns fixed to the left when horizontal scrolling | |
|
focusColumnIndex
(property only) |
number |
Index of column to be focused on when the Data Grid receives focus | |
|
focusRowIndex
(property only) |
number |
Index of row to be focused on when the Data Grid receives focus | |
| generate-header | 'none' | 'default' | 'sticky' |
Whether the grid should automatically generate a header row and its type | |
| grid-template-columns | string |
String that gets applied to the css gridTemplateColumns attribute of child rows | |
|
headerCellItemTemplate
(property only) |
@microsoft/fast-element#ViewTemplate<any, any> |
Templete to use when rendering grid header cells | |
| no-tabbing | boolean |
Remove the grid from the tab order | |
|
rowElementTag
(property only) |
string |
Element tag for header rows | |
|
rowItemTemplate
(property only) |
@microsoft/fast-element#ViewTemplate<any, any> |
Templete to use when rendering rows | |
|
rowsData
(property only) |
object[] |
Content of the grid in data format | |
| selection-mode | 'none' | 'single-row' | 'multi-row' | 'single-cell' | 'multi-cell' |
Set the selection mode |
| Name | Description |
|---|---|
| default | Default slot. |
| Name | Description |
|---|---|
| default | Default slot. |
| Event | Type | Description |
|---|---|---|
| blur | FocusEvent |
Fires when the element loses focus. |
| cell-click | CustomEvent<{cell: HTMLElement, row: HTMLElement, isHeaderCell: boolean, columnDataKey: string}> |
Event that fires when a cell is clicked |
| click | MouseEvent |
Fires when a pointing device button (such as a mouse's primary mouse button) is both pressed and released while the pointer is located inside the element. |
| focus | FocusEvent |
Fires when the element receives focus. |
| input | Event |
Fires when the value of an element has been changed. |
| keydown | KeyboardEvent |
Fires when a key is pressed. |
| keyup | KeyboardEvent |
Fires when a key is released. |
| Name | Type | Description |
|---|---|---|
| blur | FocusEvent |
Fires when the element loses focus. |
| cell-click | CustomEvent<{cell: HTMLElement, row: HTMLElement, isHeaderCell: boolean, columnDataKey: string}> |
Event that fires when a cell is clicked |
| click | MouseEvent |
Fires when a pointing device button (such as a mouse's primary mouse button) is both pressed and released while the pointer is located inside the element. |
| focus | FocusEvent |
Fires when the element receives focus. |
| input | Event |
Fires when the value of an element has been changed. |
| keydown | KeyboardEvent |
Fires when a key is pressed. |
| keyup | KeyboardEvent |
Fires when a key is released. |
| Property | Type | Default | Description |
|---|---|---|---|
| cellItemTemplate | @microsoft/fast-element#ViewTemplate<any, any> |
The template used to render cells in generated rows. | |
| columnDefinitions | {columnDataKey: string; gridColumn?: string; title?: string; headerCellTemplate?: @microsoft/fast-element#ViewTemplate<any, any>; headerCellInternalFocusQueue?: false | true; headerCellFocusTargetCallback?: ((cell: @vonage/vivid#VwcDataGridCellElement) => HTMLElement); cellTemplate?: @microsoft/fast-element#ViewTemplate<any, any>; cellInternalFocusQueue?: false | true; cellFocusTargetCallback?: ((cell: @vonage/vivid#VwcDataGridCellElement) => HTMLElement); isRowHeader?: false | true}[] |
The column definitions of the row | |
| gridTemplateColumns | string |
String that gets applied to the the css gridTemplateColumns attribute for the row x | |
| headerCellItemTemplate | @microsoft/fast-element#ViewTemplate<any, any> |
The template used to render header cells in generated rows. | |
| rowData | object |
The base data for this row | |
| rowIndex | number |
The index of the row in the parent grid. This is typically set programmatically by the parent grid. | |
| rowType | 'header' | 'default' | 'sticky-header' |
The type of row | |
| selected | false | true |
Reflects selected state of the row |
| Property | Type | Default | Description |
|---|---|---|---|
|
cellItemTemplate
(property only) |
@microsoft/fast-element#ViewTemplate<any, any> |
The template used to render cells in generated rows. | |
|
columnDefinitions
(property only) |
{columnDataKey: string; gridColumn?: string; title?: string; headerCellTemplate?: @microsoft/fast-element#ViewTemplate<any, any>; headerCellInternalFocusQueue?: false | true; headerCellFocusTargetCallback?: ((cell: @vonage/vivid#VwcDataGridCellElement) => HTMLElement); cellTemplate?: @microsoft/fast-element#ViewTemplate<any, any>; cellInternalFocusQueue?: false | true; cellFocusTargetCallback?: ((cell: @vonage/vivid#VwcDataGridCellElement) => HTMLElement); isRowHeader?: false | true}[] |
The column definitions of the row | |
| grid-template-columns | string |
String that gets applied to the the css gridTemplateColumns attribute for the row x | |
|
headerCellItemTemplate
(property only) |
@microsoft/fast-element#ViewTemplate<any, any> |
The template used to render header cells in generated rows. | |
|
rowData
(property only) |
object |
The base data for this row | |
|
rowIndex
(property only) |
number |
The index of the row in the parent grid. This is typically set programmatically by the parent grid. | |
| row-type | 'header' | 'default' | 'sticky-header' |
The type of row | |
| selected | false | true |
Reflects selected state of the row |
| Name | Description |
|---|---|
| default | Default slot. |
| Name | Description |
|---|---|
| default | Default slot. |
| Event | Type | Description |
|---|---|---|
| blur | FocusEvent |
Fires when the element loses focus. |
| cell-click | CustomEvent<{cell: HTMLElement, row: HTMLElement, isHeaderCell: boolean, columnDataKey: string}> |
Event that fires when a cell is clicked |
| click | MouseEvent |
Fires when a pointing device button (such as a mouse's primary mouse button) is both pressed and released while the pointer is located inside the element. |
| focus | FocusEvent |
Fires when the element receives focus. |
| input | Event |
Fires when the value of an element has been changed. |
| keydown | KeyboardEvent |
Fires when a key is pressed. |
| keyup | KeyboardEvent |
Fires when a key is released. |
| row-focused | CustomEvent<HTMLElement> |
Fires a custom 'row-focused' event when focus is on an element (usually a cell or its contents) in the row |
| Name | Type | Description |
|---|---|---|
| blur | FocusEvent |
Fires when the element loses focus. |
| cell-click | CustomEvent<{cell: HTMLElement, row: HTMLElement, isHeaderCell: boolean, columnDataKey: string}> |
Event that fires when a cell is clicked |
| click | MouseEvent |
Fires when a pointing device button (such as a mouse's primary mouse button) is both pressed and released while the pointer is located inside the element. |
| focus | FocusEvent |
Fires when the element receives focus. |
| input | Event |
Fires when the value of an element has been changed. |
| keydown | KeyboardEvent |
Fires when a key is pressed. |
| keyup | KeyboardEvent |
Fires when a key is released. |
| row-focused | CustomEvent<HTMLElement> |
Fires a custom 'row-focused' event when focus is on an element (usually a cell or its contents) in the row |
| Property | Type | Default | Description |
|---|---|---|---|
| cellType | 'default' | 'columnheader' | 'rowheader' |
The type of cell | |
| columnDefinition | {sortDirection?: null | 'none' | 'ascending' | 'descending' | 'other'; sortable?: false | true; columnDataKey: string; gridColumn?: string; title?: string; headerCellTemplate?: @microsoft/fast-element#ViewTemplate<any, any>; headerCellInternalFocusQueue?: false | true; headerCellFocusTargetCallback?: ((cell: @vonage/vivid#VwcDataGridCellElement) => HTMLElement); cellTemplate?: @microsoft/fast-element#ViewTemplate<any, any>; cellInternalFocusQueue?: false | true; cellFocusTargetCallback?: ((cell: @vonage/vivid#VwcDataGridCellElement) => HTMLElement); isRowHeader?: false | true} |
The base data for the column | |
| gridColumn | string |
The column index of the cell. This will be applied to the css grid-column-index value applied to the cell | |
| rowData | object |
The base data for the parent row | |
| selected | boolean |
Reflects selected state of the row | |
| sortDirection | 'none' | 'ascending' | 'descending' | 'other' |
Sets the sorting direction. |
| Property | Type | Default | Description |
|---|---|---|---|
| cell-type | 'default' | 'columnheader' | 'rowheader' |
The type of cell | |
|
columnDefinition
(property only) |
{sortDirection?: null | 'none' | 'ascending' | 'descending' | 'other'; sortable?: false | true; columnDataKey: string; gridColumn?: string; title?: string; headerCellTemplate?: @microsoft/fast-element#ViewTemplate<any, any>; headerCellInternalFocusQueue?: false | true; headerCellFocusTargetCallback?: ((cell: @vonage/vivid#VwcDataGridCellElement) => HTMLElement); cellTemplate?: @microsoft/fast-element#ViewTemplate<any, any>; cellInternalFocusQueue?: false | true; cellFocusTargetCallback?: ((cell: @vonage/vivid#VwcDataGridCellElement) => HTMLElement); isRowHeader?: false | true} |
The base data for the column | |
| grid-column | string |
The column index of the cell. This will be applied to the css grid-column-index value applied to the cell | |
|
rowData
(property only) |
object |
The base data for the parent row | |
| selected | boolean |
Reflects selected state of the row | |
| sort-direction | 'none' | 'ascending' | 'descending' | 'other' |
Sets the sorting direction. |
| Name | Description |
|---|---|
| action-items | Add action items to the cell using this slot. |
| default | Default slot. |
| Name | Description |
|---|---|
| action-items | Add action items to the cell using this slot. |
| default | Default slot. |
| Event | Type | Description |
|---|---|---|
| blur | FocusEvent |
Fires when the element loses focus. |
| cell-click | CustomEvent<{cell: HTMLElement, row: HTMLElement, isHeaderCell: boolean, columnDataKey: string}> |
Event that fires when a cell is clicked |
| cell-focused | CustomEvent<HTMLElement> |
Fires a custom 'cell-focused' event when focus is on the cell or its contents |
| click | MouseEvent |
Fires when a pointing device button (such as a mouse's primary mouse button) is both pressed and released while the pointer is located inside the element. |
| focus | FocusEvent |
Fires when the element receives focus. |
| input | Event |
Fires when the value of an element has been changed. |
| keydown | KeyboardEvent |
Fires when a key is pressed. |
| keyup | KeyboardEvent |
Fires when a key is released. |
| sort | CustomEvent<{columnDataKey: string, ariaSort: string | null, sortDirection: string | null}> |
Event that fires when a sortable column header is clicked |
| Name | Type | Description |
|---|---|---|
| blur | FocusEvent |
Fires when the element loses focus. |
| cell-click | CustomEvent<{cell: HTMLElement, row: HTMLElement, isHeaderCell: boolean, columnDataKey: string}> |
Event that fires when a cell is clicked |
| cell-focused | CustomEvent<HTMLElement> |
Fires a custom 'cell-focused' event when focus is on the cell or its contents |
| click | MouseEvent |
Fires when a pointing device button (such as a mouse's primary mouse button) is both pressed and released while the pointer is located inside the element. |
| focus | FocusEvent |
Fires when the element receives focus. |
| input | Event |
Fires when the value of an element has been changed. |
| keydown | KeyboardEvent |
Fires when a key is pressed. |
| keyup | KeyboardEvent |
Fires when a key is released. |
| sort | CustomEvent<{columnDataKey: string, ariaSort: string | null, sortDirection: string | null}> |
Event that fires when a sortable column header is clicked |