[web2py] Re: how to render a widget using a class?

2014-04-26 Thread Massimo Di Pierro
I think you want this:

class CustomWIdget():

   def __init__(self, parameters=None):
pass

   def  html(self):
   return DIV(self.field.name)
   
   def __call__(self, field, value):
   self.field = field
   self.value = value
   return self.html() #this doesnt return html markup, escaped text 
instead

db.define_table(table1,
Field(myField, string, widget=CustomWidget() ) 
)


On Friday, 25 April 2014 22:40:57 UTC-5, aleon...@gmail.com wrote:


 Hi, Im having troubles defining widgets as classes. When widgets are 
 defined as functions it all works well, the function returns a html 
 object(DIV, UL, ...) and the view renders the markup correctly, but im 
 trying to define the widget as a class to handle static files requirements 
 for that widget.

 I have tried the __str__ method to render the widget but the view(or 
 SQLFORM) renders as text, not html markup.

 Here is some example code:

 #Widget as a function
 def CustomWidget(field, value):
 return DIV(field.name, _class=myClass)

 #Widget as a class
 class CustomWIdget():

def __init__(self, field, value):
self.field = field
self.value = value

def  html(self):
return DIV(self.field.name)

def __str__(self):
return str(self.html()) #this doesnt return html markup, escaped 
 text instead

 db.define_table(table1,
 Field(myField, string, widget=CustomWidget) 
 )

 Thanks in advance.



-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
web2py-users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: how to render a widget using a class?

2014-04-26 Thread aleonserra
Thank you very much, it works wonders.

Here is the code i was trying, when the widget is created it also adds 
static file requirements (css, js) dynamically at the html head by using 
response.include_files() there.
 

 db = DAL('mysql://root:1234@localhost/mydb')

 class Widget():
 def add(self, file):
 if not file in response.files:
 response.files.append(file)

 class ColorWidget(Widget):
 #add required files
 def __init__(self, parameters=None):
 self.add(js/jquery.js)
 self.add(js/colorselector.js)
 self.add(css/colorpicker.css)
 
 #return some kind of html markup for the widget
 def __call__(self, field, value):
 self.field = field
 self.value = value
 return DIV(field.name) 


 db.define_table('agencies',
 Field('agency','string', unique=True),
 Field('color','string', widget=ColorWidget())
 )



-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
web2py-users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.