Django + python + easyui datagrid

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.