If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

Include the necessary files by adding this to your model or controller:

response.files.append("http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js")
response.files.append("http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/ui-darkness/jquery-ui.css")

Copy the widget code to your model:

def date_widget(f,v):
    wrapper = DIV()
    inp = SQLFORM.widgets.string.widget(f,v,_class="jqdate")
    jqscr = SCRIPT("jQuery(document).ready(function(){jQuery('#%s').datepicker({dateFormat:'yy-mm-dd'});});" % inp['_id'],_type="text/javascript")
    wrapper.components.extend([inp,jqscr])
    return wrapper

Apply the widget to a field:

db.define_table("test",Field("posted","date",widget=date_widget))

Try it out in your controller:

def index():
    form = SQLFORM(db.test)    
    if form.accepts(request.vars,session):
        response.flash = "Got it"
    all = db().select(db.test.ALL)
    return dict(form=form,all=all)

datepicker


Demo application (right click/Save As): Download

Related slices

Comments (3)

  • Login to post



  • 0
    apardue 10 years ago
    Hi - thanks for this. It works great in Firefox, however there seems to be a problem in IE8. The rest of the page after the field using the date picker plugin is completely blank. I downloaded the demo app on this page to confirm it wasn't my implementation of it that had bungled something. I checked the error details for the IE page which stated: "HTML Parsing Error: Unable to modify the parent container element before the child element is closed (KB927917)" Does anybody have any ideas on a possible fix for this? I have been forced to drop the plugin in the mean time. Any help appreciated.

  • 0
    mrfreeze 10 years ago
    @vanbasten - I made it work by wrapping the script in a jQuery(document).ready function. Can you try again?

  • 0
    apardue 10 years ago
    Brilliant! Thank you Mr Freeze, problem solved.

Hosting graciously provided by:
Python Anywhere