rendering hybrid_properties in tables
WolfgangFahl opened this issue · comments
I am trying to display hybrid properties in a table but it does not work out of the box.
see http://fb4demo.bitplan.com/datatable
what is needed to get this working?
def datatable(self):
'''
test data table
'''
icons=BootstrapIcon.query.all()
return render_template('datatable.html',listOfDicts=icons)
class BootstrapIcon(db.Model):
id=Column(types.String(30), primary_key=True)
index=Column(types.Integer)
@hybrid_property
def url(self):
myUrl="https://icons.getbootstrap.com/icons/%s/" % self.id
return myUrl
@hybrid_property
def link(self):
myLink=Link(self.url,self.id)
return myLink
@hybrid_property
def icon(self):
myIcon=Icon(self.id)
myIcon.userdata['#']=self.index
return myIcon
I've worked around the issue by using my own table macro and preparing a list of dicts.
def datatable(self):
'''
test data table
'''
icons=BootstrapIcon.query.all()
dictList=[]
for icon in icons:
dictList.append(icon.asDict())
return render_template('datatable.html',listOfDicts=dictList)
...
def asDict(self):
myDict={
'link': self.link,
'icon': self.icon
}
return myDict
{# table for dict list #}
{% macro table4DictList(dictList) -%}
<table class="table table-bordered table-hover">
<!-- table header -->
{% if dictList %}
<thead class="thead-light">
<tr>
<th>#</th>
{% for key in dictList[0] %}
<th> {{ key }} </th>
{% endfor %}
</tr>
<thead>
<tbody>
<!-- table rows -->
{% for dict_item in dictList %}
<tr>
<td> {{loop.index}} </td>
{% for value in dict_item.values() %}
<td> {{ value|safe }} </td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
{% endif %}
</table>
{%- endmacro %}
Also need rendering cell data that is an URL (starting with https://
or http://
) as a clickable link in the table. Also with an option for the entire table that adds opening in a new tab with target="_blank"
.
Any progress on this issue? Would like to help out if needed.