Thank you Anthony that works perfectly!
On Wednesday, December 4, 2013 4:44:13 PM UTC+1, Anthony wrote:
>
> You have created a custom widget, but the widget code doesn't set the
> default value of the widget based on the "value" argument to the widget.
> Try changing:
>
> seed = SELECT(**attr)
> for branch in tree:
> seed.append(branch)
>
> to:
>
> seed = SELECT(*tree, value=value, **attr)
>
> Anthony
>
> On Wednesday, December 4, 2013 9:29:22 AM UTC-5, Ivo wrote:
>>
>> default.py:
>> def products():
>> edit_in_form = 'edit' in request.args
>> db.product.price.writable = not edit_in_form
>>
>> grid = SQLFORM.grid(db.product,
>> columns=[
>> db.product.name,
>> db.product.price,
>> ],
>> )
>>
>> return dict(
>> buttons=grid.gridbuttons,
>> grid=grid)
>>
>> db.py:
>> db.define_table('product',
>>
>> Field('name',length=512),
>> Field('user_id', db.auth_user),
>> Field('category'), #db.category
>> Field('price'),
>> )
>> db.product.category.widget = category.select_widget()
>>
>> here's the widget itself:
>>
>> def select_widget(self):
>> categories = self._db().select(
>> self._db[self._tablename].ALL,
>> orderby=self._db[self._tablename].lft
>> )
>> def widget(field, value, **attributes):
>> rgt = []
>>
>> tree = []
>>
>> attr = OptionsWidget._attributes(field, {}, **attributes)
>>
>> values = re.compile('[\w\-:]+').findall(str(value))
>>
>> if hasattr(field.requires, 'options'):
>> opts = []
>> options = field.requires.options()
>> tree.append(OPTION(_value="", _class="branch"))
>> for cat in categories:
>> if len(rgt) > 0:
>> if rgt[-1] > cat.rgt:
>> # open UL
>> pass
>> while rgt[-1] < cat.rgt:
>> rgt.pop()
>> if len(rgt) == 0:
>> break
>>
>> s = ""
>> for i in range(len(rgt)):
>> s += " "
>> s += cat.name
>> branch = OPTION(
>> XML(s),
>> _value=cat.id,
>> _class="branch",
>> )
>>
>> tree.append(branch)
>> rgt.append(cat.rgt)
>>
>> seed = SELECT(**attr)
>>
>> for branch in tree:
>> seed.append(branch)
>>
>> return seed
>> return widget
>>
>>
>>
>>
>> On Wednesday, December 4, 2013 2:56:24 PM UTC+1, Anthony wrote:
>>>
>>> Doesn't sound right. Can you show your code?
>>>
>>> On Wednesday, December 4, 2013 5:11:25 AM UTC-5, Ivo wrote:
>>>>
>>>> Is there a way to default the option widget to the current field value?
>>>> The thing is that when you edit a form, the option widget always
>>>> defaults to empty (you need to make a selection even if the field already
>>>> contains a value). I'm quite sure a lot of users will not notice that the
>>>> select is empty, overriding the existing value upon save. They should
>>>> however need to be able to change the value if they choose to. I prefer to
>>>> do it from the controller side if possible.
>>>>
>>>>
--
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/groups/opt_out.