Vivid API

Data Grid

Allows users to interact with data in a tabular format.

Usage

import { registerDataGrid } from '@vonage/vivid-api';
registerDataGrid('your-prefix');

Data Grid Row and Data Grid Cell sub-components are registered automatically in the same function.


Remove From Tab Order

Use the no-tabbing attribute to remove the component from the tab order.

Setting Focus on a Cell

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.

Cell Click Event

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 }

Rows Data

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).

Column Definitions

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.

Generate Header

Use the generate-header property to programmatically define the type of grid header that is generated when using rows-data.

Row, Cell and Header Cell Templates

The ViewTemplates used to render rows, cells and header cells can be customised using the following properties:

  • rowItemTemplate
  • cellItemTemplate
  • headerCellItemTemplate

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>

Row Element Tag

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.

CSS Variables

Row Background

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.

Cell Background

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.

Block Size

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.

White Space

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).

API Reference

data-grid

Properties

PropertyTypeDefaultDescription
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-api#VwcDataGridCellElement) => HTMLElement); cellTemplate?: @microsoft/fast-element#ViewTemplate<any, any>; cellInternalFocusQueue?: false | true; cellFocusTargetCallback?: ((cell: @vonage/vivid-api#VwcDataGridCellElement) => HTMLElement); isRowHeader?: false | true}[]Configure the grid header columns
fixedColumnsnumberSets the number of columns fixed to the left when horizontal scrolling
focusColumnIndexnumberIndex of column to be focused on when the Data Grid receives focus
focusRowIndexnumberIndex 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
gridTemplateColumnsstringString 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
noTabbingbooleanRemove the grid from the tab order
rowElementTagstringElement tag for header rows
rowItemTemplate@microsoft/fast-element#ViewTemplate<any, any>Templete to use when rendering rows
rowsDataobject[]Content of the grid in data format
selectionMode'none' | 'single-row' | 'multi-row' | 'single-cell' | 'multi-cell'Set the selection mode
PropertyTypeDefaultDescription
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-api#VwcDataGridCellElement) => HTMLElement); cellTemplate?: @microsoft/fast-element#ViewTemplate<any, any>; cellInternalFocusQueue?: false | true; cellFocusTargetCallback?: ((cell: @vonage/vivid-api#VwcDataGridCellElement) => HTMLElement); isRowHeader?: false | true}[]Configure the grid header columns
fixed-columnsnumberSets the number of columns fixed to the left when horizontal scrolling
focusColumnIndex
(property only)
numberIndex of column to be focused on when the Data Grid receives focus
focusRowIndex
(property only)
numberIndex 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-columnsstringString 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-tabbingbooleanRemove the grid from the tab order
rowElementTag
(property only)
stringElement 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

Slots

NameDescription
defaultDefault slot.
NameDescription
defaultDefault slot.

Events

NameTypeDescription
cell-clickCustomEvent<{cell: HTMLElement, row: HTMLElement, isHeaderCell: boolean, columnDataKey: string}>Event that fires when a cell is clicked
NameTypeDescription
cell-clickCustomEvent<{cell: HTMLElement, row: HTMLElement, isHeaderCell: boolean, columnDataKey: string}>Event that fires when a cell is clicked

data-grid-row

Properties

PropertyTypeDefaultDescription
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-api#VwcDataGridCellElement) => HTMLElement); cellTemplate?: @microsoft/fast-element#ViewTemplate<any, any>; cellInternalFocusQueue?: false | true; cellFocusTargetCallback?: ((cell: @vonage/vivid-api#VwcDataGridCellElement) => HTMLElement); isRowHeader?: false | true}[]The column definitions of the row
gridTemplateColumnsstringString 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.
rowDataobjectThe base data for this row
rowIndexnumberThe 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
selectedfalse | trueReflects selected state of the row
PropertyTypeDefaultDescription
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-api#VwcDataGridCellElement) => HTMLElement); cellTemplate?: @microsoft/fast-element#ViewTemplate<any, any>; cellInternalFocusQueue?: false | true; cellFocusTargetCallback?: ((cell: @vonage/vivid-api#VwcDataGridCellElement) => HTMLElement); isRowHeader?: false | true}[]The column definitions of the row
grid-template-columnsstringString 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)
objectThe base data for this row
rowIndex
(property only)
numberThe 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
selectedfalse | trueReflects selected state of the row

Slots

NameDescription
defaultDefault slot.
NameDescription
defaultDefault slot.

Events

NameTypeDescription
cell-clickCustomEvent<{cell: HTMLElement, row: HTMLElement, isHeaderCell: boolean, columnDataKey: string}>Event that fires when a cell is clicked
row-focusedCustomEvent<HTMLElement>Fires a custom 'row-focused' event when focus is on an element (usually a cell or its contents) in the row
NameTypeDescription
cell-clickCustomEvent<{cell: HTMLElement, row: HTMLElement, isHeaderCell: boolean, columnDataKey: string}>Event that fires when a cell is clicked
row-focusedCustomEvent<HTMLElement>Fires a custom 'row-focused' event when focus is on an element (usually a cell or its contents) in the row

data-grid-cell

Properties

PropertyTypeDefaultDescription
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-api#VwcDataGridCellElement) => HTMLElement); cellTemplate?: @microsoft/fast-element#ViewTemplate<any, any>; cellInternalFocusQueue?: false | true; cellFocusTargetCallback?: ((cell: @vonage/vivid-api#VwcDataGridCellElement) => HTMLElement); isRowHeader?: false | true}The base data for the column
gridColumnstringThe column index of the cell. This will be applied to the css grid-column-index value applied to the cell
rowDataobjectThe base data for the parent row
selectedbooleanReflects selected state of the row
sortDirection'none' | 'ascending' | 'descending' | 'other'Sets the sorting direction.
PropertyTypeDefaultDescription
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-api#VwcDataGridCellElement) => HTMLElement); cellTemplate?: @microsoft/fast-element#ViewTemplate<any, any>; cellInternalFocusQueue?: false | true; cellFocusTargetCallback?: ((cell: @vonage/vivid-api#VwcDataGridCellElement) => HTMLElement); isRowHeader?: false | true}The base data for the column
grid-columnstringThe column index of the cell. This will be applied to the css grid-column-index value applied to the cell
rowData
(property only)
objectThe base data for the parent row
selectedbooleanReflects selected state of the row
sort-direction'none' | 'ascending' | 'descending' | 'other'Sets the sorting direction.

Slots

NameDescription
action-itemsAdd action items to the cell using this slot.
defaultDefault slot.
NameDescription
action-itemsAdd action items to the cell using this slot.
defaultDefault slot.

Events

NameTypeDescription
cell-clickCustomEvent<{cell: HTMLElement, row: HTMLElement, isHeaderCell: boolean, columnDataKey: string}>Event that fires when a cell is clicked
cell-focusedCustomEvent<HTMLElement>Fires a custom 'cell-focused' event when focus is on the cell or its contents
sortCustomEvent<{columnDataKey: string, ariaSort: string | null, sortDirection: string | null}>Event that fires when a sortable column header is clicked
NameTypeDescription
cell-clickCustomEvent<{cell: HTMLElement, row: HTMLElement, isHeaderCell: boolean, columnDataKey: string}>Event that fires when a cell is clicked
cell-focusedCustomEvent<HTMLElement>Fires a custom 'cell-focused' event when focus is on the cell or its contents
sortCustomEvent<{columnDataKey: string, ariaSort: string | null, sortDirection: string | null}>Event that fires when a sortable column header is clicked