Add the widget to your model:
def select_datewidget(f,v):
import datetime
now = datetime.date.today()
dtval = v if v else now.isoformat()
y,m,d= str(dtval).split("-")
dt = SQLFORM.widgets.string.widget(f,v)
dayid = dt['_id']+'_day'
monthid = dt['_id']+'_month'
yearid = dt['_id']+'_year'
wrapper = DIV(_id=dt['_id']+"_wrapper")
day = SELECT([OPTION(str(i).zfill(2)) for i in range(1,32)],
value=d,_id=dayid)
month = SELECT([OPTION(datetime.date(2008,i,1).strftime('%B')
,_value=str(i).zfill(2)) for i in range(1,13)],
value=m,_id=monthid)
year = SELECT([OPTION(i) for i in range(now.year-50,now.year+50)],
value=y,_id=yearid)
setval = "var curval = jQuery('#%s').val();if(curval){var pcs = curval.split('-');"\
"var dd = pcs[2];var mm = pcs[1];var yy = pcs[0];"\
"jQuery('#%s').val(dd);jQuery('#%s').val(mm);jQuery('#%s').val(yy);}" % \
(dt['_id'],dayid,monthid,yearid)
combined = "jQuery('#%s').val()+'-'+jQuery('#%s').val()+'-'+jQuery('#%s').val()" % \
(yearid,monthid,dayid)
combine = "jQuery('#%s').val(%s);" % (dt['_id'],combined)
onchange = "jQuery('#%s select').change(function(e){%s});" % \
(wrapper['_id'],combine)
jqscr = SCRIPT("jQuery('#%s').hide();%s%s" % (dt['_id'],setval,onchange))
wrapper.components.extend([month,day,year,dt,jqscr])
return wrapper
Create a test form in your controller and set the field to use the widget:
def index():
form = SQLFORM.factory(Field('posted','date',default=request.now,widget=select_datewidget))
if form.accepts(request.vars,session):
response.flash = "New record added"
return dict(form=form)
That all folks....
Comments (8)
0
reyelts 14 years ago
0
mrfreeze 14 years ago
0
reyelts 14 years ago
0
mrfreeze 14 years ago
0
johannspies 15 years ago
0
mrfreeze 15 years ago
0
sophie 15 years ago
0
mrfreeze 15 years ago