From 9d154b93151c3fc6d9f86909c91093cc76ef9f22 Mon Sep 17 00:00:00 2001 From: Ophir Lojkine Date: Sat, 28 Nov 2026 23:46:33 +0000 Subject: [PATCH] Move advanced table examples to individual files --- .../table/examples/07-presentation.md | 29 ++ .../components/table/examples/08-empty.md | 7 + .../table/examples/09-frozen-columns.md | 47 ++++ .../table/examples/10-dynamic-columns.md | 47 ++++ .../table/examples/11-action-buttons.md | 88 ++++++ .../sqlpage/migrations/01_documentation.sql | 260 +----------------- 6 files changed, 219 insertions(+), 259 deletions(-) create mode 100644 examples/official-site/content/components/table/examples/07-presentation.md create mode 100644 examples/official-site/content/components/table/examples/08-empty.md create mode 100644 examples/official-site/content/components/table/examples/09-frozen-columns.md create mode 100644 examples/official-site/content/components/table/examples/10-dynamic-columns.md create mode 100644 examples/official-site/content/components/table/examples/11-action-buttons.md diff --git a/examples/official-site/content/components/table/examples/07-presentation.md b/examples/official-site/content/components/table/examples/07-presentation.md new file mode 100644 index 000000000..1b9aa5687 --- /dev/null +++ b/examples/official-site/content/components/table/examples/07-presentation.md @@ -0,0 +1,29 @@ +--- +rows: + - component: table + hover: true + striped_rows: true + description: Some Star Trek Starfleet starships + small: true + initial_search_value: NCC- + - name: USS Enterprise + registry: NCC-1701-C + class: Ambassador + - name: USS Archer + registry: NCC-44278 + class: Archer + - name: USS Endeavour + registry: NCC-06 + class: Columbia + - name: USS Constellation + registry: NCC-1974 + class: Constellation + - name: USS Dakota + registry: NCC-63892 + class: Akira + - name: USS Defiant + registry: IX-74205 + class: Defiant +--- + +A table with some presentation options diff --git a/examples/official-site/content/components/table/examples/08-empty.md b/examples/official-site/content/components/table/examples/08-empty.md new file mode 100644 index 000000000..03e34cdb0 --- /dev/null +++ b/examples/official-site/content/components/table/examples/08-empty.md @@ -0,0 +1,7 @@ +--- +rows: + - component: table + empty_description: Nothing to see here at the moment. +--- + +An empty table with a friendly message diff --git a/examples/official-site/content/components/table/examples/09-frozen-columns.md b/examples/official-site/content/components/table/examples/09-frozen-columns.md new file mode 100644 index 000000000..aa9b3a049 --- /dev/null +++ b/examples/official-site/content/components/table/examples/09-frozen-columns.md @@ -0,0 +1,47 @@ +--- +rows: + - component: table + freeze_columns: true + freeze_headers: true + - feature: SQL Execution + description: Fully compatible with existing databases SQL dialects, executes any SQL query. + benefits: Short learning curve, easy to use, interoperable with existing tools. + - feature: Data Visualization + description: 'Automatic visualizations of query results: graphs, plots, pie charts, heatmaps, etc.' + benefits: Quickly analyze data trends, attractive and easy to understand, no external visualization tools + or languages to learn. + - feature: User Authentication + description: Supports user sessions, from basic auth to single sign-on. + benefits: Secure, enforces access control policies, and provides a customizable security layer. + - feature: APIs + description: Allows building JSON REST APIs and integrating with external APIs. + benefits: Enables automation and integration with other platforms, facilitates data exchange. + - feature: Files + description: File uploads, downloads and processing. Supports local filesystem and database storage. + benefits: Convenient file management, secure data handling, flexible storage options, integrates with existing + systems. + - feature: Maps + description: Supports GeoJSON and is compatible with GIS data for map visualization. + benefits: Geospatial data representation, integrates with geographic information systems. + - feature: Custom Components + description: Build advanced features using HTML, JavaScript, and CSS. + benefits: Tailor-made user experiences, easy to implement custom UI requirements. + - feature: Forms + description: Insert and update data in databases based on user input. + benefits: Simplified data input and management, efficient user interactions with databases. + - feature: DB Compatibility + description: Works with MySQL, PostgreSQL, SQLite, Microsoft SQL Server and compatible databases. + benefits: Broad compatibility with popular database systems, ensures seamless integration. + - feature: Security + description: 'Built-in protection against common web vulnerabilities: no SQL injection, no XSS.' + benefits: Passes audits and security reviews, reduces the risk of data breaches. + - feature: Performance + description: Designed for performance, with a focus on efficient data processing and minimal overhead. + benefits: Quickly processes large datasets, handles high volumes of requests, and minimizes server load. + - _sqlpage_footer: true + feature: Summary + description: Summarizes the features of the product. + benefits: Provides a quick overview of the product's features and benefits. +--- + +`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. diff --git a/examples/official-site/content/components/table/examples/10-dynamic-columns.md b/examples/official-site/content/components/table/examples/10-dynamic-columns.md new file mode 100644 index 000000000..5e686f67a --- /dev/null +++ b/examples/official-site/content/components/table/examples/10-dynamic-columns.md @@ -0,0 +1,47 @@ +#### 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](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide) + - In PostgreSQL, you can use the [`json_build_object`](https://www.postgresql.org/docs/current/functions-json.html#FUNCTIONS-JSON-PROCESSING) +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. + - In SQLite, you can use the [`json_object`](https://www.sqlite.org/json1.html) function for a fixed number of columns, +or the `json_group_object` function for a dynamic number of columns. + - 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, +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. + - 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. + +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}` + +```sql +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. diff --git a/examples/official-site/content/components/table/examples/11-action-buttons.md b/examples/official-site/content/components/table/examples/11-action-buttons.md new file mode 100644 index 000000000..e876665d5 --- /dev/null +++ b/examples/official-site/content/components/table/examples/11-action-buttons.md @@ -0,0 +1,88 @@ +--- +rows: + - component: table + edit_url: /examples/show_variables.sql?action=edit&update_id={id} + delete_url: /examples/show_variables.sql?action=delete&delete_id={id} + custom_actions: + name: history + tooltip: View Standard History + link: /examples/show_variables.sql?action=history&standard_id={id} + icon: history + - name: CalStd + vendor: PharmaCo + Product: P1234 + lot number: T23523 + status: Available + expires on: '2026-10-13' + _sqlpage_id: 32 + _sqlpage_actions: + - name: View PDF + tooltip: View Presentation + link: https://sql-page.com/pgconf/2024-sqlpage-badass.pdf + icon: file-type-pdf + - name: Action + tooltip: Set In Use + link: /examples/show_variables.sql?action=set_in_use&standard_id=32 + icon: caret-right + - name: CalStd + vendor: PharmaCo + Product: P1234 + lot number: T2352 + status: In Use + expires on: '2026-10-14' + _sqlpage_id: 33 + _sqlpage_actions: + - name: View PDF + tooltip: View Presentation + link: https://sql-page.com/pgconf/2024-sqlpage-badass.pdf + icon: file-type-pdf + - name: Action + tooltip: Retire Standard + link: /examples/show_variables.sql?action=retire&standard_id=33 + icon: test-pipe-off + - name: CalStd + vendor: PharmaCo + Product: P1234 + lot number: A123 + status: Discarded + expires on: '2026-09-30' + _sqlpage_id: 31 + _sqlpage_actions: + - name: View PDF + tooltip: View Presentation + link: https://sql-page.com/pgconf/2024-sqlpage-badass.pdf + icon: file-type-pdf + - name: Action +--- + +## Using Action Buttons in a table. + +### Preset Actions: `edit_url` & `delete_url` +Since edit and delete are common actions, the `table` component has dedicated `edit_url` and `delete_url` properties to add buttons for these actions. +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. + +### Column with fixed action buttons + +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. +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. + +### Column with variable action buttons + +The `table` component also supports the row level `_sqlpage_actions` column in your data table. +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. + +> WARNING! +> If the number of array items in `_sqlpage_actions` is not consistent across all rows, the table may not render correctly. +> You can leave blank spaces by including an object with only the `name` property. + +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. + +### `custom_actions` & `_sqlpage_actions` JSON properties. + +Each button is defined by the following properties: +* `name`: sets the column header and the tooltip if no tooltip is provided, +* `tooltip`: text to display when hovering over the button, +* `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, +* `icon`: the tabler icon name or image link to display on the button + +### Example using all of the above diff --git a/examples/official-site/sqlpage/migrations/01_documentation.sql b/examples/official-site/sqlpage/migrations/01_documentation.sql index d925b1654..9eb93fb81 100644 --- a/examples/official-site/sqlpage/migrations/01_documentation.sql +++ b/examples/official-site/sqlpage/migrations/01_documentation.sql @@ -513,265 +513,7 @@ Numbers can be displayed {"id": 996, "part_no": "SQL-FORMS-86-M", "Price": 1, "Amount in stock": 1234}, {"id": 131456, "part_no": "SQL-CARDS-56-K", "Price": 127, "Amount in stock": 98} ]' - )), - ( - 'table', - 'A table with some presentation options', - json( - '[{"component":"table", - "hover": true, "striped_rows": true, - "description": "Some Star Trek Starfleet starships", - "small": true, "initial_search_value": "NCC-" - }, - {"name": "USS Enterprise", "registry": "NCC-1701-C", "class":"Ambassador"}, - {"name": "USS Archer", "registry": "NCC-44278", "class":"Archer"}, - {"name": "USS Endeavour", "registry": "NCC-06", "class":"Columbia"}, - {"name": "USS Constellation", "registry": "NCC-1974", "class":"Constellation"}, - {"name": "USS Dakota", "registry": "NCC-63892", "class":"Akira"}, - {"name": "USS Defiant", "registry": "IX-74205", "class":"Defiant"} - ]' - )), - ( - 'table', - 'An empty table with a friendly message', - json('[{"component":"table", "empty_description": "Nothing to see here at the moment."}]') - ), - ( - 'table', - '`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.', - json('[ - {"component": "table", "freeze_columns": true, "freeze_headers": true}, - { - "feature": "SQL Execution", - "description": "Fully compatible with existing databases SQL dialects, executes any SQL query.", - "benefits": "Short learning curve, easy to use, interoperable with existing tools." - }, - { - "feature": "Data Visualization", - "description": "Automatic visualizations of query results: graphs, plots, pie charts, heatmaps, etc.", - "benefits": "Quickly analyze data trends, attractive and easy to understand, no external visualization tools or languages to learn." - }, - { - "feature": "User Authentication", - "description": "Supports user sessions, from basic auth to single sign-on.", - "benefits": "Secure, enforces access control policies, and provides a customizable security layer." - }, - { - "feature": "APIs", - "description": "Allows building JSON REST APIs and integrating with external APIs.", - "benefits": "Enables automation and integration with other platforms, facilitates data exchange." - }, - { - "feature": "Files", - "description": "File uploads, downloads and processing. Supports local filesystem and database storage.", - "benefits": "Convenient file management, secure data handling, flexible storage options, integrates with existing systems." - }, - { - "feature": "Maps", - "description": "Supports GeoJSON and is compatible with GIS data for map visualization.", - "benefits": "Geospatial data representation, integrates with geographic information systems." - }, - { - "feature": "Custom Components", - "description": "Build advanced features using HTML, JavaScript, and CSS.", - "benefits": "Tailor-made user experiences, easy to implement custom UI requirements." - }, - { - "feature": "Forms", - "description": "Insert and update data in databases based on user input.", - "benefits": "Simplified data input and management, efficient user interactions with databases." - }, - { - "feature": "DB Compatibility", - "description": "Works with MySQL, PostgreSQL, SQLite, Microsoft SQL Server and compatible databases.", - "benefits": "Broad compatibility with popular database systems, ensures seamless integration." - }, - { - "feature": "Security", - "description": "Built-in protection against common web vulnerabilities: no SQL injection, no XSS.", - "benefits": "Passes audits and security reviews, reduces the risk of data breaches." - }, - { - "feature": "Performance", - "description": "Designed for performance, with a focus on efficient data processing and minimal overhead.", - "benefits": "Quickly processes large datasets, handles high volumes of requests, and minimizes server load." - }, - { - "_sqlpage_footer": true, - "feature": "Summary", - "description": "Summarizes the features of the product.", - "benefits": "Provides a quick overview of the product''s features and benefits." - } -]') - ), - ( - 'table', - '#### 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](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide) - - In PostgreSQL, you can use the [`json_build_object`](https://www.postgresql.org/docs/current/functions-json.html#FUNCTIONS-JSON-PROCESSING) -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. - - In SQLite, you can use the [`json_object`](https://www.sqlite.org/json1.html) function for a fixed number of columns, -or the `json_group_object` function for a dynamic number of columns. - - 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, -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. - - 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. - -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}` - -```sql -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. - -', NULL - ), - ( - 'table', -'## Using Action Buttons in a table. - -### Preset Actions: `edit_url` & `delete_url` -Since edit and delete are common actions, the `table` component has dedicated `edit_url` and `delete_url` properties to add buttons for these actions. -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. - -### Column with fixed action buttons - -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. -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. - -### Column with variable action buttons - -The `table` component also supports the row level `_sqlpage_actions` column in your data table. -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. - -> WARNING! -> If the number of array items in `_sqlpage_actions` is not consistent across all rows, the table may not render correctly. -> You can leave blank spaces by including an object with only the `name` property. - -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. - -### `custom_actions` & `_sqlpage_actions` JSON properties. - -Each button is defined by the following properties: -* `name`: sets the column header and the tooltip if no tooltip is provided, -* `tooltip`: text to display when hovering over the button, -* `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, -* `icon`: the tabler icon name or image link to display on the button - -### Example using all of the above -' - , - json('[ - { - "component": "table", - "edit_url": "/examples/show_variables.sql?action=edit&update_id={id}", - "delete_url": "/examples/show_variables.sql?action=delete&delete_id={id}", - "custom_actions": { - "name": "history", - "tooltip": "View Standard History", - "link": "/examples/show_variables.sql?action=history&standard_id={id}", - "icon": "history" - } - }, - { - "name": "CalStd", - "vendor": "PharmaCo", - "Product": "P1234", - "lot number": "T23523", - "status": "Available", - "expires on": "2026-10-13", - "_sqlpage_id": 32, - "_sqlpage_actions": [ - { - "name": "View PDF", - "tooltip": "View Presentation", - "link": "https://sql-page.com/pgconf/2024-sqlpage-badass.pdf", - "icon": "file-type-pdf" - }, - { - "name": "Action", - "tooltip": "Set In Use", - "link": "/examples/show_variables.sql?action=set_in_use&standard_id=32", - "icon": "caret-right" - } - ] - }, - { - "name": "CalStd", - "vendor": "PharmaCo", - "Product": "P1234", - "lot number": "T2352", - "status": "In Use", - "expires on": "2026-10-14", - "_sqlpage_id": 33, - "_sqlpage_actions": [ - { - "name": "View PDF", - "tooltip": "View Presentation", - "link": "https://sql-page.com/pgconf/2024-sqlpage-badass.pdf", - "icon": "file-type-pdf" - }, - { - "name": "Action", - "tooltip": "Retire Standard", - "link": "/examples/show_variables.sql?action=retire&standard_id=33", - "icon": "test-pipe-off" - } - ] - }, - { - "name": "CalStd", - "vendor": "PharmaCo", - "Product": "P1234", - "lot number": "A123", - "status": "Discarded", - "expires on": "2026-09-30", - "_sqlpage_id": 31, - "_sqlpage_actions": [ - { - "name": "View PDF", - "tooltip": "View Presentation", - "link": "https://sql-page.com/pgconf/2024-sqlpage-badass.pdf", - "icon": "file-type-pdf" - }, - { - "name": "Action" - } - ] - } -]' -) -); + ));