[web2py] Re: TABLE and TBODY helpers add TR innapropriately
On Friday, April 7, 2017 at 9:48:37 AM UTC-4, Brian M wrote: > > Anthony, this is what I was looking for. Thanks! I am trying not to build > the html myself and use the helpers instead. I am going for readability and > ease of use for others to modify later. In all the examples in the book > nowhere did it show the append and insert for the HTML helpers I looked at. > > The gist of it was that I needed to was to not convert everything to > strings, but make them the various helper objects so when passed to the > TBODY and TABLE they don't add > > > tbody=TBODY() > headerrow=TR() > > > ### > #big fancy query loop and processing to accomplish the following# > headerrow.append('column 1') > tbody.append(TR(TD('db data processed and gussied up'))) > ### > > > thead=THEAD(headerrow) > table=TABLE(_class='pure-table pure-table-bordered') > table.append(thead) > table.append(tbody) > htmltable=XML(table) > > Note, some of this is covered here: http://web2py.com/books/default/chapter/29/05/the-views#HTML-helpers You can do as you have above and directly .append() to a helper object such as a TBODY. But you can also simply build a Python list of helpers and then pass the entire list to the parent. For example: table_rows = [TR(TD(record.name)) for record in records] table_body = TBODY(*table_rows) The above uses *table_rows argument unpacking, but you can in fact simply pass a list to TBODY rather than using the unpacking syntax: table_body = TBODY(table_rows) Now let's say you have some complicated code to produce a row of the table. You can abstract that into a separate function and then build the whole table body in a simple one-liner: def build_complicated_row(record): cells = [] cells.append(TD(some_function(record))) ... # more code to add TDs return TR(cells, _class='some_class') table_body = TBODY([build_complicated_row(record) for record in records]) Anthony -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[web2py] Re: TABLE and TBODY helpers add TR innapropriately
Leonel thanks for your help. I did need to put everything in the XML the way I was doing it because otherwise it all gets escaped. But that is because I was basically converting everything to a string first which is not what I wanted so you were right and I was confused. As far as why not put it in the view, there is no way to re-use those code snipets. I am trying to build code that makes building a table that has control over my little linux based device. The table is the way it makes available to the user to configure and control things. So for example, I will need links in most tables and I need to do it all in controller to have functions defined so that I can use the bit of code that makes the links work from any web page that has a table of stuff to control. Again thanks for your help. -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[web2py] Re: TABLE and TBODY helpers add TR innapropriately
Anthony, this is what I was looking for. Thanks! I am trying not to build the html myself and use the helpers instead. I am going for readability and ease of use for others to modify later. In all the examples in the book nowhere did it show the append and insert for the HTML helpers I looked at. The gist of it was that I needed to was to not convert everything to strings, but make them the various helper objects so when passed to the TBODY and TABLE they don't add tbody=TBODY() headerrow=TR() ### #big fancy query loop and processing to accomplish the following# headerrow.append('column 1') tbody.append(TR(TD('db data processed and gussied up'))) ### thead=THEAD(headerrow) table=TABLE(_class='pure-table pure-table-bordered') table.append(thead) table.append(tbody) htmltable=XML(table) -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[web2py] Re: TABLE and TBODY helpers add TR innapropriately
> > htmltablebody=TBODY(TAG(XML(htmlrows))) > Probably you meant TAG(htmlrows) (i.e., without the XML()), but even that I do not think will work (TAG() will not return a list of TR objects but a parent TAG object). However, this should work: htmltablebody = TBODY(CAT(XML(htmlrows))) But as noted in my other response, there are better options. Anthony -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[web2py] Re: TABLE and TBODY helpers add TR innapropriately
> > Here is my python in the controller that builds the table HTML: > > #these three lines are static, but in my code the looping that builds them > is pretty complex eventually with form elements, javascript, and links > htmlrows="row 1 descrow 1 > linka" > htmlrows+="row 2 descrow 2 > linkb" > htmlrows+="row 3 descrow 2 > linkc" > Why can't you instead do something like this: tablerows = [] for i in something: tablerows.append(TR(...)) tbody = TBODY(tablerows) > htmltablehead=XML(THEAD(TR(TH('Column 1'),TH('links'),TH('column 3' > htmltablebody=XML(TBODY(XML(htmlrows))) > htmltable=XML(TABLE(XML(htmltablehead),XML(htmltablebody),_class="pure-table > pure-table-bordered")) > Note, when you have actual HTML helper objects (e.g., your THEAD object), there is no need to wrap them in XML() before passing them to other helper objects. Also, if for some reason you feel you really need to build the rows as raw HTML markup, then there isn't really much point in using the TBODY and TABLE helpers anyway -- they're each just a single HTML tag, which you might as well just write in raw HTML yourself. Anthony -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[web2py] Re: TABLE and TBODY helpers add TR innapropriately
Yeah sometimes it's not a good idea to mix XML made stuff with the other helpers, namely the TABLE helper tries to be convenient and put stuff that you give it to him inside TDs if they aren't yet. If you really have to use XML and TABLE use TAG to turn it into web2py helpers: htmlrows="row 1 descrow 1 linka" htmlrows+="row 2 descrow 2 linkb" htmlrows+="row 3 descrow 2 linkc" htmltablehead=THEAD(TR(TH('Column 1'),TH('links'),TH('column 3'))) htmltablebody=TBODY(TAG(XML(htmlrows))) htmltable=TABLE(htmltablehead, htmltablebody,_class="pure-table pure-table-bordered") {{=htmltable}} Also, notice that you don't need to be putting everything inside XML. Don't forget you can always just generate this whole thing in a string and simply call XML in the end. Finally, wouldn't all this HTML building be better off in a view and simply not using helpers? -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.