使用Flask Admin包查看数据库表,如何使单元格中的值右对齐?请参阅下面的代码。
class VwSalesByPersonYearView(MyModelView):
"""VwSalesByPersonYear model view"""
can_create = False
can_edit = False
can_delete = False
column_display_pk = False
can_set_page_size = True
page_size = 50
column_list = (
"service_year",
"name_",
"sales_total",
)
column_labels = {
"service_year": "Year",
"name_": "Employee",
"sales_total": "Total Sales",
}
column_descriptions = {
"service_year": "Year of the sales",
"sales_total": "Total sales (i.e. labour + parts)",
}
column_sortable_list = (
"service_year",
"name_",
"sales_total",
)
column_default_sort = [
("service_year", True),
("sales_total", True),
("name_", True),
]
column_filters = (
"service_year",
"name_",
"sales_total",
)
column_searchable_list = (
"service_year",
"name_",
)
column_formatters = {
"service_year": lambda v, c, m, n: f"{m.service_year:.0f}",
"sales_total": lambda v, c, m, n: f"{m.sales_total or 0:,.0f}",
}
我找到了一个解决方案:在column_formatters中使用
Markup
和style="text-align: right;"
:
column_formatters = {
"service_year": lambda v, c, m, n: f"{m.service_year:.0f}",
"sales_total": lambda v, c, m, n: Markup(f'<div style="text-align: right;">{m.sales_total or 0:,.0f}</div>'),
}