The Ignite UI for Web Components Data Table / Data Grid is a tabular Web Components grid component that allows you to quickly bind and display your data with little coding or configuration. Features of the Web Components data grid in our toolbox include filtering, sorting, templates, row selection, row grouping, row pinning and movable columns.
The Web Components tables are optimized for live streaming data, with the ability to handle unlimited data set size in a number of rows or columns.
Web Components Data Grid Example
In this Ignite UI for Web Components Grid example, you can see how users can do both basic and excel-style filtering, live-data sorting, and use grid summaries as well as cell templating. The demo also includes paging set to display 10 items per page.
<!DOCTYPE html><html><head><title>Sample | Ignite UI | Web Components | infragistics</title><metacharset="UTF-8" /><linkrel="shortcut icon"href="https://static.infragistics.com/xplatform/images/browsers/wc.png" ><linkrel="stylesheet"href="https://fonts.googleapis.com/icon?family=Material+Icons" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Kanit&display=swap" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Titillium Web" /><linkrel="stylesheet"href="https://static.infragistics.com/xplatform/css/samples/shared.v8.css" /><linkrel="stylesheet"href="/src/index.css"type="text/css" /></head><body><divid="root"><divclass="container sample ig-typography"><divclass="container fill"><igc-gridauto-generate="false"id="grid"name="grid"id="grid"primary-key="ProductID"allow-filtering="true"filter-mode="excelStyleFilter"><igc-paginatorper-page="10"></igc-paginator><igc-columnfield="ProductName"header="Product Name"data-type="string"sortable="true"has-summary="true"editable="true"resizable="true"></igc-column><igc-columnfield="UnitPrice"header="Unit Price"data-type="number"sortable="true"has-summary="true"editable="true"resizable="true"></igc-column><igc-columnfield="UnitsInStock"header="Units in Stock"data-type="number"sortable="true"has-summary="true"editable="true"resizable="true"></igc-column><igc-columnfield="OrderDate"header="Order Date"data-type="date"sortable="true"has-summary="true"editable="true"resizable="true"></igc-column><igc-columnfield="Discontinued"header="Discontinued"data-type="boolean"sortable="true"has-summary="true"editable="true"name="column1"id="column1"></igc-column><igc-columnfield="ReorderLevel"header="Reorder Level"data-type="number"sortable="true"has-summary="true"editable="true"filterable="true"></igc-column></igc-grid></div></div></div><!-- This script is needed only for parcel and it will be excluded for webpack -->
<% if (false) { %><scriptsrc="src/index.ts"></script><% } %>
</body></html>html
The corresponding styles should also be referenced. You can choose light or dark option for one of the themes and based on your project configuration to import it:
constructor() {
let grid1 = document.getElementById("grid1") as IgcGridComponent;
grid1.data = data;
}
typescript
The id property is a string value and is the unique identifier of the grid which will be auto-generated if not provided, while data binds the grid, in this case to local data.
The autoGenerate property tells the grid to auto generate the grid's IgcColumnComponent components based on the data source fields. It will also try to deduce the appropriate data type for the column if possible. Otherwise, the developer needs to explicitly define the columns and the mapping to the data source fields.
Editable Web Components Grid
Each operation for grid editing includes batch operations, meaning the API gives you the option to group edits into a single server call, or you can perform grid edit / update operations as they occur with grid interactions. Along with a great developer experience as an editable grid with CRUD operations, the grid includes Excel-like keyboard navigation. Common default grid navigation is included, plus the option to override any navigation option to meet the needs of your customers. An editable grid in with a great navigation scheme is critical to any modern line of business application, with the Ignite UI grid we make it easy.
IgcColumnComponent is used to define the grid's columns collection and to enable features per column like sorting and filtering. Cell, header, and footer templates are also available.
Defining Columns
Let's turn the autoGenerate property off and define the columns collection in the markup:
constructor() {
var name = this.name = document.getElementById('name') as IgcColumnComponent;
this._bind = () => {
name.headerTemplate = this.nameHeaderTemplate;
}
this._bind();
}
public nameHeaderTemplate = (ctx: IgcColumnTemplateContext) => {
return html`${this.formatUppercase(ctx.column.field)}
`;
}
publicformatUppercase(value: string) {
return value.toUpperCase();
}
typescript
Note:
Whenever a header template is used along with grouping/moving functionality the column header area becomes draggable and you cannot access the custom elements part of the header template until you mark them as not draggable. Example below.
As you can see, we are adding Draggable attribute set to false.
Cell Template
When cell template is set it changes all the cells in the column. The context object provided in the template consists of the cell value provided implicitly and the cell object itself. It can be used to define a template where the cells' text could be formatted e.g. as title case.
constructor() {
var name = this.name = document.getElementById('name') as IgcColumnComponent;
name.bodyTemplate = this.nameCellTemplate;
}
public nameCellTemplate = (ctx: IgcCellTemplateContext) => {
return html`${this.formatTitleCase(ctx.implicit)}
`;
}
publicformatTitleCase(value: string) {
return value.toUpperCase();
}
typescript
In the snippet above we take a reference to the implicitly provided cell value. This is sufficient if you just want to present some data and maybe apply some custom styling or pipe transforms over the value of the cell. However even more useful is to take the Cell instance itself as shown below:
constructor() {
var grid = this.grid = document.getElementById('grid') as IgcGridComponent;
var name = this.name = document.getElementById('name') as IgcColumnComponent;
var subscription = this.subscription = document.getElementById('subscription') as IgcColumnComponent;
grid.data = this.data;
name.bodyTemplate = this.nameCellTemplate;
subscription.bodyTemplate = this.subscriptionCellTemplate;
}
public nameCellTemplate = (ctx: IgcCellTemplateContext) => {
return html`<spantabindex="0" @keydown="${() => this.deleteRow(ctx.cell.id.rowIndex)}">${this.formatTitleCase(ctx.cell.value)}</span>
`;
}
public subscriptionCellTemplate = (ctx: IgcCellTemplateContext) => {
if (ctx.cell.value) {
return html`<inputtype="checkbox"checked /> `;
} else {
return html`<inputtype="checkbox"/> `;
}
}
publicdeleteRow(rowIndex: number) {
this.grid.deleteRow(rowIndex);
}
publicformatTitleCase(value: string) {
return value.toUpperCase();
}
typescript
Note:
The grid exposes a default handling for number, string, date and boolean column types. For example, the column will display check or close icon, instead of true/false by default, for boolean column type.
When properly implemented, the cell editing template also ensures that the cell's EditValue will correctly pass through the grid editing event cycle.
Cell Editing Template
The column also accepts one last template that will be used when a cell is in edit mode. As with the other column templates, the provided context object is again the cell value and the cell object itself. Of course in order to make the edit-mode template accessible to end users, you need
to set the editable property of the column to true.
constructor() {
var price = this.price = document.getElementById('price') as IgcColumnComponent;
price.inlineEditorTemplate = this.priceCellTemplate;
}
public priceCellTemplate = (ctx: IgcCellTemplateContext) => {
return html`<label>
Enter the new price tag
</label><inputname="price"type="number"value="${ctx.cell.value}" @change="${() => this.updateValue(ctx.cell.value)}" />
`;
}
publicupdateValue(value: number) {
}
typescript
Make sure to check the API for the Cell in order to get accustomed with the provided properties you can use in your templates.
Column Template API
Each of the column templates can be changed programmatically at any point through the IgcColumnComponent object itself. For example in the code below, we have declared two templates for our user data. In our TypeScript code we'll get references to the templates themselves and then based on some condition we will render the appropriate template for the column in our application.
var user = this.user = document.getElementById('user') as IgcColumnComponent;
// Return the appropriate template based on some condition.// For example saved user settings, viewport size, etc.
user.bodyTemplate = this.smallView;
public normalViewTemplate = (ctx: IgcCellTemplateContext) => {
return html`<divclass="user-details">${ ctx.cell.value }</div><user-details-component></user-details-component>
`;
}
public smallViewTemplate = (ctx: IgcCellTemplateContext) => {
return html`<divclass="user-details-small">${ ctx.cell.value }</div>
`;
}
typescript
Column properties can also be set in code in the ColumnInit event which is emitted when the columns are initialized in the grid.
The code above will make the ProductName column sortable and editable and will instantiate the corresponding features UI (like inputs for editing, etc.).
Custom Display Format
There are optional parameters for formatting:
format - determines what date/time parts are displayed, defaults to 'mediumDate', equivalent to 'MMM d, y'
timezone - the timezone offset for dates. By default uses the end-user's local system timezone
digitsInfo - decimal representation objects. Default to 1.0-3
To allow customizing the display format by these parameters, the pipeArgs input is exposed. A column will respect only the corresponding properties for its data type, if pipeArgs is set. Example:
If you use autoGenerate columns the data keys must be identical.
Grid Data Binding
Before going any further with the grid we want to change the grid to bind to remote data service, which is the common scenario in large-scale applications.
You can do this by fetching the data from a given url receiving a JSON response and assigning it to the grid's data property that is used as the grid's data source:
<igc-gridid="grid1"></igc-grid>html
public fetchData(url: string): void {
fetch(url)
.then(response => response.json())
.then(data =>this.onDataLoaded(data));
}
publiconDataLoaded(jsonData: any[]) {
var grid1 = document.getElementById("grid1") as IgcGridComponent;
grid1.data = jsonData;
}
typescript
Note: The grid autoGenerate property is best to be avoided when binding to remote data for now. It assumes that the data is available in order to inspect it and generate the appropriate columns. This is usually not the case until the remote service responds, and the grid will throw an error. Making autoGenerate available, when binding to remote service, is on our roadmap for future versions.
Complex Data Binding
The IgcGridComponent supports binding to complex objects (including nesting deeper than one level) through a "path" of properties in the data record.
An alternative way to bind complex data, or to visualize composite data (from more than one column) in the IgcGridComponent is to use a custom body template for the column. Generally, one can:
use the value of the cell, that contains the nested data
use the cell object in the template, from which to access the ctx.cell.id.rowIndex or ctx.cell.id.rowID to get the row via the grid's API and retrieve any value from it and interpolate those in the template.
<!DOCTYPE html><html><head><title>Sample | Ignite UI | Web Components | infragistics</title><metacharset="UTF-8" /><linkrel="shortcut icon"href="https://static.infragistics.com/xplatform/images/browsers/wc.png" ><linkrel="stylesheet"href="https://fonts.googleapis.com/icon?family=Material+Icons" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Kanit&display=swap" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Titillium Web" /><linkrel="stylesheet"href="https://static.infragistics.com/xplatform/css/samples/shared.v8.css" /><linkrel="stylesheet"href="/src/index.css"type="text/css" /></head><body><divid="root"><divclass="container sample ig-typography"><divclass="container fill"><igc-gridauto-generate="false"name="grid"id="grid"id="grid"><igc-columnheader="Name"field="Name"width="15%"></igc-column><igc-columnfield="Title"header="Title"width="15%"></igc-column><igc-columnfield="Salary"header="Salary"width="10%"></igc-column><igc-columnfield="Employees"header="Employees"width="20%"name="column1"id="column1"></igc-column><igc-columnfield="City"header="City"width="15%"></igc-column><igc-columnfield="Country"header="Country"width="15%"></igc-column><igc-columnfield="Age"header="Age"width="10%"></igc-column><igc-columnfield="HireDate"header="Hire Date"data-type="date"></igc-column></igc-grid></div></div></div><!-- This script is needed only for parcel and it will be excluded for webpack -->
<% if (false) { %><scriptsrc="src/index.ts"></script><% } %>
</body></html>html
/* shared styles are loaded from: *//* https://static.infragistics.com/xplatform/css/samples */css
Working with Flat Data Overview
The flat data binding approach is similar to the one that we already described above, but instead of cell value we are going to use the data property of the IgcGridRow.
Since the Web Components grid is a component for rendering, manipulating and preserving data records, having access to every data record gives you the opportunity to customize the approach of handling it. The data property provides you this opportunity.
constructor() {
var address = this.address = document.getElementById('address') as IgcColumnComponent;
address.bodyTemplate = this.addressCellTemplate;
}
public addressCellTemplate = (ctx: IgcCellTemplateContext) => {
return html`<divclass="address-container"><!-- In the Address column combine the Country, City and PostCode values of the corresponding data record --><span><strong>Country:</strong>${this.getCountry(ctx.cell.id.rowIndex)}</span><br/><span><strong>City:</strong>${this.getCity(ctx.cell.id.rowIndex)}</span><br/><span><strong>Postal Code:</strong>${this.getPostalCode(ctx.cell.id.rowIndex)}</span></div>
`;
}
publicgetCountry(rowIndex: number) {
returnthis.grid.getRowByIndex(rowIndex).data["Country"];
}
publicgetCity(rowIndex: number) {
returnthis.grid.getRowByIndex(rowIndex).data["City"];
}
publicgetPostalCode(rowIndex: number) {
returnthis.grid.getRowByIndex(rowIndex).data["PostalCode"];
}
typescript
Keep in mind that with the above defined template you will not be able to make editing operations, so we need an editor template.
<!DOCTYPE html><html><head><title>Sample | Ignite UI | Web Components | infragistics</title><metacharset="UTF-8" /><linkrel="shortcut icon"href="https://static.infragistics.com/xplatform/images/browsers/wc.png" ><linkrel="stylesheet"href="https://fonts.googleapis.com/icon?family=Material+Icons" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Kanit&display=swap" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Titillium Web" /><linkrel="stylesheet"href="https://static.infragistics.com/xplatform/css/samples/shared.v8.css" /><linkrel="stylesheet"href="/src/index.css"type="text/css" /></head><body><divid="root"><divclass="container sample ig-typography"><divclass="container fill"><igc-gridauto-generate="false"primary-key="ID"name="grid"id="grid"><igc-columnheader="ID"field="ID"></igc-column><igc-columnfield="ContactName"header="Contact"editable="true"width="250px"resizable="false"name="column1"id="column1"></igc-column><igc-columnheader="Address"field="Address"editable="true"width="250px"resizable="false"name="column2"id="column2"></igc-column><igc-columnheader="Country"field="Country"></igc-column><igc-columnheader="Region"field="Region"></igc-column><igc-columnheader="Phone"field="Phone"></igc-column><igc-columnheader="Fax"field="Fax"></igc-column></igc-grid></div></div></div><!-- This script is needed only for parcel and it will be excluded for webpack -->
<% if (false) { %><scriptsrc="src/index.ts"></script><% } %>
</body></html>html
/* shared styles are loaded from: *//* https://static.infragistics.com/xplatform/css/samples */css
Keyboard Navigation
Keyboard navigation of the IgcGridComponent provides a rich variety of keyboard interactions for the user. It enhances accessibility and allows intuitive navigation through any type of elements inside (cell, row, column header, toolbar, footer, etc.).
Styling Web Components Grid
Note:
The grid uses css grid layout, which is not supported in IE without prefixing, consequently it will not render properly.
In addition to the predefined themes, the grid could be further customized by setting some of the available CSS properties. In case you would like to change the header background and text color, you need to set a class for the grid first:
<igc-gridclass="grid"></igc-grid>html
Then set the --header-background and --header-text-color CSS properties for that class:
Currently we do not support mixing of column widths with % and px.
When trying to filter a column of type number
If a value different than number is entered into the filtering input, NaN is returned due to an incorrect cast.
Grid width does not depend on the column widths
The width of all columns does not determine the spanning of the grid itself. It is determined by the parent container dimensions or the defined grid's width.
Grid nested in parent container
When grid's width is not set and it is placed in a parent container with defined dimensions, the grid spans to this container.
Grid OnPush ChangeDetectionStrategy
The grid operates with ChangeDetectionStrategy.OnPush so whenever some customization appears make sure that the grid is notified about the changes that happens.
Columns have a minimum allowed column width. Depending on the --ig-size CSS variable, they are as follows: "small": 56px "medium": 64px "large ": 80px
If width less than the minimum allowed is set it will not affect the rendered elements. They will render with the minimum allowed width for the corresponding --ig-size. This may lead to an unexpected behavior with horizontal virtualization and is therefore not supported.
Row height is not affected by the height of cells that are not currently rendered in view.
Because of virtualization a column with a custom template (that changes the cell height) that is not in the view will not affect the row height. The row height will be affected only while the related column is scrolled in the view.