TurboGears – DataGrid
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>
在浏览器中输入 http://localhost:8080/showlist URL 时将显示以下表格数据 −

