Skip to content

Commit 7cbe89a

Browse files
committed
custom background colors in tables
Closes #25
1 parent 42bd7d4 commit 7cbe89a

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Add a new `icon` attribute to the [table](https://sql.ophir.dev/documentation.sql?component=table#component) component to display icons in the table.
1313
- Fix `textarea` fields in the [form](https://sql.ophir.dev/documentation.sql?component=table#component) component to display the provided `value` attribute. Thanks Frank for the contribution !
1414
- SQLPage now guarantees that a single web request will be handled by a single database connection. Previously, connections were repeatedly taken and put back to the connection pool between each statement, preventing the use of temporary tables, transactions, and other connection-specific features such as [`last_insert_rowid`](https://www.sqlite.org/lang_corefunc.html#last_insert_rowid). This makes it much easier to keep state between SQL statements in a single `.sql` file. Please report any performance regression you might encounter. See [the many-to-many relationship example](./examples/modeling%20a%20many%20to%20many%20relationship%20with%20a%20form/).
15+
- The [table](https://sql.ophir.dev/documentation.sql?component=table#component) component now supports setting a custom background color, and a custom CSS class on a given table line.
1516

1617
## 0.7.2 (2023-07-10)
1718

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,10 @@ INSERT INTO parameter(component, name, description, type, top_level, optional) S
351351
('sort', 'Make the columns clickable to let the user sort by the value contained in the column.', 'BOOLEAN', TRUE, TRUE),
352352
('search', 'Add a search bar at the top of the table, letting users easily filter table rows by value.', 'BOOLEAN', TRUE, TRUE),
353353
('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),
354-
('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)
354+
('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),
355+
-- row level
356+
('_sqlpage_css_class', 'For advanced users. Sets a css class on the table row.', 'TEXT', FALSE, TRUE),
357+
('_sqlpage_color', 'Sets the background color of the row.', 'TEXT', FALSE, TRUE)
355358
) x;
356359

357360
INSERT INTO example(component, description, properties) VALUES
@@ -363,7 +366,7 @@ INSERT INTO example(component, description, properties) VALUES
363366
'{"Forename": "Linus", "Surname": "Torvalds", "Pseudonym": "torvalds"}]')),
364367
('table', 'A table that uses markdown to display links',
365368
json('[{"component":"table", "markdown": "Documentation", "icon": "icon"}, '||
366-
'{"icon": "table", "name": "Table", "description": "Displays SQL results as a searchable table.", "Documentation": "[docs](documentation.sql?component=table)"},
369+
'{"icon": "table", "name": "Table", "description": "Displays SQL results as a searchable table.", "Documentation": "[docs](documentation.sql?component=table)", "_sqlpage_color": "red"},
367370
{"icon": "timeline", "name": "Chart", "description": "Show graphs based on numeric data.", "Documentation": "[docs](documentation.sql?component=chart)"}
368371
]'));
369372

sqlpage/templates/table.handlebars

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,24 @@
1212
<thead>
1313
<tr>
1414
{{#each this}}
15+
{{~#if (not (starts_with @key '_sqlpage_'))~}}
1516
<th>
1617
{{#if ../../sort}}
1718
<button class="table-sort sort" data-sort="{{@key}}">{{@key}}</button>
1819
{{else}}
1920
{{@key}}
2021
{{/if}}
2122
</th>
23+
{{~/if~}}
2224
{{/each}}
2325
</tr>
2426
</thead>
2527
<tbody class="table-tbody list">{{#delay}}</tbody>{{/delay}}
2628
{{/if}}
2729

28-
<tr>
30+
<tr class="{{_sqlpage_css_class}} {{#if _sqlpage_color}}bg-{{_sqlpage_color}}-lt{{/if}}">
2931
{{~#each this~}}
32+
{{~#if (not (starts_with @key '_sqlpage_'))~}}
3033
<td class="{{@key}} align-middle">
3134
{{~#if (array_contains ../../markdown @key)~}}
3235
{{{markdown this}}}
@@ -38,6 +41,7 @@
3841
{{~/if~}}
3942
{{~/if~}}
4043
</td>
44+
{{~/if~}}
4145
{{~/each~}}
4246
</tr>
4347
{{~/each_row~}}

src/templates.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ impl AllTemplates {
203203

204204
handlebars.register_helper("sum", Box::new(sum_helper));
205205

206+
handlebars_helper!(starts_with: |s: str, prefix:str| s.starts_with(prefix));
207+
handlebars.register_helper("starts_with", Box::new(starts_with));
208+
206209
// to_array: convert a value to a single-element array. If the value is already an array, return it as-is.
207210
handlebars_helper!(to_array: |x: Json| match x {
208211
JsonValue::Array(arr) => arr.clone(),

0 commit comments

Comments
 (0)