datatables icon indicating copy to clipboard operation
datatables copied to clipboard

How to sort by joined data

Open dfeinzeig opened this issue 5 years ago • 1 comments

If I have users and addresses and i'm creating a table of addresses, i want to also show user name and sort by it, not just by address.user_id. how can i do this?

    table = DataTable(request.args, Address, db.session.query(Address).join(User),
        [("address_line_1"),
        ("address_line_2"),
        ("zip",),
        ("user", "user_id", lambda i: i.name),  <-- this shows name in the table, but sorts by id i think
        ("user", "user.name"),  <-- this does not work at all
    ])

dfeinzeig avatar Dec 17 '19 22:12 dfeinzeig

ok, i think i've figured this out. seems like the join to User is unnecessary and results in alias issues.

this seems to work, including properly sorting by user.name instead of user_id:

    table = DataTable(request.args, Address, db.session.query(Address),
        [("address_line_1"),
        ("address_line_2"),
        ("zip",),
        ("user", "user.name", lambda i: i.name)
    ])

it seems the filter for the user will get passed a User object.

dfeinzeig avatar Dec 18 '19 16:12 dfeinzeig