2013-07-25 74 views
2

我想创建一个HTML表来显示一个mongodb集合的内容。该集合包含有关来自小企业的不同客户订单的数据。某些数据将始终存在于给定的文档中,例如客户名称和电话号码。但是,每个文件中的一些数据片段需要变化,例如订购的物品,因为一个客户可能订购1件物品,而另一个可能订购3件物品。因此,如果我在每个文档中都有一个包含任意数字字段的文档的MongoDB集合,我如何动态地将它们添加到HTML表格中以显示文档的内容?作为我正在寻找的显示器类型的示例,这里是我知道的字段的硬编码HTML将保持不变。用HTML显示MongoDB文档

<!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> 

它可能是更好的动态创建整个表,但我不知道在哪里合适的地方做,这是

  • 在HTML文件中的JavaScript函数?
  • 或者也许在pongongo文件中保存mongodb数据库中的信息?

处理将mongodb文档发送到HTML表单的python代码使用python Bottle模板。

@bottle.route('/view/<_id>', method = 'GET') 
def show_invoice(_id): 
    client = pymongo.MongoClient("mongodb://localhost") 
    db = client.orders 
    collection = db.myorders 
    from bson.objectid import ObjectId 
    result = collection.find_one({'_id': ObjectId(_id)}) 
    return bottle.template('invoice', rows = result) 

我非常感谢有人可以提供任何帮助! =)

回答

2

查看瓶子模板引擎的文档,看起来您可以使用'ifs'和'fors'来完成此操作。

举例来说,如果你为了存储在行[“订单”],你不知道有多少,在你的模板,你可以放置:

%for item in rows['orders']: 
    <td>{{item}}</td> 
%end 

或者说你需要如果你的客户订购的是经常在缺货的项目显示一个特殊的警告,你已经在另一个变量传递,“缺货”,指定此:

%if backorder: 
    <span>This item is frequently on backorder</span> 
%end 

我没有测试过其中任意一个,但我使用Django和Flask模板引擎完成了类似的事情。我把这些样品从这里:

http://bottlepy.org/docs/dev/tutorial.html#templates

和这里的瓶模板来格式化输出“部分:

http://bottlepy.org/docs/dev/tutorial_app.html#using-bottle-for-a-web-based-todo-list

希望这有助于!

+0

非常感谢! – sreisman