Creates a table from an array of column configurations and an array of row items.
On the project page, NNM Simple table, you can read more details and the theory and background on the ideas and decisions.
The rows can be sorted by clicking on the column headers. Current sorting column is indicated with different color and bars.
First click on a column header sorts ascending, second click sorts descending, and third click displays the original order.
Most of the examples are using the same fictive data for the rows (which you can see in the first example). Some examples use data from the real world.
All the unmodified original data, presented with all the columns.
From the original row data, a portion is sliced out (rows.slice(2, 7)
).
Only showing the employees. The first two columns are renamed.
Created new information from the original data. Here we see that you easily can combine and create new properties to present things differently.
Randomly created, that can be easily sorted (sort and see how the colors harmonize). Also utilizing the optional cell enhancer functionality.
Real world data.
Utilizing the optional cell enhancer functionality.
Randomly created, that can be easily sorted (sort and see how the colors harmonize). Also utilizing the optional cell enhancer functionality for several columns.
Using the optional cell enhancer functionality, to display special content (in this case the nnm-timestamp-enhancer
web component).
normal
or striped
. If set to striped
, the table rows will be striped. Defaults to normal
.
You need to set the arrays via properties, and not via attributes (Google Developers). So the column configuration and the rows must be set programmatically in JavaScript.
ColumnConfiguration
objects.
RowItem
objects.
The following TypeScript types are used within the component:
type PropertyType = "text" | "number" | "boolean" | "enum"; type PropertyValue = string | number | boolean | null | undefined; type CellEnhancer = (cell: HTMLTableCellElement, value: PropertyValue, rowItem: RowItem) => void; type ColumnConfiguration = { label: string; propertyName: string; propertyType: PropertyType; width?: string | null; // Highly needed when you have a pagination table. cellEnhancer?: CellEnhancer; }; type RowItem = Record<string, PropertyValue>; type SortingOrder = "asc" | "desc" | null;
The purpose of the cellEnhancer
property is to make it easy to dynamically modify a cell depending on the actual cell content.
The fifth example, the tall table with random integers, is created with the following TypeScript code:
const columnsFifthExample: ColumnConfiguration[] = [ { label: "Id", propertyName: "id", propertyType: "number" }, { label: "Random integer", propertyName: "randomInteger", propertyType: "number", cellEnhancer: (tableCellElement: HTMLTableCellElement, value: PropertyValue) => { const alpha = 0.4; const smallIndicatorColor = `rgba(255, 200, 200, ${alpha})`; const mediumIndicatorColor = `rgba(200, 255, 200, ${alpha})`; const largeIndicatorColor = `rgba(200, 200, 255, ${alpha})`; if (typeof value === "number") { const indicatorColor = value < 50 ? smallIndicatorColor : value < 100 ? mediumIndicatorColor : largeIndicatorColor; tableCellElement.setAttribute("style", `background-color: ${indicatorColor};`); } } }, ]; const rowsRandom: RowItem[] = Array.from(Array(40).keys()) .map((index: number) => { return { id: index + 1, randomInteger: Math.ceil(Math.random() * 120) } }); const fifthExample = document.querySelector("#fifth-example") as NnmSimpleTable; if (fifthExample) { fifthExample.columnConfigurations = columnsFifthExample; fifthExample.rowItems = rowsRandom; }
Note: The <nnm-simple-table>
component was initially created in March 2022.