A
A
Anton Degtyarev2018-06-06 22:00:59
Python
Anton Degtyarev, 2018-06-06 22:00:59

Flask_table + sqlalchemy. How to change the class of a tr tag based on data from the database?

Good afternoon.
How can I change the class of the tr tag in a table generated with flask_table depending on the information I pass to the table with sqlalchemy?
I create the table like this

class Item(object):
        def __init__(self, summ, describe, date, floatto):
            self.summ = summ
            self.describe = describe
            self.date = date
            self.floatto = floatto

        def plus(self):
            return self.floatto.lower().startswith('p')

    class FloatCol(Col):
        def td_format(self, content):
            if content == 'plus':
                return 'Приход'
            elif content == 'minus':
                return 'Расход'
            
    class ItemTable(Table):
        classes = ['table', 'table-hover']
        summ = Col('Сумма')
        describe = Col('Описание')
        date = Col('Число')
        floatto = Col('Тип операции')

    items = Finance.query.all() # Здесь запрос к базе
    table = ItemTable(items)
    finance_table = table.__html__()

I read in the docs that the tag class can be changed in this way:
class ItemTable(Table):
    name = Col('Name')
    description = Col('Description')

    def get_tr_attrs(self, item):
        if item.important():
            return {'class': 'important'}
        else:
            return {}

But alas, it doesn't work that way with a query from the database.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question