diff --git a/examples/official-site/sqlpage/migrations/13_tab.sql b/examples/official-site/sqlpage/migrations/13_tab.sql index 162386f15..1fd5ad472 100644 --- a/examples/official-site/sqlpage/migrations/13_tab.sql +++ b/examples/official-site/sqlpage/migrations/13_tab.sql @@ -92,6 +92,7 @@ For instance: `SELECT ''text'' AS component, contents_md FROM my_page_contents W Or you could write different queries for different tabs and use the `$tab` parameter with a static value in a where clause to switch between tabs: ```sql +set tab = coalesce($tab, ''Projects''); select ''tab'' as component; select ''Projects'' as title, $tab = ''Projects'' as active; select ''Tasks'' as title, $tab = ''Tasks'' as active; @@ -103,7 +104,7 @@ select * from my_tasks where $tab = ''Tasks''; ``` Note that the example below is completely static, and does not use the `tab` parameter to actually switch between tabs. -View the [dynamic tabs example](/examples/tabs/). +The query above defaults to Projects on the first visit; the links then select either `?tab=Projects` or `?tab=Tasks`. ', JSON( '[ diff --git a/examples/official-site/sqlpage/migrations/70_pagination.sql b/examples/official-site/sqlpage/migrations/70_pagination.sql index 5bb50d01a..d791e3a29 100644 --- a/examples/official-site/sqlpage/migrations/70_pagination.sql +++ b/examples/official-site/sqlpage/migrations/70_pagination.sql @@ -13,10 +13,11 @@ The pagination component displays navigation buttons (first, previous, next, las For large numbers of pages, an offset can limit the visible page links. -A minimal example of a SQL query that uses the pagination would be: +This SQLite example uses the first page when no offset is supplied. Order by a unique key so successive pages have a stable order. ```sql +set offset = max(0, coalesce(cast($offset as integer), 0)); select ''table'' as component; -select * from my_table limit 100 offset $offset; +select * from my_table order by id limit 100 offset cast($offset as integer); select ''pagination'' as component; with recursive pages as ( @@ -28,7 +29,7 @@ with recursive pages as ( select (offset/100+1) as contents, sqlpage.link(sqlpage.path(), json_object(''offset'', offset)) as link, - offset = coalesce(cast($offset as integer), 0) as active + offset = cast($offset as integer) as active from pages; ``` diff --git a/examples/official-site/sqlpage/migrations/71_blog_pagination.sql b/examples/official-site/sqlpage/migrations/71_blog_pagination.sql index 859b9e8cc..0a2a34f43 100644 --- a/examples/official-site/sqlpage/migrations/71_blog_pagination.sql +++ b/examples/official-site/sqlpage/migrations/71_blog_pagination.sql @@ -11,7 +11,7 @@ VALUES To display a large number of records from a database, it is often practical to split these data into pages. The user can thus navigate from one page to another, as well as directly to the first or last page. With SQLPage, it is possible to perform these operations using the pagination component. -This component offers many options, and I recommend consulting its documentation before proceeding with the rest of this tutorial. +See the [pagination reference](/component.sql?component=pagination) for the available controls. Of course, this component only handles its display and does not implement any logic for data processing or state changes. In this tutorial, we will implement a complete example of using the pagination component with a SQLite database, but the code should work without modification (or with very little modification) with any relational database management system (RDBMS). @@ -29,7 +29,7 @@ SET MAX_PAGES = 10; Now, we need to know the number of rows present in the table to be displayed. We can then calculate the number of pages required. ``` -SET records_count = (SELECT COUNT(*) FROM album); +SET records_count = (SELECT COUNT(*) FROM users); SET pages_count = (CAST($records_count AS INTEGER) / CAST($MAX_RECORD_PER_PAGE AS INTEGER)); ``` @@ -62,13 +62,14 @@ We can now read and display the data based on the active page. To do this, we si ``` SELECT - ''table'' as component + ''table'' as component; SELECT user_id AS id, last_name AS "Last name", first_name AS "First name" FROM users +ORDER BY user_id LIMIT CAST($MAX_RECORD_PER_PAGE AS INTEGER) OFFSET (CAST($page AS INTEGER) - 1) * CAST($MAX_RECORD_PER_PAGE AS INTEGER); ``` @@ -146,10 +147,12 @@ The final step is to generate the page numbers based on the number of pages and ``` WITH RECURSIVE page_numbers AS ( - SELECT $idx_page AS number + SELECT CAST($idx_page AS INTEGER) AS number + WHERE CAST($idx_page AS INTEGER) <= CAST($pages_count AS INTEGER) UNION ALL SELECT number + 1 FROM page_numbers + WHERE number < CAST($pages_count AS INTEGER) LIMIT CAST($MAX_PAGES AS INTEGER) ) SELECT