Add flexible grid class for v3 grids for width=half etc.
also add 'percent' type renderer, and include column name in <td> class
This commit is contained in:
parent
38418a4200
commit
172efe2ab9
|
@ -51,8 +51,8 @@ class Grid(object):
|
|||
Core grid class. In sore need of documentation.
|
||||
"""
|
||||
|
||||
def __init__(self, key, data, columns, request=None, mobile=False, model_class=None, enums={},
|
||||
labels={}, renderers={}, extra_row_class=None, linked_columns=[], url='#',
|
||||
def __init__(self, key, data, columns, width='auto', request=None, mobile=False, model_class=None,
|
||||
enums={}, labels={}, renderers={}, extra_row_class=None, linked_columns=[], url='#',
|
||||
joiners={}, filterable=False, filters={},
|
||||
sortable=False, sorters={}, default_sortkey=None, default_sortdir='asc',
|
||||
pageable=False, default_pagesize=20, default_page=1,
|
||||
|
@ -62,6 +62,7 @@ class Grid(object):
|
|||
self.key = key
|
||||
self.data = data
|
||||
self.columns = columns
|
||||
self.width = width
|
||||
self.request = request
|
||||
self.mobile = mobile
|
||||
self.model_class = model_class
|
||||
|
@ -116,17 +117,6 @@ class Grid(object):
|
|||
self.linked_columns.remove(key)
|
||||
|
||||
def set_renderer(self, key, renderer):
|
||||
# TODO: deprecate / remove "type" detection here
|
||||
if renderer == 'boolean':
|
||||
renderer = self.render_boolean
|
||||
elif renderer == 'currency':
|
||||
renderer = self.render_currency
|
||||
elif renderer == 'datetime':
|
||||
renderer = self.render_datetime
|
||||
elif renderer == 'gpc':
|
||||
renderer = self.render_gpc
|
||||
elif renderer == 'quantity':
|
||||
renderer = self.render_quantity
|
||||
self.renderers[key] = renderer
|
||||
|
||||
def set_type(self, key, type_):
|
||||
|
@ -140,6 +130,8 @@ class Grid(object):
|
|||
self.set_renderer(key, self.render_enum)
|
||||
elif type_ == 'gpc':
|
||||
self.set_renderer(key, self.render_gpc)
|
||||
elif type_ == 'percent':
|
||||
self.set_renderer(key, self.render_percent)
|
||||
elif type_ == 'quantity':
|
||||
self.set_renderer(key, self.render_quantity)
|
||||
else:
|
||||
|
@ -194,6 +186,12 @@ class Grid(object):
|
|||
return ""
|
||||
return value.pretty()
|
||||
|
||||
def render_percent(self, obj, column_name):
|
||||
value = self.obtain_value(obj, column_name)
|
||||
if value is None:
|
||||
return ""
|
||||
return "{:0.2f}".format(value)
|
||||
|
||||
def render_quantity(self, obj, column_name):
|
||||
value = self.obtain_value(obj, column_name)
|
||||
return pretty_quantity(value)
|
||||
|
@ -294,6 +292,12 @@ class Grid(object):
|
|||
def render_grid(self, template='/grids3/grid.mako', **kwargs):
|
||||
context = kwargs
|
||||
context['grid'] = self
|
||||
grid_class = ''
|
||||
if self.width == 'full':
|
||||
grid_class = 'full'
|
||||
elif self.width == 'half':
|
||||
grid_class = 'half'
|
||||
context['grid_class'] = '{} {}'.format(grid_class, context.get('grid_class', ''))
|
||||
return render(template, context)
|
||||
|
||||
def get_default_filters(self):
|
||||
|
@ -960,5 +964,5 @@ class CustomWebhelpersGrid(webhelpers2_grid.Grid):
|
|||
if self.linked_columns and column_name in self.linked_columns:
|
||||
url = self.url_generator(record, i)
|
||||
value = tags.link_to(value, url)
|
||||
class_name = 'c{}'.format(column_number)
|
||||
class_name = 'c{} {}'.format(column_number, column_name)
|
||||
return HTML.tag('td', value, class_=class_name)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
## -*- coding: utf-8; -*-
|
||||
<div class="newgrid grid3 full">
|
||||
<div class="newgrid grid3 ${grid_class}">
|
||||
<table>
|
||||
${grid.make_webhelpers_grid()}
|
||||
</table>
|
||||
|
|
|
@ -214,7 +214,7 @@ class MasterView2(MasterView):
|
|||
"""
|
||||
defaults = {
|
||||
'model_class': getattr(self, 'model_class', None),
|
||||
# 'width': 'full',
|
||||
'width': 'full',
|
||||
'filterable': self.filterable,
|
||||
'sortable': self.sortable,
|
||||
'pageable': self.pageable,
|
||||
|
@ -239,7 +239,7 @@ class MasterView2(MasterView):
|
|||
|
||||
defaults = {
|
||||
'model_class': self.model_row_class,
|
||||
# 'width': 'full',
|
||||
'width': 'full',
|
||||
'filterable': self.rows_filterable,
|
||||
'sortable': self.rows_sortable,
|
||||
'pageable': self.rows_pageable,
|
||||
|
|
Loading…
Reference in a new issue