开发者

How do I prevent duplicating code with pylons html table updated via ajax?

开发者 https://www.devze.com 2022-12-20 04:22 出处:网络
I have a pylons web-page with a table.I have created python functions in the template which help with the construction of the t开发者_Go百科able html.One of these functions takes an \'item\' and gener

I have a pylons web-page with a table. I have created python functions in the template which help with the construction of the t开发者_Go百科able html. One of these functions takes an 'item' and generates an html row while also adding css zebra striping. The other def generates the header row's html.

This works perfectly for loading the initial table using the context variable 'items'. However, when I try to update the table via ajax, I pull new table contents off the server in JSON format. My 'items' are then Javascript objects in a Javascript array. I can no longer use the pylons 'getHeaderHtml()' and 'getRowHtml(item)'. So the handling of my zebra striping as well as the formatting of the html must be duplicated? There has to be a better way, right?


One possibility would be to actually generate the AJAX HTML server-side (instead of generating JSON), and insert it right into the DOM tree (instead of parsing the JSON and generating the HTML on the client). Then you could use the same functions on the server side to generate the AJAX rows before they are shipped off. An advantage here is that you don't have to worry about parsing anything in the browser, so the JavaScript could become much simpler, and potentially faster.

0

精彩评论

暂无评论...
验证码 换一张
取 消