On Mar 16, 2007, at 5:50 PM, Stefan Meretz wrote:
> > Hi, > > my question is: How can attrs of a widget inside a form be changed > during render time? > > Here are some details. > > I want to build an inplace-editing page using AJAX. In the following > (simplified) example I have one text line (a headline), a TextField > and > two buttons. Initially the TextField is not visible (display:none) and > the headline is displayed statically using simple text. An edit > button > is visible, too, the cancel button is not visible. Clicking the edit > button, the static text gets hidden, and the TextField is made visible > vice versa. The edit-button changes to "save" and the cancel button > appears (all done by DOM manipulation via javascript). So far, so > good -- when you have a single form. > > However, I have n>1 (identical) forms. What I need then is to > modify the > onclick-call by passing different parameters to the javascript > function > depending on which form of the n was clicked. So, how can I modify the > onclick-call at render time when the buttons are inside a form? > > Here is the code: > > - main template, page.kid snippet: > <div py:for="t in text"> > <p py:content="t_form(dict(t_input=t.headline), > id=t.number, submit_text=t.headline)"/> > </div> > > - widget/form is this: > class TextblockForm(TableForm): > template = 'myproject.widgets.templates.textblock' > fields = [TextField(name="textblock_input", > attrs=dict(style="display:none")), > ImageButton(name="textblock_button", > default="Edit", src="/static/images/edit.png", > attrs=dict( > onclick="modifyTextblock(1);return false")), > ImageButton(name="textblock_cancel", > default="Cancel", src="/static/images/cancel.png", > attrs=dict( > onclick="modifyTextblock(0);return false"))] > > t_form = TextblockForm() > > - form template textblock.kid: > <form xmlns:py="http://purl.org/kid/ns#" > name="${name}" > method="${method}" > id="${id}" <=id is available for modifying the js-call, but how? > py:attrs="form_attrs"> > <span id="textblock_text">${submit_text}</span> > ${display_field_for("textblock_input")} > ${display_field_for("textblock_button")} > ${display_field_for("textblock_cancel")} > </form> > > - javascript is: > var modifyTextblock = function(modify) { > // pseudo code, basically: > // modify==1 and "edit" was clicked: > // - show TextField / hide static text > // - change "edit" to "save" button > // - show cancel button > // modify==1 and "save" was clicked: > // - show static text / hide textField > // - change "save" to "edit" button > // - hide cancel button > // - save TextField content via JSON > // modify==0 (cancel was clicked): > // - the same as in "save", but without saving > } > > Due to not being able to determine the clicked item in DOM, I need > something like this: > var modifyTextblock = function(item_number,modify) { > // item_number specifies the clicked form > ... > } > > passed by something like this: > onclick="modifyTextblock(1,1) > onclick="modifyTextblock(2,1) > onclick="modifyTextblock(3,1) > .... > > Therefore, I have to modify the associated js-call (attrs-dict of > button) in the main loop of page.kid, but I don't know how. > > I read about using params, but this is on form level, right? I did not > reach the button widget inside the form from the main template loop. > Also RepeatingFormField does not meet what I want to do. I thought > about using Signal, but onclick already is a DOM-signal -- so, why > change? Then I read about update_param and super, but I didn't > understand that completly. > > Finally http://trac.turbogears.org/wiki/StatelessWidgets is quite > clear: "Define the widget's per-request knowledge and behavior at > render time." The example is: > class BananaWidget(Widget): > def __init__(self, foo, **kw): > super(Widget, self).__init__(**kw) > self.foo = foo > > But how can I pass foo to the widget at render time, when it sits > inside > a form? > > Maybe my approach is not really the right way to reach the goal (being > Python/TG beginner), and others are far better. > > Any advice is heavily appreciated! Take a look at the first tip at http://docs.turbogears.org/1.0/ RoughDocs/WidgetTips. Alberto --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "TurboGears" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/turbogears?hl=en -~----------~----~----~----~------~----~------~--~---

