<th> now gets class="col-X" - plus added col-X documentation

Refs #209
This commit is contained in:
Simon Willison 2018-04-17 19:11:11 -07:00
commit a5792a8c61
No known key found for this signature in database
GPG key ID: 17E2DEA2588B7F52
4 changed files with 25 additions and 7 deletions

View file

@ -522,7 +522,7 @@ class RowTableShared(BaseView):
# or to the simple or compound primary key # or to the simple or compound primary key
if link_column: if link_column:
cells.append({ cells.append({
'column': 'Link', 'column': pks[0] if len(pks) == 1 else 'Link',
'value': jinja2.Markup( 'value': jinja2.Markup(
'<a href="/{database}/{table}/{flat_pks_quoted}">{flat_pks}</a>'.format( '<a href="/{database}/{table}/{flat_pks_quoted}">{flat_pks}</a>'.format(
database=database, database=database,

View file

@ -2,7 +2,7 @@
<thead> <thead>
<tr> <tr>
{% for column in display_columns %} {% for column in display_columns %}
<th scope="col"> <th class="col-{{ column.name|to_css_class }}" scope="col">
{% if not column.sortable %} {% if not column.sortable %}
{{ column.name }} {{ column.name }}
{% else %} {% else %}

View file

@ -66,7 +66,7 @@ The row template (``/dbname/tablename/rowid``) gets::
<body class="row db-dbname table-tablename"> <body class="row db-dbname table-tablename">
The ``db-x`` and ``table-x`` classes use the database or table names themselves IF The ``db-x`` and ``table-x`` classes use the database or table names themselves if
they are valid CSS identifiers. If they aren't, we strip any invalid they are valid CSS identifiers. If they aren't, we strip any invalid
characters out and append a 6 character md5 digest of the original name, in characters out and append a 6 character md5 digest of the original name, in
order to ensure that multiple tables which resolve to the same stripped order to ensure that multiple tables which resolve to the same stripped
@ -82,6 +82,23 @@ Some examples::
"-" => "336d5e" "-" => "336d5e"
"no $ characters" => "no--characters-59e024" "no $ characters" => "no--characters-59e024"
``<td>`` and ``<th>`` elements also get custom CSS classes reflecting the
database column they are representing, for example::
<table>
<thead>
<tr>
<th class="col-id" scope="col">id</th>
<th class="col-name" scope="col">name</th>
</tr>
</thead>
<tbody>
<tr>
<td class="col-Link"><a href="...">1</a></td>
<td class="col-name">SMITH</td>
</tr>
</tbody>
</table>
Custom templates Custom templates
---------------- ----------------

View file

@ -183,13 +183,13 @@ def test_table_html_simple_primary_key(app_client):
assert ['nofollow'] == a['rel'] assert ['nofollow'] == a['rel']
assert [ assert [
[ [
'<td class="col-Link"><a href="/test_tables/simple_primary_key/1">1</a></td>', '<td class="col-id"><a href="/test_tables/simple_primary_key/1">1</a></td>',
'<td class="col-content">hello</td>' '<td class="col-content">hello</td>'
], [ ], [
'<td class="col-Link"><a href="/test_tables/simple_primary_key/2">2</a></td>', '<td class="col-id"><a href="/test_tables/simple_primary_key/2">2</a></td>',
'<td class="col-content">world</td>' '<td class="col-content">world</td>'
], [ ], [
'<td class="col-Link"><a href="/test_tables/simple_primary_key/3">3</a></td>', '<td class="col-id"><a href="/test_tables/simple_primary_key/3">3</a></td>',
'<td class="col-content"></td>' '<td class="col-content"></td>'
] ]
] == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')] ] == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')]
@ -265,6 +265,7 @@ def test_table_html_compound_primary_key(app_client):
for expected_col, th in zip(('pk1', 'pk2', 'content'), ths[1:]): for expected_col, th in zip(('pk1', 'pk2', 'content'), ths[1:]):
a = th.find('a') a = th.find('a')
assert expected_col == a.string assert expected_col == a.string
assert th['class'] == ['col-{}'.format(expected_col)]
assert a['href'].endswith('/compound_primary_key?_sort={}'.format( assert a['href'].endswith('/compound_primary_key?_sort={}'.format(
expected_col expected_col
)) ))
@ -285,7 +286,7 @@ def test_table_html_foreign_key_links(app_client):
table = Soup(response.body, 'html.parser').find('table') table = Soup(response.body, 'html.parser').find('table')
expected = [ expected = [
[ [
'<td class="col-Link"><a href="/test_tables/foreign_key_references/1">1</a></td>', '<td class="col-pk"><a href="/test_tables/foreign_key_references/1">1</a></td>',
'<td class="col-foreign_key_with_label"><a href="/test_tables/simple_primary_key/1">hello</a>\xa0<em>1</em></td>', '<td class="col-foreign_key_with_label"><a href="/test_tables/simple_primary_key/1">hello</a>\xa0<em>1</em></td>',
'<td class="col-foreign_key_with_no_label"><a href="/test_tables/primary_key_multiple_columns/1">1</a></td>' '<td class="col-foreign_key_with_no_label"><a href="/test_tables/primary_key_multiple_columns/1">1</a></td>'
] ]