Skip to content

Commit

Permalink
Ability to sort using form fields (for mobile portrait mode)
Browse files Browse the repository at this point in the history
We now display sort options as a select box plus a descending checkbox, which
means you can apply sort orders even in portrait mode on a mobile phone where
the column headers are hidden.

Closes #199
  • Loading branch information
simonw committed Apr 10, 2018
1 parent 67982b6 commit 57b19f0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
13 changes: 13 additions & 0 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,18 @@ async def data(self, request, name, hash, table):
forward_querystring=False
)

# Spot ?_sort_by_desc and redirect to _sort_desc=(_sort)
if '_sort_by_desc' in special_args:
return self.redirect(
request,
path_with_added_args(request, {
'_sort_desc': special_args.get('_sort'),
'_sort_by_desc': None,
'_sort': None,
}),
forward_querystring=False
)

filters = Filters(sorted(other_args.items()))
where_clauses, params = filters.build_where_clauses()

Expand Down Expand Up @@ -836,6 +848,7 @@ async def extra_template():
'display_columns': display_columns,
'filter_columns': filter_columns,
'display_rows': display_rows,
'is_sortable': any(c['sortable'] for c in display_columns),
'path_with_added_args': path_with_added_args,
'request': request,
'sort': sort,
Expand Down
8 changes: 4 additions & 4 deletions datasette/static/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ form label {
display: inline-block;
width: 15%;
}
label.sort_by_desc {
width: auto;
padding-right: 1em;
}
form input[type=text],
form input[type=search] {
border: 1px solid #ccc;
Expand Down Expand Up @@ -216,10 +220,6 @@ form input[type=submit] {
.filters input.filter-value {
width: 140px;
}
form input[type=submit] {
display: block;
margin-top: 0.6em;
}
}

a.not-underlined {
Expand Down
17 changes: 16 additions & 1 deletion datasette/templates/table.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,22 @@ <h3>{% if filtered_table_rows_count or filtered_table_rows_count == 0 %}{{ "{:,}
{% endfor %}
</select>
</div><input type="text" name="_filter_value" class="filter-value">
<input type="submit" value="{% if filters.has_selections() %}Apply filters{% else %}Add filter{% endif %}">
</div>
<div class="filter-row">
{% if is_sortable %}
<div class="select-wrapper">
<select name="_sort" id="sort_by">
<option value="">Sort...</option>
{% for column in display_columns %}
{% if column.sortable %}
<option value="{{ column.name }}"{% if column.name == sort or column.name == sort_desc %} selected{% endif %}>Sort by {{ column.name }}</option>
{% endif %}
{% endfor %}
</select>
</div>
<label class="sort_by_desc"><input type="checkbox" name="_sort_by_desc"{% if sort_desc %} checked{% endif %}> descending</label>
{% endif %}
<input type="submit" value="Apply">
</div>
</form>

Expand Down
13 changes: 13 additions & 0 deletions tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ def test_empty_search_parameter_gets_removed(app_client):
)


def test_sort_by_desc_redirects(app_client):
path_base = app_client.get(
'/test_tables/sortable', allow_redirects=False, gather_request=False
).headers['Location']
path = path_base + '?' + urllib.parse.urlencode({
'_sort': 'sortable',
'_sort_by_desc': '1',
})
response = app_client.get(path, allow_redirects=False, gather_request=False)
assert response.status == 302
assert response.headers['Location'].endswith('?_sort_desc=sortable')


@pytest.mark.parametrize('path,expected_classes', [
('/', ['index']),
('/test_tables', ['db', 'db-test_tables']),
Expand Down

0 comments on commit 57b19f0

Please sign in to comment.