Grid
<esp-grid>
Renders data in a table. Specify grid column definitions by adding esp-grid-column elements to the default slot.
Slots
<esp-grid> has the following slots:
Default
Grid has a default slot that expects <esp-grid-column> elements to define the columns in the grid:
The grid also supports cursor-based infinite scrolling. Instead of fetching all data up front, provide a fetchPage callback that returns a page of items and a cursor for the next page. The grid virtualizes the rows and fetches more data as the user scrolls. Search and sort are delegated to the server via the callback parameters. width attribute. Use the grow attribute on one or more columns to fill remaining horizontal space when the grid is wider than its content.
When the total number of items is smaller than the configured grid-height, the grid automatically shrinks to fit after data loads. This also works after sort, search, and reload — the grid re-measures and adjusts its height each time.
The grid also regrows when a reload returns more items. In this example the grid starts with 1 row (compact), then clicking "Load more" replaces the data with 10 rows and the grid expands to fit.
Columns can use a .template callback to render custom web components inside each cell. The grid explicitly waits for Lit-based custom elements (those exposing updateComplete) to finish rendering before measuring row heights. For other custom elements, a zero-height guard prevents the grid from collapsing if the element hasn't painted yet. In the example below, a <grid-cell-demo> custom element renders a two-line contact cell with the person's name and email.
header-buttons
The header buttons slot is for adding buttons next to the search box at the top of the grid. Useful for things like an add item button:
Attributes
<esp-grid> has the following attributes:
data-field
data-field has a default value of "".
If there is a data-url specified, the name of the
field on the response to get the array of items from.
If it is empty, the API response should simply return
an array of data items.
data-url
data-url has a default value of "".
Specify a URL the grid will fetch data from.
items
items has a default value of undefined.
If using a data-url, the items retrieved are available
in the items property. An items array can be assigned
instead of using a data-url.
page-size
page-size has a default value of 10.
The number of items to display on a page. In paged mode this controls how many rows are shown per page. In infinite scroll mode this controls how many items are fetched per request.
grid-height
grid-height has a default value of "60vh".
The height of the grid when in infinite scroll mode. Accepts
any valid CSS height value. Defaults to 60vh.
If the loaded content is shorter than this value (and there are no more pages to fetch), the grid shrinks to fit.
search-fields
search-fields has a default value of [].
The fields to include when filtering results. It defaults to
an empty array, which includes all fields. The test dataset
includes an address field that is not displayed, so to only
filter results by fields displayed in the grid, specify those
fields in the search-fields attribute.
Methods
<esp-grid> has the following methods:
delete
Deletes items from the grid where the lookup function returns true.
addOrReplace
Adds or replaces the item from the grid where the lookup function returns true. If there is more than one match, only the first match is replaced.
addInOrder
Inserts or replaces an item in sorted order. If an existing item
matches the lookup function, it is replaced in-place. Otherwise
the new item is inserted at the position determined by the
compare function (same semantics as Array.prototype.sort).
Returns the index of the inserted or replaced item, which can be passed to scrollToIndex to scroll the item into view.
scrollToIndex
Scrolls to the item at the given index.
In infinite mode this waits for the virtualizer to
complete its layout pass, then delegates to its
scrollToIndex method.
In paged mode this navigates to the page containing
the item at index.
clear
Clears all items from the grid.
reload
Clears all loaded data and re-fetches from the beginning.
In infinite mode this resets the cursor and triggers a
fresh fetchPage call. In paged mode this is a no-op
(use the data-url attribute to reload).
traverseToClosest
Traverse up the DOM tree to find the closest element that matches the selector. This method is aware of shadow DOM boundaries and will traverse through them to find the element.
Events
<esp-grid> emits the following events:
grid-event
grid-event is of type CustomEvent<GridClickedEvent>.
The grid listens for click events, and if a child element has a grid-event attribute, grid publishes an event with the grid, clicked element, and associated data.
CSS Properties
<esp-grid> has the following CSS properties:
--esp-grid-border
The border style of the grid.
--esp-grid-background
Background color of the grid container. Defaults to var(--esp-color-layer-1).
--esp-grid-text-color
Text color used by the grid. Defaults to var(--esp-color-text).
--esp-grid-header-background
Background color of header rows and pinned header cells. Defaults to var(--esp-color-layer-4).
--esp-grid-header-active-background
Background color used for sorted or hovered sortable headers. Defaults to var(--esp-color-layer-3).
--esp-grid-row-background-odd
Background color of odd rows. Defaults to var(--esp-color-layer-1).
--esp-grid-row-background-even
Background color of even rows. Defaults to var(--esp-color-layer-2).
--esp-grid-row-hover-background
Background color of hovered rows. Defaults to var(--esp-color-layer-3).
--esp-grid-cell-border-color
Cell divider color used throughout the grid. Defaults to var(--esp-color-layer-3).
--esp-grid-row-hover-border-color
Divider color used while rows are hovered. Defaults to var(--esp-color-layer-4).
--esp-grid-link-color
Link color used inside grid cells. Defaults to var(--esp-color-link).
--esp-grid-link-hover-color
Link hover decoration color used inside grid cells. Defaults to var(--esp-color-link-hover).
--esp-field-background
Background color of the shared field shell. Defaults to var(--esp-color-layer-2).
--esp-field-border-color
Border color of the shared field shell. Defaults to var(--esp-color-border).
--esp-field-border-width
Border width of the shared field shell. Defaults to 1px.
--esp-field-text-color
Text color used inside the shared field shell. Defaults to var(--esp-color-text).
--esp-field-hover-bg
Hover background color of the shared field shell. Derived from --esp-field-background.
--esp-field-focus-bg
Focus background color of the shared field shell. Derived from --esp-field-background.
--esp-field-focus-shadow
Shadow color used for shared field focus treatment. Defaults to var(--esp-color-shadow).