A Javascript library for the browser, regular-table exports a
custom element
named <regular-table>, which renders a regular HTML <table> to a sticky
position within a scollable viewport. Only visible cells are rendered and
queried from a natively async virtual data model, making regular-table ideal
for enormous or remote data sets. Use it to build Data Grids, Spreadsheets,
Pivot Tables, File Trees, or anytime you need:
- Just a regular
<table>.
- Virtually rendered for high-performance.
async data model handles slow, remote, enormous, and/or distributed
backends.
- Easy to style, works with any regular CSS for
<table>.
- Small bundle size, no dependencies.
Examples
Documentation
What follows functions as a quick-start guide, and will explain the basics of
the Virtual Data Models, Styling and Interaction APIs. Documented
examples are also
available.
Installation
Include via a CDN like JSDelivr:
<script type="module" src="https://cdn.jsdelivr.net/npm/regular-table"></script>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/regular-table/dist/css/material.css"
/>
Or, add to your project via npm:
npm add regular-table
... then import into your asset bundle.
import "regular-table";
import "regular-table/dist/css/material.css";
<regular-table> Custom Element
regular-table exports no symbols, only the <regular-table> Custom Element
which is registered as a module import side-effect. Once loaded,
<regular-table> can be used just like any other HTMLElement, using regular
browser APIs:
const regularTable = document.createElement("regular-table");
document.body.appendChild(regularTable);
... or from regular HTML:
<regular-table></regular-table>
... or from your library of choice, as long as it supports regular HTML! Here's
an example for React/JSX:
const App = () => <regular-table></regular-table>;
ReactDOM.render(<App />, document.getElementById("root"));
.setDataListener() Virtual Data Model
Let's start with with a simple data model, a two dimensional Array. This one
is very small at 3 columns x 6 rows, but even for very small data sets,
regular-table won't read your entire dataset at once. Instead, we'll need to
write a simple virtual data model to access DATA and COLUMN_NAMES
indirectly.
const DATA = [
[0, 1, 2, 3, 4, 5],
["A", "B", "C", "D", "E", "F"],
[true, false, true, false, true, false],
];
When clipped by the scrollable viewport, you may end up with a <table> of just
a rectangular region of DATA, rather than the entire set. A simple viewport
2x2 may yield this <table>:
{
"num_rows": 26,
"num_columns": 3,
"data": [
[0, 1],
["A", "B"]
]
}
Here's a an implementation for this simple virtual data model, the function
getDataSlice(). This function is called by your <regular-table> whenever it
needs more data, with coordinate arguments, (x0, y0) to (x1, y1). Only this
region is needed to render the viewport, so getDataSlice() returns this
rectangular slice of DATA. For the window (0, 0) to (2, 2), getDataSlice()
would generate an Object as above, containing the data slice, as well as the
overall dimensions of DATA itself ( num_rows, num_columns), for sizing the
scroll area. To render this virtual data model to a regular HTML <table>,
register this data model via the setDataListener() method:
function getDataSlice(x0, y0, x1, y1) {
return {
num_rows: (num_rows = DATA[0].length),
num_columns: DATA.length,
data: DATA.slice(x0, x1).map((col) => col.slice(y0, y1)),
};
}
regularTable.setDataListener(getDataSlice);
This will render your regular HTML <table> ! Your DOM will look something like
this, depending on the size of your viewport. Notice there are fewer rows and
columns in the resulting HTML, e.g. the column Column 3 (boolean) - as you
scroll, more data will be fetched from getDataSlice(), and parts of the
<table> will redrawn or extended as needed.
<regular-table>
<table>
<tbody>
<tr>
<td>0</td>
<td>A</td>
</tr>
<tr>
<td>1</td>
<td>B</td>
</tr>
</tbody>
</table>
</regular-table>
virtual_mode Option
regular-table supports four modes of virtual scrolling, which can be
configured via the virtual_mode optional argument. Note that using a
virtual_mode other than the default "both" will render the entire
<table> along the non-virtual axis(es), and may cause rendering performance
degradation.
- "both" (default) virtualizes scrolling on both axes.
- "vertical" only virtualizes vertical (y) scrolling.
- "horizontal" only virtualizes horizontal (x) scrolling.
- "none" disable all scroll virtualization.
table.setDataListener(listener, { virtual_mode: "vertical" });
Column and Row Headers
regular-table can also generate Hierarchial Row and Column Headers, using
<th> elements which layout in a fixed position within the virtual table. It
can generate Column Headers (within the <thead>), or Row Headers (the first
children of each tbody tr), via the column_headers and row_headers
properties (respectively) of your data model's Response object. This can be
renderered with column_headers, a two dimensional Array which must be of
length x1 - x0, one Array for every column in your data window.
| Column 1 (number) |
Column 2 (string) |
| 0 |
A |
| 1 |
B |
{
"num_rows": 26,
"num_columns": 3,
"data": [
[0, 1],
["A", "B"]
],
"column_headers": [["Column 1 (number)"], ["Column 2 (string)"]]
}
Hierarchial/Group Headers
regular-table supports multiple <tr> of <th>, and also uses colspan and
rowspan to merge simple consecutive names, which allows description of simple
Row and Column Group Hierarchies such as this:
|
Colgroup 1 |
| Column 1 |
Column 2 |
| Rowgroup 1 |
Row 1 |
0 |
A |
| Row 2 |
1 |
B |
{
"num_rows": 26,
"num_columns": 3,
"data": [
[0, 1],
["A", "B"]
],
"row_headers": [
["Rowgroup 1", "Row 1"],
["Rowgroup 1", "Row 2"]
],
"column_headers": [
["Colgroup 1", "Column 1"],
["Colgroup 1", "Column 2"]
]
}
Note that in the rendered HTML, for these Row and Column Array, repeated
elements in a sequence will be automatically merged via rowspan and colspan
attributes. In this example, e.g. "Rowgroup 1" will only output to one <th>
node in the resulting <table>.