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:
Lance Edgar 2017-07-14 18:21:24 -05:00
parent 38418a4200
commit 172efe2ab9
3 changed files with 21 additions and 17 deletions

View file

@ -51,8 +51,8 @@ class Grid(object):
Core grid class. In sore need of documentation. Core grid class. In sore need of documentation.
""" """
def __init__(self, key, data, columns, request=None, mobile=False, model_class=None, enums={}, def __init__(self, key, data, columns, width='auto', request=None, mobile=False, model_class=None,
labels={}, renderers={}, extra_row_class=None, linked_columns=[], url='#', enums={}, labels={}, renderers={}, extra_row_class=None, linked_columns=[], url='#',
joiners={}, filterable=False, filters={}, joiners={}, filterable=False, filters={},
sortable=False, sorters={}, default_sortkey=None, default_sortdir='asc', sortable=False, sorters={}, default_sortkey=None, default_sortdir='asc',
pageable=False, default_pagesize=20, default_page=1, pageable=False, default_pagesize=20, default_page=1,
@ -62,6 +62,7 @@ class Grid(object):
self.key = key self.key = key
self.data = data self.data = data
self.columns = columns self.columns = columns
self.width = width
self.request = request self.request = request
self.mobile = mobile self.mobile = mobile
self.model_class = model_class self.model_class = model_class
@ -116,17 +117,6 @@ class Grid(object):
self.linked_columns.remove(key) self.linked_columns.remove(key)
def set_renderer(self, key, renderer): 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 self.renderers[key] = renderer
def set_type(self, key, type_): def set_type(self, key, type_):
@ -140,6 +130,8 @@ class Grid(object):
self.set_renderer(key, self.render_enum) self.set_renderer(key, self.render_enum)
elif type_ == 'gpc': elif type_ == 'gpc':
self.set_renderer(key, self.render_gpc) self.set_renderer(key, self.render_gpc)
elif type_ == 'percent':
self.set_renderer(key, self.render_percent)
elif type_ == 'quantity': elif type_ == 'quantity':
self.set_renderer(key, self.render_quantity) self.set_renderer(key, self.render_quantity)
else: else:
@ -194,6 +186,12 @@ class Grid(object):
return "" return ""
return value.pretty() 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): def render_quantity(self, obj, column_name):
value = self.obtain_value(obj, column_name) value = self.obtain_value(obj, column_name)
return pretty_quantity(value) return pretty_quantity(value)
@ -294,6 +292,12 @@ class Grid(object):
def render_grid(self, template='/grids3/grid.mako', **kwargs): def render_grid(self, template='/grids3/grid.mako', **kwargs):
context = kwargs context = kwargs
context['grid'] = self 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) return render(template, context)
def get_default_filters(self): 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: if self.linked_columns and column_name in self.linked_columns:
url = self.url_generator(record, i) url = self.url_generator(record, i)
value = tags.link_to(value, url) 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) return HTML.tag('td', value, class_=class_name)

View file

@ -1,5 +1,5 @@
## -*- coding: utf-8; -*- ## -*- coding: utf-8; -*-
<div class="newgrid grid3 full"> <div class="newgrid grid3 ${grid_class}">
<table> <table>
${grid.make_webhelpers_grid()} ${grid.make_webhelpers_grid()}
</table> </table>

View file

@ -214,7 +214,7 @@ class MasterView2(MasterView):
""" """
defaults = { defaults = {
'model_class': getattr(self, 'model_class', None), 'model_class': getattr(self, 'model_class', None),
# 'width': 'full', 'width': 'full',
'filterable': self.filterable, 'filterable': self.filterable,
'sortable': self.sortable, 'sortable': self.sortable,
'pageable': self.pageable, 'pageable': self.pageable,
@ -239,7 +239,7 @@ class MasterView2(MasterView):
defaults = { defaults = {
'model_class': self.model_row_class, 'model_class': self.model_row_class,
# 'width': 'full', 'width': 'full',
'filterable': self.rows_filterable, 'filterable': self.rows_filterable,
'sortable': self.rows_sortable, 'sortable': self.rows_sortable,
'pageable': self.rows_pageable, 'pageable': self.rows_pageable,