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)
.tag_on {color: green}
.tag_off {color: red}