Skip to content

Commit fc4ba7d

Browse files
committed
new align_right attribute in the table component
fixes #115
1 parent ecc172a commit fc4ba7d

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 0.15.0
44

5+
- Add a new `align_right` attribute to the [table](https://sql.ophir.dev/documentation.sql?component=table#component) component to align a column to the right.
56
- Fix display of long titles in the shell component.
67
- New [`sqlpage.variables`](https://sql.ophir.dev/functions.sql?function=variables#function) function for easy handling of complex forms
78
- `sqlpage.variables('get')` returns a json object containing all url parameters. Inside `/my_page.sql?x=1&y=2`, it returns the string `'{"x":"1","y":"2"}'`

examples/official-site/documentation.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,22 @@ select
116116
WHEN 'false' THEN 'FALSE'
117117
WHEN 'null' THEN 'NULL'
118118
ELSE quote(t.value)
119-
END as key,
119+
END as val,
120120
CASE parent.fullkey
121121
WHEN '$' THEN t.key
122122
ELSE parent.key
123-
END as val
123+
END as key
124124
from t inner join t parent on parent.id = t.parent
125125
where t.atom is not null
126126
),
127127
key_val_padding as (select
128-
key,
128+
CASE WHEN key LIKE '% %' THEN format('"%s"', replace(key, '"', '""')) ELSE key END as key,
129129
val,
130-
1 + max(0, max(case when length(key) < 30 then length(key) else 0 end) over () - length(key)) as padding
130+
1 + max(0, max(case when length(val) < 30 then length(val) else 0 end) over () - length(val)) as padding
131131
from key_val
132132
)
133133
select group_concat(
134-
format(' %s%.*cas %s', key, padding, ' ', val),
134+
format(' %s%.*cas %s', val, padding, ' ', key),
135135
',' || char(10)
136136
) from key_val_padding
137137
) || ';',

examples/official-site/sqlpage/migrations/01_documentation.sql

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ INSERT INTO parameter(component, name, description, type, top_level, optional) S
406406
('search', 'Add a search bar at the top of the table, letting users easily filter table rows by value.', 'BOOLEAN', TRUE, TRUE),
407407
('markdown', 'Set this to the name of a column whose content should be interpreted as markdown . Used to display rich text with links in the table. This argument can be repeated multiple times to intepret multiple columns as markdown.', 'TEXT', TRUE, TRUE),
408408
('icon', 'Set this to the name of a column whose content should be interpreted as a tabler icon name. Used to display icons in the table. This argument can be repeated multiple times to intepret multiple columns as icons. Introduced in v0.8.0.', 'TEXT', TRUE, TRUE),
409+
('align_right', 'Name of a column the contents of which should be right-aligned. This argument can be repeated multiple times to align multiple columns to the right. Introduced in v0.15.0.', 'TEXT', TRUE, TRUE),
409410
-- row level
410411
('_sqlpage_css_class', 'For advanced users. Sets a css class on the table row. Added in v0.8.0.', 'TEXT', FALSE, TRUE),
411412
('_sqlpage_color', 'Sets the background color of the row. Added in v0.8.0.', 'TEXT', FALSE, TRUE)
@@ -425,12 +426,12 @@ INSERT INTO example(component, description, properties) VALUES
425426
]')),
426427
(
427428
'table',
428-
'A table with dashes',
429+
'A table with numbers',
429430
json(
430-
'[{"component":"table", "search": true, "sort": true}, ' ||
431-
'{"id": 31456, "part_no": "MIC-ROCC-F-23-206-C"},
432-
{"id": 996, "part_no": "MIC-ROCC-F-24-206-A"},
433-
{"id": 131456, "part_no": "KIB-ROCC-F-13-205-B"}
431+
'[{"component":"table", "search": true, "sort": true, "align_right": ["Price ($)", "Amount in stock"]}, ' ||
432+
'{"id": 31456, "part_no": "MIC-ROCC-F-23-206-C", "Price ($)": 12, "Amount in stock": 5},
433+
{"id": 996, "part_no": "MIC-ROCC-F-24-206-A", "Price ($)": 1, "Amount in stock": 15},
434+
{"id": 131456, "part_no": "KIB-ROCC-F-13-205-B", "Price ($)": 127, "Amount in stock": 9}
434435
]'
435436
)
436437
);

sqlpage/templates/table.handlebars

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
<tr>
1414
{{#each this}}
1515
{{#if (not (starts_with @key '_sqlpage_'))}}
16-
<th>
16+
<th class="
17+
{{~#if (array_contains ../../align_right @key)}} text-end {{/if~}}
18+
">
1719
{{~#if ../../sort~}}
18-
<button class="table-sort sort" data-sort="{{@key}}">{{@key}}</button>
20+
<button class="table-sort sort d-inline" data-sort="{{@key}}">{{@key}}</button>
1921
{{~else~}}
2022
{{~@key~}}
2123
{{~/if~}}
@@ -30,7 +32,11 @@
3032
<tr class="{{_sqlpage_css_class}} {{#if _sqlpage_color}}bg-{{_sqlpage_color}}-lt{{/if}}">
3133
{{~#each this~}}
3234
{{~#if (not (starts_with @key '_sqlpage_'))~}}
33-
<td class="{{@key}} align-middle">
35+
<td class="
36+
{{~@key~}}
37+
{{~#if (array_contains ../../align_right @key)
38+
}} text-end {{
39+
/if}} align-middle">
3440
{{~#if (array_contains ../../markdown @key)~}}
3541
{{{markdown this}}}
3642
{{~else~}}

0 commit comments

Comments
 (0)