2.45.0 • Published 2 months ago

@vonage/vwc-data-grid v2.45.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 months ago

vwc-data-grid

vwc-data-grid component provides a tabular data view.

As in opposite to a simple HTML table, this component is performance oriented, capable of handling large amounts of data efficiently and fully featured from both, API and UX/UI perspective.

Highlights:

  • static (all upfront) and dynamic (chunks on demand) data provisioning
  • sorting / filtering
  • tree view (group view)
  • expanded row view
  • rows selection (one / all / partial)
  • columns management - hiding - reordering - resizing - freezing
  • customization - columns management features opt in/out - header custom rendering - footer custom rendering - cell custom rendering - full row (aka row details, aka expanded row) custom rendering

API

Grid API may roughly split into 2 categories:

  • configuration / customization
  • data

Additionally there is a set of APIs of more general nature, like Events API.

Data API

More formal description of these APIs found below in the Grid Configuration / Customization / Management section.

There are 2 ways to supply data to grid, via the following grid component properties:

  • items: unknown[] - simplest - all data upfront - suitable for small to medium amounts of data (in terms of memory occupation)
  • dataProvider: (params: { page: number, pageSize: number }, callback: (pageItems: unknown[], totalItems: number) => void) => void - stream of chunks, on demand - should be used when memory usage concern present (from the data perspective) - should be used when pulling data from the backend on the fly

In case of collision between those 2 (both set to something contentful), error will be printed to the console and dataProvider will take precedence.

Some changes to the data, eg 'deep' change within items or logical conditions that dataProvider works with, won't trigger the refresh of the grid. Use API below to refresh the data:

  • refreshData(): void - will re-render the visible data in the grid
items

Each new assignment to grid's items property will refresh the grid's content.

Array manipulation (eg grid.items.splice(0, 1)) as well is 'deep' data mutations wont't trigger grid update. In such a cases you need to trigger data refresh on demand, via refreshData API.

dataProvider

Grid will call dataProvider each time new chunk of data needed. First argument will hold all needed chunk params, page number, page size etc. Second argument is the grid's own callback to be called with the fetched / prepared data.

Similarly to the said above, in case the internal conditions changing and you'd like to refresh the data in the grid, call refreshData API.

Selection API

There are few APIs to manage items selection:

  • selectedItems: unknown[] - an Array of selected items (item references taken from the items or those provided by dataProvider)
  • selectItem(item: unknown, singleSelectMode: boolean = false): void - will add the item to the selected ones (and reflect it in UI if selector column used, or any custom UI that reflects selection) - if the singleSelectMode switch set to true, the API will unselect all previously selected items and leave the provided item as the only selected one
  • unselectItem(item: unkown): void - will remove item from the selected ones
  • selectAll(): void - will add the item to the selected ones (and reflect it in UI if selector column used, or any custom UI that reflects selection); this method will throw if the data provisioning is done via dataProvider method
  • unselectAll(): void - will unselect all selected items
  • event selected-items-changed will be fired on any selection change
Selector UI column

In addition to the selection APIs, vwc-data-grid provides an OOTB selector UI, column that:

  • will auto render checkbox per row, this checkbox will add/remove corresponding item to/from selection
  • will auto render checkbox header to perform select / deselect all - this checkbox will reflect the current selection state, being in indeterminate state when some of the items selected - 'select all' header won't be rendered when the selector column is said to work in single select mode - 'select all' header won't be renderer when the data provisioning method is via dataProvider

Configuration / Customization / Management

Some of the grid's features are configured via column/s configuration, see below on that. Others are related to the grid as a whole, we'll describe them first.

Grid

Some of the properties of grid are also reflected via attributes. All those cases explicitly mention the attribute name in the table below. In those cases attribute and property may be used interchangeably.

PropertyAttributeTypeDefaultDescription
heightByRowsbooleanfalsesets grid block-size to its total rows block-size
multiSortmulti-sortbooleanfalsesorting by multiple columns
reorderingreorderingbooleanfalsecolumns reordering via UI (drag'n'drop')
columnsDataGridColumn[][]columns definitions, the majority of grid configuration, see below more
rowDetailsRendererRowDetailsRendererundefinedwhen provided, will handle an expanded / detailed row part rendering; see more details on RowDetailsRenderer below
itemsunknown[]undefinedsee Data API above
dataProvider(params: { page: number, pageSize: number }, callback: (pageItems: unknown[], treeLevelSize: number) => void): voidundefinedsee Data API above
selectedItemsunknown[][]see Data API above

These are the methods available on grid component:

MethodSignatureDescription
refreshConfiguration(): voidrefreshes configuration on demand, if needed; use when performing changes directly on the columns object (if the array reference remains the same)
openItemDetails(item: unknown): voidmarks the row metadata as expanded and triggers rowDetailsRenderer, if available (see above)
closeItemDetails(item: unknown): voidunmarks the row as expanded and collapses the additional visual space provided for the details
refreshData(): voidtriggers visible data refresh; useful when undetectable data changes happen ('deep' changs in items or dataProvider internal logical conditions)
selectItem(item: unknown, singleSelectMode: boolean = false): voidselects a given item, adding it to the selectedItems array; when singleSelectMode is true all previously selected items will be unselected
deselectItem(item: unknown): voidunselects a given item
selectAll(): voidselect all items; this method will throw when the data provisioning is set to work with dataProvider
deselectAll(): voidunselects all selected items
DataGridColumn

Configuration of the grid columns is the major part of the whole grid setup. There are 2 ways to configure grid's columns:

  • via HTML elements bearing columns definitions - the configuration conveyed via vwc-data-grid-column component's properties / attributes - changes to the columns defitions back-reflected to the columns configuration object - add/remove columns is not supported - direct changes of the columns configuration object are NOT further-reflected in the configurational elements
  • via configuration object manipulation by JavaScript and columns property of the grid component - direct Array manipulations on columns as well as deep changes to the DataGridColumn won't result in auto update of the grid, do <grid>.refreshConfiguration() (see API above)

Note: both flavors adhere to the same configuration interface, namely DataGridColumn API (see below).

Note: durting the initial bootstrapping of a grid instance declarative (vwc-data-grid-column) approach will take precedence over the columns object. Yet, furter changes to the latter and former in an unordered and random fashion may lead to unexpected configuration, which still being valid and fully functional might not reflect the desired state. As such it is better to stick to a chosen flavor along the way.

vwc-data-grid-column component adheres to the DataGridColumn interface, therefore effectively there is a single API definition regardless of which configuration flavor used.

PropertyAttributeTypeDefaultDescription
pathpathstring''path into the item to get the data for this column's cells
treetreebooleanfalsemakes column to behave as tree-able (open/close control, shifted value rendering etc)
hiddenhiddenbooleanfalseshow/hide column
frozenfrozenbooleanfalsefreeze/unfreeze column (horizontal scroll pinning)
sortablesortablebooleanfalseshows sorting UI and provides OOTB sorting support
filterablefilterablebooleanfalseshows filtering UI and provides OOTB filtering support
resizableresizablebooleanfalsecontrols column resizeability support (UI)
selectorselectorstring: 'single' | 'multi'undefinedcolumn designated as selector will provide and OOTB header (checkbox) and cells (checkbox) to manage / reflect selection; see 'Selection API' above
autoWidthauto-widthbooleanfalseshould the column to auto calculate and set it's own width (based on currently rendered content)
widthwidthstringundefinedsets static column width (width CSS value)
headerheaderstring''header label
headerRendererMetaRendererundefinedcustom header rendering; see MetaRenderer details below
footerfooterstring''footer text
footerRendererMetaRendererundefinedcustom footer rendering; see MetaRenderer details below
cellRendererDataRendererundefinedcustom cell rendering; see DataRenderer details below
MetaRenderer

MetaRenderer is a functional interface, defining the signature of the method to be used as renderer of meta elements like headers and footers.

Signature:

(container: HTMLElement, configuration: CellRendererConfiguration): void

Each time grid component will require fresh render of the header/s and/or footer/s, it'll call the custom renderers, if provided.

See more details on CellRendererConfiguration below.

Attention: the renderer MAY and WILL be called several times for the same container, it is your responsibility to enforce idempotency of its logic.

MetaRenderer API is used internally for sorting header and selector column's header. You may see the implementation details correspondingly.

DataRenderer

DataRenderer is a functional interface, defining the signature of the method to be used as renderer of cell contents.

Signature:

(container: HTMLElement, configuration: CellRendererConfiguration, data: { item: unknown, selected: boolean }): void

Each time grid component will require fresh render of the cell/s content, it'll call the custom renderer, if provided.

See more details on CellRendererConfiguration below.

Attention: the renderer MAY and WILL be called several times for the same container, it is your responsibility to enforce idempotency of its logic.

DataRenderer API is used internally for selector column's cells. You may see the implementation details there.

RowDetailsRenderer

RowDetailsRenderer is a functional interface, defining the signature of the method to be used as renderer of the row details (expanded row view).

Signature:

(container: HTMLElement, configuration: RowRendererConfiguration, data: { item: unknown, selected: boolean }): void
CellRendererConfiguration

CellRendererConfiguration supplied to any cell-level custom renderer, namely: header, footer and cell custom renderers.

Interface:

/**
 * `grid` component, the renderer is belonging to
 */
grid: DataGrid,

/**
 * column configuration, the renderer is belonging to
 */
column: DataGridColumn
RowRendererConfiguration

RowRendererConfiguration supplied to any row-level custom renderer, namely: expanded row details custom renderer.

Interface:

/**
 * `grid` component, the renderer is belonging to
 */
grid: DataGrid,

Events API

vwc-data-grid provides a convenient API to handle events happening within the grid.

It is possible and convenient to register a listener for the relevant event on the grid level and then retrieve the actual context the event occured in

dataGrid.addEventListener('click', event => {
	const _dataGrid = event.target;
	const eventContext = _dataGrid.getEventContext(event);
	//	work with context as appropriate
});

getEventContext(event: Event): EventContext | null The method will return an event contex or null in case of non-relevant event.

EventContext interface
/**
 * row number, index, of the interacted row
 */
row: number,

/**
 * actual data item, underlying interacted row
 */
item: unknown

Examples

Few examples of grid definition in several flavors. All of them employ LitHtml binding notation, which of course can be replaced by any other data-centric framework.

Note: the dot (.) leaded notaion reads 'assign the said object to the said property by reference'. For the sake of simplicity we'll assume that all of the top level objects are reachable from the current templating scope.

JavaScript oriented
<vwc-data-grid
	.columns="${columns}"
	.items="${items}"
	.rowDetailsRenderer="${expandedRowRenderer}">
</vwc-data-grid>

Now the JavaScript part, assuming running in the same scope as the template above:

const columns = [
	{ header: 'First Name', path: 'fname' },
	{ header: 'Last Name', path: 'lname', sortable: true },
	{ header: 'Expand Row', autoWidth: true, cellRenderer: cellRenderer }
];
const data = [
	{ fname: 'Max', lname: 'Weber' },
	...
];
const cellRenderer = (container, column, data) {
	container.innerHTML = ...
};

In order to customize columns at runtime use the following pattern:

const grid = document.querySelector('vwc-data-grid');
grid.columns[0].frozen = true;
grid.refreshConfiguration();

Attention: the refreshConfiguration invokation is required as of now, due to internals of columns data structure not being observed for a change. In any change of this in the future further notice will be provided.

HTML oriented
<vwc-data-grid .items="${items}" .rowDetailsRenderer="${expandedRowRenderer}">
	<vwc-data-grid-column header="First Name" path="fname"></vwc-data-grid-column>
	<vwc-data-grid-column header="Last Name" path="lname" sortable></vwc-data-grid-column>
	<vwc-data-grid-column header="Expand Row" auto-width .cellRenderer="${cellRenderer}"></vwc-data-grid-column>
</vwc-data-grid>

Now the JavaScript part, assuming running in the same scope as the template above:

const data = [
	{ fname: 'Max', lname: 'Weber' },
	...
];
const cellRenderer = (container, column, data) {
	container.innerHTML = ...
};

Pay attention, how the columns data structure is not maintained in JS anymore. Moreover, attributes like sortable, auto-width etc can be bound to some data structure managed by data-binding framework, thus removing the whole customization part out of the scripting scope.

2.45.0

2 months ago

2.43.0

10 months ago

2.43.2

9 months ago

2.43.1

10 months ago

2.44.0

8 months ago

2.41.0

11 months ago

2.42.0

11 months ago

2.38.0

1 year ago

2.39.1

1 year ago

2.39.0

1 year ago

2.37.2

1 year ago

2.40.0

1 year ago

2.36.3

1 year ago

2.36.2

1 year ago

2.37.0

1 year ago

2.36.0

2 years ago

2.36.1

2 years ago

2.35.0

2 years ago

2.32.0

2 years ago

2.34.0

2 years ago

2.34.1

2 years ago

2.31.0

2 years ago

2.33.1

2 years ago

2.33.0

2 years ago

2.29.0

2 years ago

2.30.2

2 years ago

2.30.1

2 years ago

2.30.3

2 years ago

2.30.0

2 years ago

2.28.1

2 years ago

2.28.0

2 years ago

2.28.2

2 years ago

2.27.1

2 years ago

2.27.0

2 years ago

2.26.0

2 years ago

2.25.10

2 years ago

2.25.9

2 years ago

2.25.8

2 years ago

2.25.7

2 years ago

2.25.6

2 years ago

2.25.5

2 years ago

2.25.3

2 years ago

2.25.2

2 years ago

2.25.1

2 years ago

2.25.0

2 years ago

2.24.1

2 years ago

2.24.0

2 years ago

2.23.0

2 years ago

2.22.1

2 years ago

2.22.0

3 years ago

2.21.1

3 years ago

2.21.0

3 years ago

2.20.1

3 years ago

2.20.0

3 years ago

2.19.0

3 years ago

2.18.1

3 years ago

2.18.0

3 years ago

2.17.0

3 years ago

2.16.1

3 years ago

2.16.0

3 years ago

2.15.0

3 years ago

2.14.0

3 years ago

2.13.0

3 years ago

2.12.0

3 years ago

2.11.0

3 years ago

2.10.0

3 years ago

2.9.1

3 years ago

2.9.0

3 years ago

2.8.0

3 years ago

2.7.0

3 years ago

2.6.2

3 years ago

2.6.1

3 years ago

2.6.0

3 years ago

2.5.0

3 years ago

2.4.1

3 years ago

2.4.0

3 years ago

2.3.0

3 years ago

2.2.4

3 years ago

2.2.3

3 years ago

2.2.2

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago