TurboGears – 資料網格



ToscaWidgets 包含一個 DataGrid 控制元件,它提供了一種快速以表格形式呈現資料的方法。DataGrid 物件宣告如下 −

from tw2.forms import DataGrid
student_grid = DataGrid(fields = [('Name', 'name'),('City', 'city'),
   ('Address','address'), ('PINCODE', 'pincode')])

現在,showgrid() 函式檢索學生表中的所有記錄並將資料公開到 grid.html 模板。首先給出 showgrid() 函式的程式碼,然後給出 grid.html 程式碼 −

showgrid()

@expose('hello.templates.grid')
def showgrid(self):
   data = DBSession.query(student).all()
   return dict(page = 'grid', grid = student_grid, data = data)

grid.html

<!DOCTYPE html>
<html xmlns = "http://www.w3.org/1999/xhtml"
   xmlns:py = "http://genshi.edgewall.org/"
   lang = "en">
   
   <head>
      <title>Student Registration Form</title>
   </head>
   
   <body>
      <div id = "getting_started">
         <div>${grid.display(value = data)}</div>
      </div>
   </body>

</html>

當在瀏覽器中輸入 https://:8080/showlist URL 時,將顯示以下表格資料 −

Form
廣告
© . All rights reserved.