Kru*_*ake 2 django django-templates django-views django-tables2
问题:我在哪里编辑我的 django 代码以根据业务逻辑更改单个单元格的背景颜色?
在我的 views.py 中,我有捕获列“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,
})
Run Code Online (Sandbox Code Playgroud)
如何在该列中呈现最大值为 bgcolor = 'green' 的任何单元格?我目前有一个简单的表格,显示如下:
class SimpleTable(TableReport):
class Meta:
model = Teamoffense
exclude = ("column1","column2")
exclude_from_report = ("column1","column2")
attrs = {'class': 'paleblue'}
Run Code Online (Sandbox Code Playgroud)
在对Django-Tables2 API 文档进行更多研究后,我发现Table.render_foo 方法是我所需要的。这会更改列的呈现方式。确保设置column.attrs而不是 self.attrs 因为根据我的经验,这是我能够设置单个单元格样式的方式。
#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'}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1803 次 |
| 最近记录: |