| | 70 | == Multiple Widgets == |
| | 71 | |
| | 72 | It is possible to specify multiple possible widget alternatives in the S3ReusableField, and then apply them by name. |
| | 73 | |
| | 74 | Example: |
| | 75 | |
| | 76 | {{{#!python |
| | 77 | organisation_id = S3ReusableField("organisation_id", "reference org_organisation", |
| | 78 | requires = IS_EMPTY_OR(IS_ONE_OF(db, "org_organisation.id", |
| | 79 | org_organisation_represent)), |
| | 80 | represent = org_organisation_represent, |
| | 81 | label = T("Organisation"), |
| | 82 | widgets = {"default": S3OrganisationAutocompleteWidget(default_from_profile=True), |
| | 83 | "hierarchical": S3HierarchyWidget(lookup = "org_organisation", |
| | 84 | represent = org_organisation_represent, |
| | 85 | multiple = False, |
| | 86 | leafonly = False, |
| | 87 | ), |
| | 88 | }, |
| | 89 | ) |
| | 90 | }}} |
| | 91 | |
| | 92 | Without override or explicit choice, the "default" widget would be used in Field instances. |
| | 93 | |
| | 94 | To choose a widget by name: |
| | 95 | |
| | 96 | {{{ |
| | 97 | tablename = "my_table" |
| | 98 | table = db.define_table(tablename, |
| | 99 | ..., |
| | 100 | # Choose the "hierarchical" widget for this instance: |
| | 101 | organisation_id(widget="hierarchical"), |
| | 102 | ... |
| | 103 | ) |
| | 104 | }}} |
| | 105 | |
| | 106 | This is especially useful if the choice of the widget depends on a deployment setting. |
| | 107 | |
| | 108 | However, every instance can still override all possible alternatives: |
| | 109 | |
| | 110 | {{{ |
| | 111 | tablename = "my_table" |
| | 112 | table = db.define_table(tablename, |
| | 113 | ..., |
| | 114 | # Specify the widget explicitly: |
| | 115 | organisation_id(widget=MyCustomWidget()), |
| | 116 | ... |
| | 117 | ) |
| | 118 | }}} |
| | 119 | |
| | 120 | ...or enforce the web2py standard widget for the field type: |
| | 121 | |
| | 122 | {{{ |
| | 123 | tablename = "my_table" |
| | 124 | table = db.define_table(tablename, |
| | 125 | ..., |
| | 126 | # Enforce web2py standard widget for the field type: |
| | 127 | organisation_id(widget=None), |
| | 128 | ... |
| | 129 | ) |
| | 130 | }}} |
| | 131 | |