单元格颜色Django-Tables2 [英] cell color Django-Tables2
问题描述
问题:在哪里可以编辑我的django代码以根据业务逻辑更改单个单元格的背景颜色?
在我的观点.py我有逻辑来捕捉'pts'列的最大值:
In my views.py I have logic that captures the max value of column 'pts':
def show_teams(request):
reg = Teamoffense.objects.filter(~Q(rk='RK'))
pts = Teamoffense.objects.filter(~Q(pts='PTS')).values('pts')
seq = [item['pts'] for item in pts]
maxseq = max(seq)
table = SimpleTable(reg)
table_to_report = RequestConfig(request).configure(table)
if table_to_report:
return create_report_http_response(table_to_report, request)
return render(request, 'index.html', {
'table': table,
'reg': reg,
'maxseq': maxseq,
})
如何使用该列中的最大值渲染任何单元格bgcolor ='green'?我目前有一个简单的表格显示l ike so:
How can I render any cell with the max value in that column a bgcolor = 'green'? I currently have a simple table that displays like so:
class SimpleTable(TableReport):
class Meta:
model = Teamoffense
exclude = ("column1","column2")
exclude_from_report = ("column1","column2")
attrs = {'class': 'paleblue'}
推荐答案
经过更多研究,查看 Django-Tables2 API文档我发现 Table.render_foo方法是我的情况需要的。这会改变列的呈现方式。确保设置 column.attrs 而不是self.attrs,因为在我的经验中,我是如何设置单个单元格样式。
After more research looking into the Django-Tables2 API Docs I found that the Table.render_foo methods is what was needed in my case. This changes how the column is rendered. Make sure to set the column.attrs and not self.attrs because in my experience this is how I was able to set the individual cell style.
#tables.py
import django_tables2 as tables
from .models import MyTable
from MyApp import views
class SimpleTable(tables.Table):
def __init__(self, *args, **kwargs):
super(SimpleTable, self).__init__(*args, **kwargs)
self.maxpts = views.maxpts
#render_foo example method
def render_pts(self, value, column):
if value == self.maxpts:
column.attrs = {'td': {'bgcolor': 'lightgreen'}}
else:
column.attrs = {'td': {}}
return value
class Meta:
model = MyTable
attrs = {'class': 'paleblue'}
这篇关于单元格颜色Django-Tables2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!