ToscaWidgets: how to dynamically change "repetitions" of FormFieldRepeater

0 views
Skip to first unread message

Ksenia

unread,
Jan 7, 2007, 11:51:34 AM1/7/07
to TurboGears
Hi,

I was playing with tw_samples and have a question about "repetitions"
parameter of FormFieldRepeater. From js_forms.py:

# We wrap the address fieldset with a FormFieldRepeater to
handle
# repetitions. This can be done with *any* FormField.
address = FormFieldRepeater(
widget = AddressFieldset(),
repetitions = 2,
max_repetitions = 5
)

When the form is displayed, address fieldset is repeated twice. But if
I assign three addresses to a person person.addresses =
[dict(street='street1), dict(street='street1), dict(street='street1')]
only the first two are displayed. I thought the widget will
authomagically adjust the number of address fieldsets to the number of
available addresses, with the maximum of 5. Am I wrong?


Thanks
Ksenia.

Paul Johnston

unread,
Jan 7, 2007, 3:08:44 PM1/7/07
to turbo...@googlegroups.com
Hi Ksenia,

With TurboGears widgets, the behaviour is as you described; I expect the
same is true for ToscaWidgets. I created the following, and you could do
something similar:

class AutoRepeatingFieldSet(RepeatingFieldSet):
def display(self, value=None, **params):
if value and type(value) == list:
params['repetitions'] = len(value)
return super(AutoRepeatingFieldSet, self).display(value, **params)

For now you can create your own class. Really though, I think this
should be part of standard functionality. One idea: if repititions is
set to None, then it is automatically set to the length of the list.

Hope this helps,

Paul

Alberto Valverde

unread,
Jan 7, 2007, 3:18:36 PM1/7/07
to turbo...@googlegroups.com

You'd have to pass a repetitions kw. arg to display to change that
dynamically.

form.display(value, **{'.address':{'repetitions':3}})

However, I'm not very happy with the Repeaters implementation... the
max_repetitions parameter shouldn't even exist and it's true that it
should be a little smarter and adjust itself to the length of available
data...

Currently it creates all repeated fields statically (that's the reason for
max_repetitions)... I'm sure something can be done so this happens
dynamically and repeated widgets are created on-demand (lowering memory
footprint in exchange for slighty higher execution time). I hope to have
something done regarding this before the first alpha in a couple of weeks.

Alberto

Jorge Godoy

unread,
Jan 7, 2007, 3:47:58 PM1/7/07
to turbo...@googlegroups.com
"Alberto Valverde" <alb...@toscat.net> writes:

> However, I'm not very happy with the Repeaters implementation... the
> max_repetitions parameter shouldn't even exist and it's true that it
> should be a little smarter and adjust itself to the length of available
> data...

I've converted the code from someone here to a widget. The application demo
was named 'dynwidget'. There were some things that needed fixing to work with
IE 6/7 and widgets that generated things other than INPUT elements.

> Currently it creates all repeated fields statically (that's the reason for
> max_repetitions)... I'm sure something can be done so this happens
> dynamically and repeated widgets are created on-demand (lowering memory
> footprint in exchange for slighty higher execution time). I hope to have
> something done regarding this before the first alpha in a couple of weeks.

Cool... :-)

--
Jorge Godoy <jgo...@gmail.com>

Ksenia

unread,
Jan 7, 2007, 6:42:58 PM1/7/07
to TurboGears
Hi guys,

Thanks for answering. Worked like a charm.

Ksenia.

Reply all
Reply to author
Forward
0 new messages