Datagrid interface, link database to read data, datagrid Interface

Source: Internet
Author: User

Datagrid interface, link database to read data, datagrid Interface

 

 

1. HTML section of the student list

<Script type = "text/javascript"> $ (function () {// create a dataGrid $ ("# dg "). datagrid ({url: 'studentservlet ', // data source // frozen column frozenColumns: [[{field: 'id', checkbox: true}, {field: 'sno ', title: 'student ID ', width: 100, sortable: true}], \ columns that can be arranged // column definition columns: [[{field: 'sname', title: 'name', width: 100}, {field: 'sclass ', title: 'class', width: 100, align: 'right'}, {field: 'ssex ', title: 'gender ', width: 100, align: 'center', hidden: false}, {field: 'sbirthday', title: 'birthday', width: 100, align: 'center'}], fitColumns: true, // The striped: true cannot be set together with the frozen column, // zebra effect idField: 'sno', // primary key column, rownumbers: true, // display the line number singleSelect: false, // whether to select pagination: true, // display the pageList: [, 20] In the pageList column, // select the list pageSize for the number of lines per page: 5, // The initial page size remoteSort: false, // whether to sort toolbar on the server: [{iconCls: 'icon-edit', text: 'jump to page 2nd ', handler: function () {$ ('# dg '). datagrid ('gopage', 2) }},{ iconCls: 'icon-edit', text: 'jump to page 3rd ', handler: function () {$ ('# dg '). datagrid ('gopage', 3) }}]});}) </script> data table <br> <table id = "dg"> </table> </body>

2. StudentServlet (Data Source)

 

Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request. setCharacterEncoding ("UTF-8"); response. setCharacterEncoding ("UTF-8"); response. setContentType ("text/html"); \\ String spage = request. getParameter ("page"); \ get page String srows = request. getParameter ("rows"); \ get rows if (spage! = Null & srows! = Null) {int page = Integer. parseInt (spage); int rows = Integer. parseInt (srows); String json = new StudentService (). getPageJSON (page, rows); \ call method response. getWriter (). println (json);} else {response. getWriter (). println ("{'Total': 0, 'row': []}") ;}}

3. StudentService () Class

Public class StudentService {// query paging data // return JSON public String getPageJSON (int page, int rows) {String rtn = "{\" total \ ": 0, \ "row \": []} "; int total = new StudentDAO (). getTotal (); if (total> 0) {List <Student> ls = new StudentDAO (). getPageList (page, rows); String ls_json = JSONArray. toJSONString (ls); rtn = "{\" total \ ":" + total + ", \" rows \ ":" + ls_json + "}"; \ Standard json format} return rtn ;}}

 

4. getPageList (retrieve paging data set) and getTotal (retrieve data records) methods in StudentDAO ()

// Obtain the public List of paging data sets <Student> getPageList (int page, int rows) {List <Student> rtn = new ArrayList <Student> (); init (); rtn = se. createQuery ("from Student order by sno "). setMaxResults (rows ). setFirstResult (page-1) * rows ). list (); destroy (); return rtn;} // obtain the number of data entries public int getTotal () {int rtn = 0; init (); List <Object> lo = se. createQuery ("select count (1) from Student "). list (); if (lo! = Null & lo. size ()> 0) {rtn = Integer. parseInt (lo. get (0). toString () ;} destroy (); return rtn ;}

 

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.