Sure. Note, you don't need to replace the whole widget -- just update the
attribute directly:
form.element('input', _id='%s_%s' % (table_name, field.name)).update(
data={'show-trigger': '%s_%s' % (table_name, field.show_if.first.name)})
Anthony
On Saturday, July 12, 2014 4:38:58 PM UTC-4, Louis Amon wrote:
>
> I think a framework like web2py shouldn't break by default, and work only
> if you know which parameter to set.
>
>
> Anyway here's a better example:
>
> db.define_table('person', Field('name'))
> db.define_table('other', Field('show_trigger', 'boolean'))
>
> db.person.name.show_if = (db.other.show_trigger == True)
>
> form=SQLFORM.factory(db.person, db.other)
>
>
> If I do as you suggest and add table_name='person' during form
> definition, web2py.js won't be able to match the data-show-trigger :
> other_show_trigger
>
>
> The code I suggested fixes this though.
>
> On Saturday, July 12, 2014 9:12:22 PM UTC+2, Anthony wrote:
>>
>> Instead, you can just do this:
>>
>> form = SQLFORM.factory(db.person, table_name='person')
>>
>> Anthony
>>
>> On Saturday, July 12, 2014 2:02:45 PM UTC-4, Louis Amon wrote:
>>>
>>> Not sure if somebody noticed before, but the show_if syntax (backed by
>>> web2py.js) is actually breaking if you use SQLFORM.factory to generate your
>>> form.
>>>
>>>
>>> Example:
>>>
>>> db.define_table('person', Field('name', 'string'), Field('show_trigger',
>>>> 'boolean'))
>>>> db.person.name.show_if = (db.person.show_trigger == True)
>>>
>>>
>>>
>>> When show_if is processed, special attributes are added to inputs.
>>> data-show-trigger is among them, and is used in the javascript code to
>>> determine the id of the element that triggers display.
>>>
>>> In our previous example, the data-show-trigger generated by our model
>>> is person_show, and web2py.js will try to match this id.
>>>
>>> *In a factory form, the tablename of a field is either specified or
>>> hard-coded ('no_table').*
>>>
>>> Example:
>>>
>>>> form = SQLFORM.factory(db.person)
>>>
>>>
>>> The generated input for the field "person.name" willl have the
>>> following id : no_table_name.
>>>
>>> *The mismatch between SQLFORM.factory's input and and data-show-trigger
>>> produces a javascript error, which of course blocks any further javascript
>>> code from being executed.*
>>>
>>>
>>>
>>> *Here is the modification I suggest in order to fix this :*
>>>
>>> In *gluon.sqlhtml.py <http://gluon.sqlhtml.py>*, replace the last line :
>>>
>>> return SQLFORM(DAL(None).define_table(table_name, *fields), **attributes
>>> )
>>>
>>> With the following code:
>>>
>>> # Generate SQLFORM
>>> form = SQLFORM(DAL(None).define_table(table_name, *fields),
>>> **attributes)
>>>
>>>
>>> # Replace data-show-trigger (cf. 'show_if') since we changed the
>>> table name
>>> def replace_triggers(fields):
>>> for field in fields:
>>> if isinstance(field, Field) and getattr(field, 'show_if',
>>> None):
>>> form.element('input', _id='%s_%s' % (table_name,
>>> field.name),
>>> replace=lambda t: t.update(**{
>>> '_data-show-trigger':'%s_%s' % (table_name, field.show_if.first.name)}))
>>> elif isinstance(field, Table):
>>> table = field
>>> # recursive call
>>> replace_trigger([field for field in table])
>>> replace_triggers(fields)
>>>
>>>
>>> return form
>>>
>>>
>>> This way, the hard-coded tablename in SQLFORM.factory is also applied to
>>> any show_if in the form.
>>>
>>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.