The table component
A table with optional filtering and sorting. Unlike most others, this component does not have a fixed set of item properties, any property that is used will be rendered directly as a column in the table. Tables can contain rich text, including images, links, and icons. Table rows can be styled with a background color, and the table can be made striped, hoverable, and bordered.
Advanced users can apply custom styles to table columns using a CSS class with the same name as the column, and to table rows using the _sqlpage_css_class
property.
Top-level parameters
align_right
border
class
description
empty_description
hover
icon
id
markdown
overflow
search
small
sort
striped_columns
striped_rows
Row-level parameters
_sqlpage_color
_sqlpage_css_class
_sqlpage_id
Example 1
The most basic table.
select
'table' as component;
select
1 as a,
2 as b;
select
3 as a,
4 as b;
Result
a | b |
---|---|
1 | 2 |
3 | 4 |
Example 2
A table of users with filtering and sorting.
select
'table' as component,
TRUE as sort,
TRUE as search;
select
'Ophir' as Forename,
'Lojkine' as Surname,
'lovasoa' as Pseudonym;
select
'Linus' as Forename,
'Torvalds' as Surname,
'torvalds' as Pseudonym;
Result
Ophir | Lojkine | lovasoa |
Linus | Torvalds | torvalds |
Example 3
A table that uses markdown to display links
select
'table' as component,
'Documentation' as markdown,
'icon' as icon,
TRUE as sort,
TRUE as search;
select
'table' as icon,
'Table' as name,
'Displays SQL results as a searchable table.' as description,
'[docs](documentation.sql?component=table)' as Documentation,
'red' as _sqlpage_color;
select
'timeline' as icon,
'Chart' as name,
'Show graphs based on numeric data.' as description,
'[docs](documentation.sql?component=chart)' as Documentation;
Result
Example 4
A table with numbers
select
'table' as component,
TRUE as search,
TRUE as sort,
'Price ($)' as align_right,
'Amount in stock' as align_right;
select
31456 as id,
'MIC-ROCC-F-23-206-C' as part_no,
12 as "Price ($)",
5 as "Amount in stock";
select
996 as id,
'MIC-ROCC-F-24-206-A' as part_no,
1 as "Price ($)",
15 as "Amount in stock";
select
131456 as id,
'KIB-ROCC-F-13-205-B' as part_no,
127 as "Price ($)",
9 as "Amount in stock";
Result
31456 | MIC-ROCC-F-23-206-C | 12 | 5 |
996 | MIC-ROCC-F-24-206-A | 1 | 15 |
131456 | KIB-ROCC-F-13-205-B | 127 | 9 |
Example 5
A table with some presentation options
select
'table' as component,
TRUE as hover,
TRUE as striped_rows,
'Some Star Trek Starfleet starships' as description,
TRUE as small;
select
'USS Enterprise' as name,
'NCC-1701-C' as registry,
'Ambassador' as class;
select
'USS Archer' as name,
'NCC-44278' as registry,
'Archer' as class;
select
'USS Endeavour' as name,
'NCC-06' as registry,
'Columbia' as class;
select
'USS Constellation' as name,
'NCC-1974' as registry,
'Constellation' as class;
select
'USS Dakota' as name,
'NCC-63892' as registry,
'Akira' as class;
Result
name | registry | class |
---|---|---|
USS Enterprise | NCC-1701-C | Ambassador |
USS Archer | NCC-44278 | Archer |
USS Endeavour | NCC-06 | Columbia |
USS Constellation | NCC-1974 | Constellation |
USS Dakota | NCC-63892 | Akira |
Example 6
An empty table with a friendly message
select
'table' as component,
'Nothing to see here at the moment.' as empty_description;
Result
Nothing to see here at the moment. |
Examples
Dynamic column names in a table
In all the previous examples, the column names were hardcoded in the SQL query.
This makes it very easy to quickly visualize the results of a query as a table,
but it can be limiting if you want to include columns that are not known in advance.
In situations when the number and names of the columns depend on the data, or on variables,
you can use the dynamic
component to generate the table columns dynamically.
For that, you will need to return JSON objects from your SQL query, where the keys are the column names, and the values are the cell contents.
Databases offer utilities to generate JSON objects from query results
- In PostgreSQL, you can use the
json_build_object
function for a fixed number of columns, orjson_object_agg
for a dynamic number of columns. - In SQLite, you can use the
json_object
function for a fixed number of columns, or thejson_group_object
function for a dynamic number of columns. - In MySQL, you can use the
JSON_OBJECT
function for a fixed number of columns, or theJSON_OBJECTAGG
function for a dynamic number of columns. - In Microsoft SQL Server, you can use the
FOR JSON PATH
clause.
For instance, let's say we have a table with three columns: store, item, and quantity_sold.
We want to create a pivot table where each row is a store, and each column is an item.
We will return a set of json objects that look like this: {"store":"Madrid", "Item1": 42, "Item2": 7, "Item3": 0}
SELECT 'table' AS component;
with filled_data as (
select
stores.store, items.item,
(select coalesce(sum(quantity_sold), 0) from store_sales where store=stores.store and item=items.item) as quantity
from (select distinct store from store_sales) as stores
cross join (select distinct item from store_sales) as items
order by stores.store, items.item
)
SELECT
'dynamic' AS component,
JSON_PATCH( -- SQLite-specific, refer to your database documentation for the equivalent JSON functions
JSON_OBJECT('store', store),
JSON_GROUP_OBJECT(item, quantity)
) AS properties
FROM
filled_data
GROUP BY
store;
This will generate a table with the stores in the first column, and the items in the following columns, with the quantity sold in each store for each item.