question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multi column sort with column_default_sort

See original GitHub issue

Currently Flask-Admin doesn’t support sorting on multiple columns. Could you implement this?

column_default_sort = [(Model1.colA, True), ('colB', False)]

and

column_default_sort = [Model1.colA, 'colB']

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:8
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
jbochicommented, Jan 23, 2018

While #1590 is not merged, this will do the trick for sqla model views:

class ModelViewMultipleColSort(ModelView):
    def _get_default_order(self):
        order = self.column_default_sort
        if order:
            if isinstance(order, list):
                order = order
            elif isinstance(order, tuple):
                order = [order]
            else:
                order = [(order, False)]
        for field, direction in (order or []):
            attr, joins = tools.get_field_with_path(self.model, field)
            yield attr, joins, direction

    def _apply_sorting(self, query, joins, sort_column, sort_desc):
        if sort_column is not None:
            if sort_column in self._sortable_columns:
                sort_field = self._sortable_columns[sort_column]
                sort_joins = self._sortable_joins.get(sort_column)
                query, joins = self._order_by(query, joins, sort_joins,
                                              sort_field, sort_desc)
        else:
            order = list(self._get_default_order())
            for sort_field, sort_joins, sort_desc in order:
                query, joins = self._order_by(query, joins, sort_joins,
                                              sort_field, sort_desc)
        return query, joins
2reactions
theguywhodoesthethingcommented, Apr 27, 2018

Thanks @jbochi. Formatting for those of you implementing this fix so you don’t get hung up for an hour like I did.

from flask_admin.contrib.sqla import tools 
column_default_sort = [[Company.name, False], [User.email_address, False]]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Default Sort Order of Multiple Columns in a custom table
Solved: Can I set the default sort order for multiple columns in a custom table? I want to make it visible to everyone,...
Read more >
Sort data in a table - Microsoft Support
Sorting is one of the most common tools for data management. In Excel, you can sort your table by one or more columns,...
Read more >
Row Sorting: Core Feature of our Datagrid - AG Grid
It is possible to sort by multiple columns. The default action for multiple column sorting is for the user to hold down shift...
Read more >
PrimeNg <p-table> Multi Sort with MetaKey default sort (e.g. ...
I have a <p-table> table with sortMode="multiple" . I would like to specify two columns as the default sort when the page is...
Read more >
Solved: Sorting a table using multiple columns
If you want to sort a table by multiple columns, the only option you have is to have a concatenated column with the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found