mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
sortable_columns also now works with views
This commit is contained in:
parent
5c02a6b0f4
commit
61f51d9ae6
4 changed files with 42 additions and 18 deletions
|
|
@ -365,7 +365,8 @@ class TableView(RowTableShared):
|
||||||
sortable_columns = set()
|
sortable_columns = set()
|
||||||
if not is_view:
|
if not is_view:
|
||||||
table_rows_count = table_info["count"]
|
table_rows_count = table_info["count"]
|
||||||
sortable_columns = self.sortable_columns_for_table(name, table, use_rowid)
|
|
||||||
|
sortable_columns = self.sortable_columns_for_table(name, table, use_rowid)
|
||||||
|
|
||||||
# Allow for custom sort order
|
# Allow for custom sort order
|
||||||
sort = special_args.get("_sort")
|
sort = special_args.get("_sort")
|
||||||
|
|
|
||||||
|
|
@ -121,6 +121,23 @@ This will restrict sorting of ``example_table`` to just the ``height`` and
|
||||||
|
|
||||||
You can also disable sorting entirely by setting ``"sortable_columns": []``
|
You can also disable sorting entirely by setting ``"sortable_columns": []``
|
||||||
|
|
||||||
|
By default, database views in Datasette do not support sorting. You can use ``sortable_columns`` to enable specific sort orders for a view called ``name_of_view`` in the database ``my_database`` like so::
|
||||||
|
|
||||||
|
{
|
||||||
|
"databases": {
|
||||||
|
"my_database": {
|
||||||
|
"tables": {
|
||||||
|
"name_of_view": {
|
||||||
|
"sortable_columns": [
|
||||||
|
"clicks",
|
||||||
|
"impressions"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.. _label_columns:
|
.. _label_columns:
|
||||||
|
|
||||||
Specifying the label column for a table
|
Specifying the label column for a table
|
||||||
|
|
|
||||||
|
|
@ -158,6 +158,9 @@ METADATA = {
|
||||||
'primary_key_multiple_columns_explicit_label': {
|
'primary_key_multiple_columns_explicit_label': {
|
||||||
'label_column': 'content2',
|
'label_column': 'content2',
|
||||||
},
|
},
|
||||||
|
'simple_view': {
|
||||||
|
'sortable_columns': ['content'],
|
||||||
|
}
|
||||||
},
|
},
|
||||||
'queries': {
|
'queries': {
|
||||||
'pragma_cache_size': 'PRAGMA cache_size;',
|
'pragma_cache_size': 'PRAGMA cache_size;',
|
||||||
|
|
|
||||||
|
|
@ -40,11 +40,6 @@ def test_sql_time_limit(app_client_shorter_time_limit):
|
||||||
assert expected_html_fragment in response.text
|
assert expected_html_fragment in response.text
|
||||||
|
|
||||||
|
|
||||||
def test_view(app_client):
|
|
||||||
response = app_client.get('/fixtures/simple_view')
|
|
||||||
assert response.status == 200
|
|
||||||
|
|
||||||
|
|
||||||
def test_row(app_client):
|
def test_row(app_client):
|
||||||
response = app_client.get(
|
response = app_client.get(
|
||||||
'/fixtures/simple_primary_key/1',
|
'/fixtures/simple_primary_key/1',
|
||||||
|
|
@ -618,25 +613,33 @@ def test_compound_primary_key_with_foreign_key_references(app_client):
|
||||||
|
|
||||||
|
|
||||||
def test_view_html(app_client):
|
def test_view_html(app_client):
|
||||||
response = app_client.get('/fixtures/simple_view')
|
response = app_client.get("/fixtures/simple_view")
|
||||||
assert response.status == 200
|
assert response.status == 200
|
||||||
table = Soup(response.body, 'html.parser').find('table')
|
table = Soup(response.body, "html.parser").find("table")
|
||||||
assert [
|
ths = table.select("thead th")
|
||||||
'content', 'upper_content'
|
assert 2 == len(ths)
|
||||||
] == [th.string.strip() for th in table.select('thead th')]
|
assert ths[0].find("a") is not None
|
||||||
|
assert ths[0].find("a")["href"].endswith("/simple_view?_sort=content")
|
||||||
|
assert ths[0].find("a").string.strip() == "content"
|
||||||
|
assert ths[1].find("a") is None
|
||||||
|
assert ths[1].string.strip() == "upper_content"
|
||||||
expected = [
|
expected = [
|
||||||
[
|
[
|
||||||
'<td class="col-content">hello</td>',
|
'<td class="col-content">hello</td>',
|
||||||
'<td class="col-upper_content">HELLO</td>'
|
'<td class="col-upper_content">HELLO</td>',
|
||||||
], [
|
],
|
||||||
|
[
|
||||||
'<td class="col-content">world</td>',
|
'<td class="col-content">world</td>',
|
||||||
'<td class="col-upper_content">WORLD</td>'
|
'<td class="col-upper_content">WORLD</td>',
|
||||||
], [
|
],
|
||||||
|
[
|
||||||
'<td class="col-content">\xa0</td>',
|
'<td class="col-content">\xa0</td>',
|
||||||
'<td class="col-upper_content">\xa0</td>'
|
'<td class="col-upper_content">\xa0</td>',
|
||||||
]
|
],
|
||||||
|
]
|
||||||
|
assert expected == [
|
||||||
|
[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")
|
||||||
]
|
]
|
||||||
assert expected == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')]
|
|
||||||
|
|
||||||
|
|
||||||
def test_index_metadata(app_client):
|
def test_index_metadata(app_client):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue