this is the model
2010/11/10 Martin.Mulone <[email protected]>
> I decided to start to make my own implementation of table maker. The
> diference with sqltable or TABLE(), it use div (more flexible), accept
> web2py helpers as values and is very simple to use.
>
> Example:
>
> http://web2pytesting.appspot.com/tablediv/
>
> Code:
>
> table =
>
> SIMPLE_TABLE_DIV(fixedwidth=True,maxwidth=500,name="simplet1",number_list=True)
> table.add_header([{'caption':'Header 1', 'width': 200},
> {'caption':'Header 2', 'width': 100},
> {'caption':'Header 3', 'width':
> 100},
> ])
> table.add_row(['Value 1','Value 2', 'Value 3'])
> table.add_row(['Value 4','Value 5', 'Value 6'])
> table.add_row(['Value 7','Value 8', 'Value 9'])
>
> If anyone is interested in this or want to contribute, i will put in
> bitbucket.
>
--
My blog: http://martin.tecnodoc.com.ar
My portfolio *spanish*: http://www.tecnodoc.com.ar
Checkout my last proyect instant-press: http://www.instant2press.com
# -*- coding: utf-8 -*-
# SIMPLE_TABLE_DIV
#
# Build tables, without using table, this use divs
# Coldef and rowdef is not working now,
#
# EXAMPLE
#def index():
#
# #Sample table
# table = SIMPLE_TABLE_DIV(fixedwidth=True,maxwidth=500,name="simplet1",number_list=True)
# table.add_header([{'caption':'Header 1', 'width': 200},
# {'caption':'Header 2', 'width': 100},
# {'caption':'Header 3', 'width': 100},
# ])
# table.add_row(['Value 1','Value 2', 'Value 3'])
# table.add_row(['Value 4','Value 5', 'Value 6'])
# table.add_row(['Value 7','Value 8', 'Value 9'])
#
#
# #Sample table with HTML HELPERS
# table2 = SIMPLE_TABLE_DIV(fixedwidth=True,maxwidth=530,name="simplet2")
# table2.add_header([{'caption':'Name', 'width': 200},
# {'caption':'Surname', 'width': 200},
# {'caption':'Tel.', 'width': 100},
# ])
# table2.add_row(['Michael', A('Douglas',_href="#douglas"), '22-322223'])
# table2.add_row(['Bill', A('Kid',_href="#kid"), '33-2222-333'],highlight=True)
# table2.add_row(['Von',A('Durssen',_href="#durssen"), '3333-2222'])
# table2.add_row(['Don',A('Jhonson',_href="#durssen"), '3333-2222'])
# table2.add_row(['Brad',A('Pitt',_href="#durssen"), '3333-2222'])
#
#
# #Sample table with HTML HELPERS AND ICONS
# url_edit = URL(request.application,'static','images/edit.png')
# url_apply = URL(request.application,'static','images/apply.png')
# url_disable = URL(request.application,'static','images/disable.png')
#
# table3 = SIMPLE_TABLE_DIV(fixedwidth=True,maxwidth=550,name="simplet3")
# table3.add_header([{'caption':'Name', 'width': 200},
# {'caption':'Email', 'width': 200},
# {'caption':'Edit', 'width': 30},
# {'caption':'Appl.', 'width': 30},
# {'caption':'Disa.', 'width': 30},
# ])
# table3.add_row(['Michael Douglas', A('[email protected]',_href="#"), IMG(_src=url_edit), IMG(_src=url_apply), IMG(_src=url_disable)])
# table3.add_row(['Paul Simon', A('[email protected]',_href="#"), IMG(_src=url_edit), IMG(_src=url_apply), IMG(_src=url_disable)])
# table3.add_row(['James Hetfield', A('[email protected]',_href="#"), IMG(_src=url_edit), IMG(_src=url_apply), IMG(_src=url_disable)])
#
# return dict(table=table, table2=table2, table3=table3)
class SIMPLE_TABLE_COLUMN(object):
def __init__(self, name="", caption="", width=100, highlight=False ):
self.name = name
self.caption = caption
self.width = width
self.highlight = highlight
class SIMPLE_TABLE_ROW(object):
def __init__(self, values=[], header=None, highlight=False ):
self.values = values
self.header = header
self.highlight = highlight
class SIMPLE_TABLE_HEADER(object):
def __init__(self, coldef=[], default_width=100):
if not isinstance(coldef, list):
raise SyntaxError, 'something wrong in headers must be a list type'
if coldef != []:
newcolumns=[]
for col in coldef:
try:
caption = col['caption']
except KeyError:
raise KeyError, "Not a valid column caption, must contain a caption"
try:
name = col['name']
except KeyError:
name = caption #if there no name use caption
try:
width = int(col['width'])
except:
width = default_width #if there no name use caption
try:
highlight = col['highlight']
except KeyError:
highlight = False #if there no name use caption
newcol = SIMPLE_TABLE_COLUMN(name, caption, width, highlight)
newcolumns.append(newcol)
coldef = newcolumns
self.columns = coldef
def insertcolumn(self, name="", caption="", width=100, highlight=False):
newcol = SIMPLE_TABLE_COLUMN(name, caption, width, highlight)
self.columns.append(newcol)
class SIMPLE_TABLE_DIV(object):
DEFAULT_NAME = "simpletable"
def __init__(self,
coldef=[],
rowsdef=[],
truncate=12,
fixedwidth=False,
showheader=True,
maxwidth=600,
row_color=True,
name="",
custom_css="",
number_list=False,
row_height=0):
# coldef=[] Column definition - NOT WORKING NOW
# rowsdef=[] Rows definition - NOT WORKING NOW
# truncate=12 Maximum number of columns
# fixedwidth=False The width of the table adjust to the maxwidth
# showheader=True Display the header
# maxwidth=600 The width of the table
# row_color=True Make alternate background color
# name="" Its important if you want to have more than one table in a view
# custom_css custom css
# number_list display column incremental
# row_height if row heigh is set, >0, this make row height to hide content in overflow
# row_height=25 is recommended to hide
if not isinstance(coldef, list):
raise SyntaxError, 'something wrong in columns definition must be a list type'
if not isinstance(rowsdef, list):
raise SyntaxError, 'something wrong in rows values must be a list type'
self._coldef = coldef[:truncate]
self._rowsdef = rowsdef
self._truncate = truncate
self._fixedwidth = fixedwidth
self._showheader = showheader
self._maxwidth = maxwidth
self._row_color = row_color
self._number_list = number_list
self._default_col_num_width = 40
self._row_height = row_height
if name!="":
self._name = name
else:
self._name = DEFAULT_NAME
if custom_css!="":
self._css = custom_css
else:
self._css = self._default_css() #get default css
self._header = []
self._rows = []
def _draw_row(self, row, highlight=False, num_row=0, selected=False):
def _add_xml_column(value,width):
xml_column='''<div class="column" style="float: left; width: %(width)spx">
%(value)s
</div>'''% {'value': value,
'width': width}
return xml_column
if highlight and not selected:
class_highlight = " highlight"
elif selected:
class_highlight = " selected"
else:
class_highlight = ""
col_idx=0
row_xml=""
xml=""
for col in self._header.columns:
value = row.values[col_idx]
width = col.width
if isinstance(value,DIV): #check if is a helper
value = value.xml()
row_xml+=_add_xml_column(value,width)
col_idx +=1
if self._number_list:
value = num_row + 1
width = self._default_col_num_width
number_column = _add_xml_column(value,width)
else:
number_column = ""
xml+='''<div class="row%(class_highlight)s">
%(number_column)s
%(row_content)s
<div style="clear: both;"></div>
</div>''' % {'number_column': number_column,
'row_content': row_xml,
'class_highlight': class_highlight }
return xml
def _draw_header(self):
def _add_xml_column(value,width):
xml_column='''<div class="column" style="float: left; width: %(width)spx">
%(caption)s
</div>'''% {'caption': value,
'width': width}
return xml_column
xml=""
col_idx = 0
if self._number_list:
caption = "N"
width = self._default_col_num_width
xml+=_add_xml_column(caption, width)
for col in self._header.columns:
caption = col.caption
width = col.width
xml+=_add_xml_column(caption, width)
col_idx +=1
xml+='<div style="clear: both;"></div>'
return xml
def add_header(self, values=[]):
self._header = SIMPLE_TABLE_HEADER(values)
def add_row(self, values=[], highlight=False):
self._rows.append(SIMPLE_TABLE_ROW(values=values, header=self._header, highlight=highlight))
def xml(self):
xml=''
header=''
rows=""
if self._showheader:
header = self._draw_header()
inc_row=0
for row in self._rows:
row_color = False
if self._row_color:
if inc_row%2==0:
row_color = True
rows += self._draw_row(row,row_color,inc_row,row.highlight)
inc_row +=1
xml = ''' %(style)s
<div class="%(name)s">
<div class="header"> %(header)s </div>
<div class="rows"> %(rows)s </div>
</div>''' % {'style': self._css,
'header': header,
'rows': rows,
'name': self._name}
return XML(xml)
def _default_css(self):
if self._fixedwidth:
table_width = "width: %spx;"%self._maxwidth
else:
table_width = ""
if self._row_height>0:
row_height = 'overflow: hidden; height: %spx;' % self._row_height
else:
row_height = ""
css = '''
<style type="text/css" charset="utf-8">
.%(name)s {
margin: 20px 0px 20px 0px;
padding: 5px;
border: 1px solid #eaeaea;
%(table_width)s
}
.%(name)s .header {
background: #eaeaea;
font-weight: bold;
}
.%(name)s .rows {
}
.%(name)s .rows .row {
}
.%(name)s .rows .highlight {
background: #CFDEFF;
}
.%(name)s .rows .selected {
background: #8de38d;
}
.%(name)s .header .column, .%(name)s .rows .column {
padding: 4px;
%(row_height)s
}
.%(name)s .header .column {
border-right: 1px solid #d0d0d0;
}
.%(name)s .rows .column {
}
</style>
''' % {'table_width':table_width,
'name': self._name,
'row_height': row_height}
return css