javascript - Displaying MongoDB Documents with HTML -
i trying create html table display contents of mongodb collection. collection contains data different customer orders small business. of data exist in given document, example customer name , phone number. however, of pieces of data within each document need vary, such items ordered since 1 customer may order 1 item, while may order 3 items. so, if have mongodb collection documents contain arbitrary number fields in each document, how can dynamically add them html table display document's contents? example of type of display looking for, here hard-coded html fields know remain constant.
<!doctype html> <html> <head> <head> <title>invoice report</title> <style type="text/css"> body {font-family:sans-serif;color:#4f494f;} form input {border-radius: 7.5px;} h5 {display: inline;} .label {text-align: right} .ordersbook {float:left; padding-top: 10px;} .name {width:100%;float:left; padding:3px;} .wrapper { padding-left: 25px; padding-top: 20px} </style> <script type="text/javascript"> var itemre = /item*/; } </script> </head> </head> <body> <div class="invoice"> <h4>order form:</h4> <table border="1"> <tr> <th>name:</th> <td>{{rows['name']}}</td> </tr> <tr> <th>created:</th> <td>{{rows['created']}}</td> </tr> <tr> <th>phone:</th> <td>{{rows['phone']}}</td> </tr> <tr> <th>email:</th> <td>{{rows['email']}}</td> </tr> <tr> <th>item:</th> <td>{{rows['item']}}</td> </tr> </div> <tr> <th>quantity:</th> <td>{{rows['qty']}}</td> </tr> <tr> <th>color:</th> <td>{{rows['color']}}</td> </tr> <tr> <th>quote:</th> <td>{{rows['quote']}}</td> </tr> </table> </div> </body> </html>
it better create entire table dynamically, not sure proper place is
- in javascript function within html file?
- or maybe in pymongo file maintains info in mongodb database?
the python code handles sending mongodb document html form uses python bottle template.
@bottle.route('/view/<_id>', method = 'get') def show_invoice(_id): client = pymongo.mongoclient("mongodb://localhost") db = client.orders collection = db.myorders bson.objectid import objectid result = collection.find_one({'_id': objectid(_id)}) return bottle.template('invoice', rows = result)
i appreciate may able offer! =)
looking on documentation bottle template engine, looks can use 'ifs' , 'fors' accomplish this.
for instance, if order stored @ rows['orders'] , don't know how many there are, in template can place:
%for item in rows['orders']: <td>{{item}}</td> %end
or need display special warning if customer ordering item on backorder, , you've passed in variable, 'backorder', specifies this:
%if backorder: <span>this item on backorder</span> %end
i haven't tested either of these, i've done similar things using django , flask template engines. pulled these samples here:
http://bottlepy.org/docs/dev/tutorial.html#templates
and 'bottle template format output' section here:
http://bottlepy.org/docs/dev/tutorial_app.html#using-bottle-for-a-web-based-todo-list
hope helps!
Comments
Post a Comment