Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #1479 (closed documentation: fixed)

Opened 7 years ago

Last modified 4 years ago

[Recipe] RepeatingFieldSet with custom legend and field names

Reported by: glezos Owned by: chrisz
Priority: normal Milestone: 1.5
Component: Documentation Version: 1.0.3
Severity: normal Keywords: tosplit
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

comment:1 Changed 6 years ago by Chris Arndt

  • Owner changed from anonymous to Chris Arndt
  • Milestone set to __unclassified__
  • Component changed from TurboGears to Documentation
  • Summary changed from RepeatingFieldSet with custom legend and field names to [Recipe] RepeatingFieldSet with custom legend and field names

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.

comment:2 Changed 5 years ago by jorge.vargas

  • Milestone changed from __unclassified__ to 1.5

comment:3 Changed 5 years ago by jorge.vargas

  • Keywords tosplit added
  • Milestone changed from 1.5 to 1.x

comment:4 Changed 5 years ago by Chris Arndt

  • Type changed from defect to documentation
  • Milestone changed from 1.x to 1.1.x bugfix

comment:5 Changed 5 years ago by Chris Arndt

  • Status changed from new to assigned

comment:6 Changed 5 years ago by Chris Arndt

  • Status changed from assigned to new
  • Owner changed from Chris Arndt to chrisz
  • Milestone changed from 1.1.x bugfix to __unclassified__

Added to  http://docs.turbogears.org/1.0/RoughDocs/WidgetRecipes

If we port TG widgets to Genshi, do we want to include this in the core widgets?

Please assign milestone and change title accordingly or close as fixed.

comment:7 Changed 4 years ago by Chris Arndt

  • Milestone changed from __unclassified__ to 1.5

comment:8 Changed 4 years ago by chrisz

  • Status changed from new to closed
  • Resolution set to fixed

The widget template posted with his ticket does not work. There should be a for loop for the field in fields, just as for the hidden fields.

I have allowed the "legend" parameter to be a list in the Genshi-Widget branch (r6971). The list can even have fewer items than the repetition; the legends will be automatically repeated as necessary. And I kept this backward compatible, i.e. you can still pass a single value for the legend.

The repeated ids are created automatically in build_name_from_path(), I don't think we should mess with this. If you still think individual ids are a good idea, please post working code with either an example projekt or automated tests.

Note: See TracTickets for help on using tickets.