TurboGears - DataGrid
ToscaWidgets contiene un controllo DataGrid che fornisce un modo rapido per presentare i dati in forma tabulare. L'oggetto DataGrid viene dichiarato come segue:
from tw2.forms import DataGrid
student_grid = DataGrid(fields = [('Name', 'name'),('City', 'city'),
('Address','address'), ('PINCODE', 'pincode')])
Ora, la funzione showgrid () recupera tutti i record nella tabella degli studenti ed espone i dati al modello grid.html. Di seguito viene fornito prima il codice per la funzione showgrid () e quindi il codice 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>
I seguenti dati tabulari verranno visualizzati quando http://localhost:8080/showlist L'URL è inserito nel browser -
