Pegase745 / sqlalchemy-datatables

SQLAlchemy integration of jQuery DataTables >= 1.10.x (Pyramid and Flask examples)
MIT License
159 stars 67 forks source link

How to append custom column? #84

Closed tecnopolis-ve closed 7 years ago

tecnopolis-ve commented 7 years ago

Hello

This plugin is very useful! I'm using it with flask and sqlalchemy, but I've a question; how can I put a custom text (in this case, a link) as a column?

For example, I've defined

columns = [
    ColumnDT(Location.id),
    ColumnDT(Location.name),
    ColumnDT(Location.location),
    ColumnDT(Location.phone),
    ColumnDT('CUSTOM URL')
]

"Custom URL" is any url generated by flask (edit, more info of current row, etc)

I know that I can personalize the rows using javascript, but certain urls are better if rendered by flask directly.

Any thought or ideas?

Thanks a lot!

tdamsma commented 7 years ago

This is not how I would do it, but if you insist:

from sqlalchemy.sql import literal_column

columns = [
    ColumnDT(Location.id),
    ColumnDT(Location.name),
    ColumnDT(Location.location),
    ColumnDT(Location.phone),
    ColumnDT(literal_column("'my_custom_url'").label('a_nice_name')),
]

Note that the quotes matter, you have too pass a string wrapped in single quotes to the literal_column.

I would do personally do this in JS. If you generate the JS from within a template, you can add the flask generated url to to the JS in this way.

tecnopolis-ve commented 7 years ago

@tdamsma thanks a lot!

This information appear in the API? because I wasn't able to find it.

Many thanks again!

tdamsma commented 7 years ago

Which bit do you mean?