coolest widget

6 views
Skip to first unread message

Massimo Di Pierro

unread,
Sep 9, 2008, 12:20:22 AM9/9/08
to web...@googlegroups.com

DEFINITION:

def tagging_widget(self,value,tags=[]):
script=SCRIPT("""
function web2py_tag(self,other,tag) {
var o=document.getElementById(other)
if(self.className=='tag_on') {
self.setAttribute('class','tag_off');
o.value=o.value.replace('['+tag+']','');
}
else if(self.className=='tag_off') {
self.setAttribute('class','tag_on');
o.value=o.value+'['+tag+']';
}
}
""")
id=self._tablename+'_'+self.name
def onclick(x): return "web2py_tag(this,'%s','%s');"%(id,x.lower())
buttons=[SPAN(A(x,_class='tag_on' if value and '[%s]'%x.lower() \
in value else 'tag_off',_onclick=onclick(x)),' ') for x
in tags]
return DIV(script,INPUT(_type='hidden',_id=id,_name=self.name,
_value=value),*buttons)

USAGE:

mytags=[,'Django','TurboGears','Pylons','web2py']
db.define_table('proposal',SQLField('tags','text'))
db.proposal.tags.widget=lambda s,v: tagging_widget(s,v,mytags)
form=SQLFORM(db.proposal)

Massimo Di Pierro

unread,
Sep 9, 2008, 12:22:40 AM9/9/08
to web...@googlegroups.com
PS you also need something like this in the CSS

.tag_on {color: green}
.tag_off {color: red}

JorgeRpo

unread,
Sep 11, 2008, 1:47:16 PM9/11/08
to web2py Web Framework
What it is supposed to do?

mdipierro

unread,
Sep 11, 2008, 2:04:52 PM9/11/08
to web2py Web Framework
For example the ajax tags selector at the bottom of
https://mdp.cti.depaul.edu/pycon2009/default/submit_proposal

Steve Shepherd

unread,
Sep 11, 2008, 6:51:36 PM9/11/08
to web...@googlegroups.com
This is a great widget to replace CHECKBOXES.
I will mean you can setup images to use as toggles or simply text.
It is all stored in one text field.
To use is a filter from there you would use the contains filter arguments
After all, putting information into a table is only fun if you can then do some reporting on it later.
A filter example on this text field would be handy Massimo

DiPierro, Massimo

unread,
Sep 11, 2008, 10:05:49 PM9/11/08
to web...@googlegroups.com
Yes, you can use images. Just edit the CSS.
________________________________________
From: web...@googlegroups.com [web...@googlegroups.com] On Behalf Of Steve Shepherd [sarg...@gmail.com]
Sent: Thursday, September 11, 2008 5:51 PM
To: web...@googlegroups.com
Subject: [web2py:9032] Re: coolest widget

André Fischer

unread,
Oct 7, 2008, 4:07:48 AM10/7/08
to web2py Web Framework
This is really great! I have the following question, though: how would
I best use it (in a controller, I assume) so that the list of
available tags were always up-to-date from a table?

For example:
db.define_table('tags',
SQLField('name','string')
)

db.define_table('taggable',
SQLField('somecontent','text'),
SQLField('tags','text)
)

I understand I have to attach the widget with db.taggable.tags.widget
= lambda s,v: tagging_widget(s,v, ***something***)

Can I just say ***something*** is db.(db.tags.id > 0).select('name')
and that would keep it in sync with the contents of the 'tags' table?

Sorry if this is a stupid question... :)

André

André Fischer

unread,
Oct 7, 2008, 6:36:26 AM10/7/08
to web2py Web Framework
I just answered my own question: put the definition of mytags =
select_whatever_I_like somewhere in the model, so that it gets updated
every time. :)

André
Reply all
Reply to author
Forward
0 new messages