Version 2 of the patch I discovered that I messed up self.custom.dspval contents with previous patch. And having both functions (do_custom & do_standard) is more convenient - this way used can decide where he wants standard fields to go - before his ones of after them.
On Tue, May 19, 2009 at 11:44 AM, Alexey Nezhdanov <snak...@gmail.com> wrote: > Here is it. Small, nice, tested and documented. > > One downside though. I tried hard but failed to understand the original > self.trows purpose. While I preserved default functionality, I'm not > sure if I missed something subtle. > > And, btw, use of trows seemed buggy for me. > Here is my best guess: > trows is intended to remove validation errors from widgets that were > manually assigned to the database fields. If so - then it fails it's > purpose because > self.trows[fieldname][1][0].components > addresses components of the <INPUT/> and not of <TD/>. To fix it you > need to remove [0]: > self.trows[fieldname][1].components > > On Tue, May 19, 2009 at 7:52 AM, Alexey Nezhdanov <snak...@gmail.com> wrote: >> Hello. >> >> Reading the SQLFORM sources doesn't give me any insight on >> how to do custom form layout w/o modifying web2py source >> AND writing extensive wrappers around it. >> >> I found these pages while googling: >> http://groups.google.com/group/web2py/browse_thread/thread/39c0bd5e01860eb/fecc0f6edbb3885f?lnk=gst&q=custom+sqlform+layout#fecc0f6edbb3885f >> http://www.wellbehavedsystems.co.uk/web2py/examples/custom_forms.html >> >> As I can see - it was an unofficial solution, proposed about a >> half-year ago. Is it a recommended approach atm? If not - is >> there one? If yes - is it going to be included? >> >> I am personally didn't like it too much - it's what I referred to >> as 'extensive wrapper'. >> >> If there is no support in current SQLFORM yet I'll probably end >> up writing something myself, hopefully something simpler. >> >> Currently I think of something like this (in SQLFORM.__init__): >> 1) iterate over fields, generate widgets (it does that already), >> 2) put generated widgets into a Storage(input_name:input_widget) >> 3) >> self.components=[] >> self.do_custom_layout() # by default does nothing, can be redefined in >> derived clases >> self.do_standard_layout() # populates table with fields remaining in the >> Storage fields >> >> do_custom_layout (when redefined) will pop widgets out of >> said Storage and put them into some HTML, adding it to self.components >> >> -- >> Sincerely yours >> Alexey Nezhdanov >> > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "web2py Web Framework" group. To post to this group, send email to web2py@googlegroups.com To unsubscribe from this group, send email to web2py+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/web2py?hl=en -~----------~----~----~----~------~----~------~--~---
--- /tmp/sqlhtml.py 2009-05-03 15:47:51.000000000 +0400 +++ sqlhtml.py 2009-05-19 12:31:26.281600242 +0400 @@ -389,11 +389,12 @@ continue else: inp = self.widgets.string.widget(field, default) - tr = self.trows[fieldname] = TR(label, inp, comment, - _id=row_id) - xfields.append(tr) + self.trows[fieldname] = dict(label=label, widget=inp, + comment=comment, _id=row_id) self.custom.dspval[fieldname]=dspval or nbsp self.custom.inpval[fieldname]=inpval or '' + self.do_custom_layout() + self.do_standard_layout() if record and linkto: for (rtable, rfield) in table._referenced_by: query = urllib.quote(str(table._db[rtable][rfield] @@ -425,7 +426,26 @@ if not self['hidden']: self['hidden'] = {} self['hidden']['id'] = record['id'] - self.components = [TABLE(*xfields)] + self.components += [TABLE(*xfields)] + + def do_standard_layout(self): + xfields=[] + for fieldname in self.fields: + try: trow=self.trows[fieldname] + except KeyError: continue + tr=TR(trow['label'], trow['widget'], trow['comment'], + _id=trow['_id']) + xfields.append(tr) + self.components.append(TABLE(*xfields)) + + def do_custom_layout(self): + """ Must pop custom items out of self.trows dictionary + and populate self.components list with html elements. + For instance it may do: + name=self.trows.pop('name') + tr=TR(name['label'],name['widget'],name['comment'],_id=name['_id']) + self.components.append(TABLE(tr)) + """ def accepts( self, @@ -485,8 +505,9 @@ for fieldname in self.fields: field = self.table[fieldname] if hasattr(field, 'widget') and field.widget\ + and self.trows.has_key(fieldname)\ and request_vars.has_key(fieldname): - self.trows[fieldname][1][0].components = \ + self.trows[fieldname]['widget'].components = \ [field.widget(field, request_vars[fieldname])] return ret