The DataGrid application of the JQuery Easyui tutorial (ii)

Source: Internet
Author: User

Last write to let database data in the DataGrid display, we just simple implementation of the display, carefully read the message displayed does not achieve our ideal effect, here we enrich:

The result of the last display is this

Click to view previous: jQuery Easyui Tutorial of the DataGrid application (i)

It's not hard to find the birthday format is milliseconds (Long data), not the format we want to date, then we'll modify

We write the method of the format time in JS, and in the Birthday column use formatter to call the method format time,

//Format time//convert long date to desired type function getDate (date) {    //Get Date object    var d = new Date (date);        Get Date    var year = D.getfullyear ();    var month = (D.getmonth () + 1);    var day = D.getdate ();        Package    var tt = year+ "-" + (month<ten?) 0 "+month:month" + "-" + (day<10?) 0 "+day:day);        return TT;    }

At this point, we look at the page and we find that the birthday bar has become the result we want.

Next is the paging, principle: The DataGrid has paging function, we just have to open the page, when sending a request, he will pass two parameters to the servlet, we accept the next pass to Dao,hibernate also provides paging function, we have a combination of good,

1. We now have the DAO layer modify the last code, passing the parameters past

 //  query contact  public  list<phone> showall (int  page, int        rows)                    {init ();                    List  <Phone> list = null  ;                    String hql  = "from Phone where 1=1"  = Se.createquery (HQL). Setmaxresults (rows)                    . Setfirstresult (page -1) * rows). list ();                    Destroy ();       return   list; }    

2. And also to add a method for the total number of queries

Here we have to query the total number, it is worth noting that we use the object type of the list collection

//gets the number of contact record bars       Public intgettotal () {init (); intRTN = 0 ; List<Object> list =NULL ; String hql= "SELECT count (1) from Phone where 1=1" ; List=se.createquery (HQL). List (); if(List! =NULL&& list.size () > 0)//determine the collection of the obtained non-null and length{RTN= Integer.parseint (list.get (0). toString ());//assigning values to variable RTN} destroy (); returnRtn; }

3. Next we modify our method in the service layer to add the number of records.

 PublicString Getpagejson (intPageintrows) {String Rtn= "{\" total\ ": 0,\" rows\ ": []}";//defining variables and initializing result sets in JSON formatPagejson<Phone> Pjson =NewPagejson<phone> ();//Package Rate Class                intTotal =NewPhonedao (). Gettotal (); if(Total > 0) {List<Phone> list =NewPhonedao (). ShowAll (page, rows);//define a list set merge assignmentString json_list= jsonarray.tojsonstring (list);//to convert a list collection to a JSON collection                            /** Assign values to instantiated objects through the Set method*/pjson.setrows (list);                            Pjson.settotal (total); RTN= Jsonarray.tojsonstring (Pjson);//an array of object JSON types        }                returnRTN;//returns a JSON-type result set}

4. Finally we are modifying the Doget method of the Servlet

protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//transcodingRequest.setcharacterencoding ("UTF-8"); Response.setcharacterencoding ("UTF-8"); Response.setcontenttype ("Text/html"); //receiving Requests//1 Number of rows per pageString rows = Request.getparameter ("Rows") ; //2 pageString page = request.getparameter ("page") ; if(Rows! =NULL&& page! =NULL)//non-null judgment        {            intROWSS = Integer.parseint (rows);//Type Conversions                        intpagess = integer.parseint (page);//Type ConversionsString json_list=NewPhoneservice (). Getpagejson (Pagess, ROWSS);//Method Invocationresponse.getwriter (). write (json_list); }        Else{response.getwriter (). Write ("{\" total\ ": 0,\" rows\ ": []}") ; }            }

Here, our paging function is implemented.

The next DataGrid additions and deletions to change

JQuery Easyui Tutorial for the DataGrid Application (ii)

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.