Front desk:
<table id= "DG" ></table>
$ (' #dg '). DataGrid ({
URL: '/url/',
columns:[[
{field: ' Name ', title: ' Name ', width:100},
{field: ' User ', title: ' User ', width:100}
]]
})
Backstage: There are 3 main parameters, page (pages), rows (rows per page), total (row number)
Ideas:
1. The DataGrid will pass its own page with rows, directly in the background to get
2, get page and rows after the type to int, take the data from the database to assign total number of rows, and then put the data in the list
3, Judge Page is not the first page:
3.1, if it is the first page: Determine how many rows (rows), Fetch rows row data
3.2, if not the first page: Delete page-1 data from the list to get the remaining data, and then do step 3.1
4. Combine total with the data obtained: Ealist = {' Rows ': alist, ' Total ': all}
5, turn ealist into JSON format data return, OK
def userlist (Request):
page = Request. get[' page ']
rows = Request. get[' rows ']
User_list = Userdb.objects.all (). order_by ('-id ')
Alist=[]
For I in User_list.values ():
Alist.append (i)
TL = Len (alist)
Easy_list = []
rs = rows
A = Int (page)-1
b = Int (RS)
Total = Json.dumps (TL)
if (a==0):
A = 1
if (B>len (alist)):
Ealist = {' Rows ': alist, ' Total ': all}
Else
for s in range (A*B):
Easy_list.append (Alist[s])
Ealist = {' Rows ': easy_list, ' Total ': all}
Else
for s in range (A*B):
Print Len (alist)
Del Alist[0]
Print Alist
if (Len (alist) <b):
Ealist = {' Rows ': alist, ' Total ': all}
Else
For I in Range (A*B):
Easy_list.append (Alist[i])
Ealist = {' Rows ': easy_list, ' Total ': all}
Easylist = Json.dumps (Ealist,cls=cjsonencoder)
return HttpResponse (easylist)
If there is time in the data, using Json.dumps () will make an error. The next function is to deal with this problem.
Class Cjsonencoder (JSON. Jsonencoder):
def default (self, obj):
Try
If Isinstance (obj, datetime):
Return Obj.struct_time ('%y-%m-%d%h:%m:%s ')
Elif isinstance (obj, date):
da = str (obj.strftime ('%y-%m-%d '))
#return obj.strftime ('%y-%m-%d ')
Return da
Else
Return JSON. Jsonencoder.default (self, obj)
Except Exception,e:
Print E
Django + python + easyui datagrid