Ticket #1479 (new defect)

Opened 1 year ago

Last modified 3 hours ago

[Recipe] RepeatingFieldSet with custom legend and field names

Reported by: glezos Assigned to: Chris Arndt
Priority: normal Milestone: __unclassified__
Component: Documentation Version: 1.0.3
Severity: normal Keywords:
Cc:

Description

The current implementation of RepeatingFieldSet? creates a set of fieldsets with common attributes; for example, legends are the same for each fieldset. In addition, the variables in each fieldset are automatically numbered -- one might want to give them custom names.

Here is an implementation of the widget that accepts lists of the size of repetitions for legends and fields.

class RepeatingFieldSetPlus(RepeatingFormField):
    """
    A widget similar to RepeatingFieldSet, but with differentiation
    between each field set. Accepts ids and legends as lists.
    """
    
    template="""
    <div xmlns:py="http://purl.org/kid/ns#">
    <fieldset py:for="repetition, id in enumerate(ids)"
        class="${field_class}"
        id="${field_id}_${id}"
    >
        <legend py:if="legends[repetition]"
                py:content="legends[repetition]" />
        <div py:for="field in hidden_fields" 
             py:replace="field.display(value_for(field[repetition]),
                        **params_for(field[repetition]))" 
        />
        <label class="fieldlabel" for="${field.field_id}"
               py:content="field.label" />
        <span py:replace="field.display(value_for(field),
                                        **params_for(field))" />
        <span py:if="error_for(field)" class="fielderror"
              py:content="error_for(field)" />
        <span py:if="field.help_text" class="fieldhelp"
              py:content="field.help_text" />
    </fieldset>
    </div>
    """
    params = ["legends", "ids", "table_attrs"]
    params_doc = {'legend' : _('Text to display as the legend '
                             'for the fieldset')}
    legends = None
    ids = None
    table_attrs = {}

Can be called like this:

RepeatingFieldSetPlus(
    ids = [f.id for f in foo.items]
    legends = [f.leg for f in foo.items]
    fields=[SingleSelectField("foo_%s" % f.id) for f in foo.items]

I hope I'm even somewhat close to something useful/unimplemented here..

Change History

08/28/08 15:29:22 changed by Chris Arndt

  • owner changed from anonymous to Chris Arndt.
  • summary changed from RepeatingFieldSet with custom legend and field names to [Recipe] RepeatingFieldSet with custom legend and field names.
  • component changed from TurboGears to Documentation.
  • milestone set to __unclassified__.

Sorry for the late answer.

This seems like an interesting widget but we probably want introduce any new features into the TG widgets package anymore, since it will be replaced by ToscaWidgets in TG 1.5.

But you are welcome to add this code as a recipe to the documentation wiki. I suggest to set up a page named http://docs.turbogears.org/1.0/RouhDocs/WidgetRecipes.

If I have time I will add the code ther myself, but please don't let that deter you from taking care of this yourself.