`.
- ```css
- // Zebra striping!
- regular-table tr:
- background: rgba(0,0,0,0.2);
- }
- ```
However, CSS alone cannot select on properties of your _data_ - if you scroll
this example, the 2nd row will always be the striped one. Some other
data-reliant style examples include:
* Styling a specific column in the virtual data set, as ` ` may represent a different column based on horizontal scroll position. Styling cells by value, +/-, heatmaps, categories, etc. Styling cells based on data within-or-outside of the virtual viewport, grouping depth, grouping categories, etc.
To make CSS that is virtual-data-model-aware, you'll need to use `addStyleListener()`, which invokes a callback whenever the `` isre-rendered, such as through API invocations of draw() and user-initiated events such as scrolling. Within this optionally async callback, you can select ``, ` | `, etc. elements via regular DOM API methods like querySelectorAll().
- ```javascript
- // Only select row_headers!
- table.addStyleListener(() => {
- for (const th of table.querySelectorAll("tbody th")) {
- style_th(th);
- }
- });
- ```
Once you've selected the ` | ` and ` | ` you want to paint, `getMeta()` will return a MetaData record of information about the HTMLElement's virtual position. This example uses meta.x, the position in data-space, to make virtual-scroll-aware zebra striping.
- ```javascript
- function style_th(th) {
- const meta = table.getMeta(th);
- th.classList.toggle("zebra-striped", meta.x % 2 === 0);
- }
- ```
- ```css
- .zebra-striped {
- background-color: rgba(0,0,0,0.2);
- }
- ```
.invalidate()
To prevent DOM renders, `` conserves DOM calls like `offsetWidth`to an internal cache. When a `` or ` | `'s `width` is modified within a callback to `.addStyleListener()`, you must indicate to `` that its dimensions have changed in order to invalidate this cache, or you may not end up with enough rendered columns to fill the screen!
A call to invalidate() that does not need new columns only imparts a small runtime overhead to re-calculate virtual width per async draw iteration, but should be used conservatively if possible. Calling invalidate() outside of a callback to .addStyleListener() will throw an Error.
- ```javascript
- table.addStyleListener(() => {
- for (const th of table.querySelectorAll("tbody th")) {
- th.style.maxWidth = "20px";
- }
- table.invalidate();
- });
- ```
.addEventListener() Interaction
`` is a normal `HTMLElement`! Use the `regular-table` APIin concert with regular DOM API methods that work on other HTMLElement to create advanced functionality, such as this example of virtual row select:
- ```javascript
- const selected_rows = [];
- table.addEventListener("mousedown", (event) => {
- const meta = table.getMeta(event.target);
- if (meta && meta.y >= 0) {
- selected_rows.push(meta.y);
- table.draw();
- }
- });
- table.addStyleListener(() => {
- for (const td of table.querySelectorAll("td")) {
- const meta = table.getMeta(td);
- td.classList.toggle("row-selected", selected_rows.includes(meta.y));
- }
- });
- ```
Advanced examples can be found in the [examples](https://github.com/finos/regular-table/tree/master/examples) directory, and in the [bl.ocks example gallery](https://github.com/finos/regular-table#examples).
Scrolling
Because of the structure of the HTML `` element, `` elements must bealigned with their respective row/column, which causes default `` to only be able to scroll in increments of a cell, which can be irregular when column data is of different lengths. Optionally, you may implement _sub-cell scrolling_ in CSS via `` slotted CSS variables.The provided material.css theme does exactly this, or you can implement this in any custom style by importing the sub_cell_scrollling.css stylesheet explicitly:
- ```html
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/regular-table/dist/css/sub-cell-scrolling.css">
- ```
Pivots, Filters, Sorts, and Column Expressions with perspective
regular-table is natively compatible with [perspective](https://github.com/finos/perspective/), a WebAssembly streaming visualization engine. By using a perspective.Table as a Virtual Data Nodel, it becomes simple to achieve user-driven row and column pivots, filters, sorts, and column expressions, as well as charts and persistent layouts, from high-frequency updating data.
Development
First install dev_dependencies:
Build the library
Run the test suite
Start the example server at [http://localhost:8080/examples/](http://localhost:8080/examples/)
<!-- Stats-->
| | | |