Skip to content

Commit 967003e

Browse files
committed
Move advanced table examples to individual files
1 parent bd8921e commit 967003e

6 files changed

Lines changed: 219 additions & 259 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
rows:
3+
- component: table
4+
hover: true
5+
striped_rows: true
6+
description: Some Star Trek Starfleet starships
7+
small: true
8+
initial_search_value: NCC-
9+
- name: USS Enterprise
10+
registry: NCC-1701-C
11+
class: Ambassador
12+
- name: USS Archer
13+
registry: NCC-44278
14+
class: Archer
15+
- name: USS Endeavour
16+
registry: NCC-06
17+
class: Columbia
18+
- name: USS Constellation
19+
registry: NCC-1974
20+
class: Constellation
21+
- name: USS Dakota
22+
registry: NCC-63892
23+
class: Akira
24+
- name: USS Defiant
25+
registry: IX-74205
26+
class: Defiant
27+
---
28+
29+
A table with some presentation options
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
rows:
3+
- component: table
4+
empty_description: Nothing to see here at the moment.
5+
---
6+
7+
An empty table with a friendly message
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
rows:
3+
- component: table
4+
freeze_columns: true
5+
freeze_headers: true
6+
- feature: SQL Execution
7+
description: Fully compatible with existing databases SQL dialects, executes any SQL query.
8+
benefits: Short learning curve, easy to use, interoperable with existing tools.
9+
- feature: Data Visualization
10+
description: 'Automatic visualizations of query results: graphs, plots, pie charts, heatmaps, etc.'
11+
benefits: Quickly analyze data trends, attractive and easy to understand, no external visualization tools
12+
or languages to learn.
13+
- feature: User Authentication
14+
description: Supports user sessions, from basic auth to single sign-on.
15+
benefits: Secure, enforces access control policies, and provides a customizable security layer.
16+
- feature: APIs
17+
description: Allows building JSON REST APIs and integrating with external APIs.
18+
benefits: Enables automation and integration with other platforms, facilitates data exchange.
19+
- feature: Files
20+
description: File uploads, downloads and processing. Supports local filesystem and database storage.
21+
benefits: Convenient file management, secure data handling, flexible storage options, integrates with existing
22+
systems.
23+
- feature: Maps
24+
description: Supports GeoJSON and is compatible with GIS data for map visualization.
25+
benefits: Geospatial data representation, integrates with geographic information systems.
26+
- feature: Custom Components
27+
description: Build advanced features using HTML, JavaScript, and CSS.
28+
benefits: Tailor-made user experiences, easy to implement custom UI requirements.
29+
- feature: Forms
30+
description: Insert and update data in databases based on user input.
31+
benefits: Simplified data input and management, efficient user interactions with databases.
32+
- feature: DB Compatibility
33+
description: Works with MySQL, PostgreSQL, SQLite, Microsoft SQL Server and compatible databases.
34+
benefits: Broad compatibility with popular database systems, ensures seamless integration.
35+
- feature: Security
36+
description: 'Built-in protection against common web vulnerabilities: no SQL injection, no XSS.'
37+
benefits: Passes audits and security reviews, reduces the risk of data breaches.
38+
- feature: Performance
39+
description: Designed for performance, with a focus on efficient data processing and minimal overhead.
40+
benefits: Quickly processes large datasets, handles high volumes of requests, and minimizes server load.
41+
- _sqlpage_footer: true
42+
feature: Summary
43+
description: Summarizes the features of the product.
44+
benefits: Provides a quick overview of the product's features and benefits.
45+
---
46+
47+
`freeze_columns` keeps the first column visible when scrolling sideways. `freeze_headers` keeps the column names visible when scrolling down. The footer in this example scrolls normally; set `freeze_footers` to pin it too.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#### Dynamic column names in a table
2+
3+
In all the previous examples, the column names were hardcoded in the SQL query.
4+
This makes it very easy to quickly visualize the results of a query as a table,
5+
but it can be limiting if you want to include columns that are not known in advance.
6+
In situations when the number and names of the columns depend on the data, or on variables,
7+
you can use the `dynamic` component to generate the table columns dynamically.
8+
9+
For that, you will need to return JSON objects from your SQL query, where the keys are the column names,
10+
and the values are the cell contents.
11+
12+
Databases [offer utilities to generate JSON objects from query results](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide)
13+
- In PostgreSQL, you can use the [`json_build_object`](https://www.postgresql.org/docs/current/functions-json.html#FUNCTIONS-JSON-PROCESSING)
14+
function for a fixed number of columns, or [`json_object_agg`](https://www.postgresql.org/docs/current/functions-aggregate.html#FUNCTIONS-AGGREGATE) for a dynamic number of columns.
15+
- In SQLite, you can use the [`json_object`](https://www.sqlite.org/json1.html) function for a fixed number of columns,
16+
or the `json_group_object` function for a dynamic number of columns.
17+
- In MySQL, you can use the [`JSON_OBJECT`](https://dev.mysql.com/doc/refman/8.0/en/json-creation-functions.html#function_json-object) function for a fixed number of columns,
18+
or the [`JSON_OBJECTAGG`](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_json-objectagg) function for a dynamic number of columns.
19+
- In Microsoft SQL Server, you can use the [`FOR JSON PATH`](https://docs.microsoft.com/en-us/sql/relational-databases/json/format-query-results-as-json-with-for-json-sql-server?view=sql-server-ver15) clause.
20+
21+
For instance, let's say we have a table with three columns: store, item, and quantity_sold.
22+
We want to create a pivot table where each row is a store, and each column is an item.
23+
We will return a set of json objects that look like this: `{"store":"Madrid", "Item1": 42, "Item2": 7, "Item3": 0}`
24+
25+
```sql
26+
SELECT 'table' AS component;
27+
with filled_data as (
28+
select
29+
stores.store, items.item,
30+
(select coalesce(sum(quantity_sold), 0) from store_sales where store=stores.store and item=items.item) as quantity
31+
from (select distinct store from store_sales) as stores
32+
cross join (select distinct item from store_sales) as items
33+
order by stores.store, items.item
34+
)
35+
SELECT
36+
'dynamic' AS component,
37+
JSON_PATCH( -- SQLite-specific, refer to your database documentation for the equivalent JSON functions
38+
JSON_OBJECT('store', store),
39+
JSON_GROUP_OBJECT(item, quantity)
40+
) AS properties
41+
FROM
42+
filled_data
43+
GROUP BY
44+
store;
45+
```
46+
47+
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.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
rows:
3+
- component: table
4+
edit_url: /examples/show_variables.sql?action=edit&update_id={id}
5+
delete_url: /examples/show_variables.sql?action=delete&delete_id={id}
6+
custom_actions:
7+
name: history
8+
tooltip: View Standard History
9+
link: /examples/show_variables.sql?action=history&standard_id={id}
10+
icon: history
11+
- name: CalStd
12+
vendor: PharmaCo
13+
Product: P1234
14+
lot number: T23523
15+
status: Available
16+
expires on: '2026-10-13'
17+
_sqlpage_id: 32
18+
_sqlpage_actions:
19+
- name: View PDF
20+
tooltip: View Presentation
21+
link: https://sql-page.com/pgconf/2024-sqlpage-badass.pdf
22+
icon: file-type-pdf
23+
- name: Action
24+
tooltip: Set In Use
25+
link: /examples/show_variables.sql?action=set_in_use&standard_id=32
26+
icon: caret-right
27+
- name: CalStd
28+
vendor: PharmaCo
29+
Product: P1234
30+
lot number: T2352
31+
status: In Use
32+
expires on: '2026-10-14'
33+
_sqlpage_id: 33
34+
_sqlpage_actions:
35+
- name: View PDF
36+
tooltip: View Presentation
37+
link: https://sql-page.com/pgconf/2024-sqlpage-badass.pdf
38+
icon: file-type-pdf
39+
- name: Action
40+
tooltip: Retire Standard
41+
link: /examples/show_variables.sql?action=retire&standard_id=33
42+
icon: test-pipe-off
43+
- name: CalStd
44+
vendor: PharmaCo
45+
Product: P1234
46+
lot number: A123
47+
status: Discarded
48+
expires on: '2026-09-30'
49+
_sqlpage_id: 31
50+
_sqlpage_actions:
51+
- name: View PDF
52+
tooltip: View Presentation
53+
link: https://sql-page.com/pgconf/2024-sqlpage-badass.pdf
54+
icon: file-type-pdf
55+
- name: Action
56+
---
57+
58+
## Using Action Buttons in a table.
59+
60+
### Preset Actions: `edit_url` & `delete_url`
61+
Since edit and delete are common actions, the `table` component has dedicated `edit_url` and `delete_url` properties to add buttons for these actions.
62+
The value of these properties should be a URL, containing the `{id}` placeholder that will be replaced by the value of the `_sqlpage_id` property for that row.
63+
64+
### Column with fixed action buttons
65+
66+
You may want to add custom action buttons to your table rows, for instance to view details, download a file, or perform a custom operation.
67+
For this, the `table` component has a `custom_actions` top-level property that lets you define a column of buttons, each button defined by a name, an icon, a link, and an optional tooltip.
68+
69+
### Column with variable action buttons
70+
71+
The `table` component also supports the row level `_sqlpage_actions` column in your data table.
72+
This is helpful if you want a more complex logic, for instance to disable a button on some rows, or to change the link or icon based on the row data.
73+
74+
> WARNING!
75+
> If the number of array items in `_sqlpage_actions` is not consistent across all rows, the table may not render correctly.
76+
> You can leave blank spaces by including an object with only the `name` property.
77+
78+
The table has a column of buttons, each button defined by the `custom_actions` column at the table level, and by the `_sqlpage_actions` property at the row level.
79+
80+
### `custom_actions` & `_sqlpage_actions` JSON properties.
81+
82+
Each button is defined by the following properties:
83+
* `name`: sets the column header and the tooltip if no tooltip is provided,
84+
* `tooltip`: text to display when hovering over the button,
85+
* `link`: the URL to navigate to when the button is clicked, possibly containing the `{id}` placeholder that will be replaced by the value of the `_sqlpage_id` property for that row,
86+
* `icon`: the tabler icon name or image link to display on the button
87+
88+
### Example using all of the above

0 commit comments

Comments
 (0)