Springmvc + jquery datatable + ajax implement dynamic paging Query

Source: Internet
Author: User

Springmvc + jquery datatable + ajax implement dynamic paging Query

There are many examples of jquery datatable on the Internet, but there are few examples of spring MVC. After two days, I can figure this out and record it as a learning note.

1. Import the required files

(1) jquery.dataTables.css

(2) jquery. js

(3) jquery. dataTables. js

2. datatable. jsp

<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %>
 
 ListView
 
Col1 Col2
<Script type = "text/javascript" src = "js/jquery. js "> </script> <script type =" text/javascript "src =" js/jquery. dataTables. js "> </script> <script type =" text/javascript "> $ (document ). ready (function () {$ ("# tb "). dataTable ({"bProcessing": false, // whether to display the waiting prompt "bServerSide": true, // This indicates that data is obtained through the server "sAjaxSource": "tableDemoAjax.html", // This is the request address "fnServerData ": retrieveData // The processing function for getting data}) ;}); // the names of the three parameters can be randomly named, but must be three parameters, function retrieveData (sSource111, aoData111, fnCallback111) {$. ajax ({url: sSource111, // This is the request address corresponding to sAjaxSourcedata: {"aoData": JSON. stringify (aoData111)}, // This is to pass some basic data of the datatable to the background, such as the starting position, the number of rows displayed on each page type: 'post', dataType: 'json ', async: false, success: function (result) {fnCallback111 (result); // You can pass the returned data to this method, and the datatable will automatically bind the data}, error: function (msg) {}}) ;}</script>

3. Background request address tableDemoAjax

@ RequestMapping ("tableDemoAjax") @ ResponseBodypublic String tableDemoAjax (@ RequestParam String aoData) {JSONArray jsonarray = JSONArray. fromObject (aoData); String sEcho = null; int iDisplayStart = 0; // start index int iDisplayLength = 0; // The number of lines displayed on each page for (int I = 0; I <jsonarray. size (); I ++) {JSONObject obj = (JSONObject) jsonarray. get (I); if (obj. get ("name "). equals ("sEcho") sEcho = obj. get ("value "). toString (); if (obj. get ("name "). equals ("iDisplayStart") iDisplayStart = obj. getInt ("value"); if (obj. get ("name "). equals ("iDisplayLength") iDisplayLength = obj. getInt ("value");} // generates a List of 20 test data entries.
 
  
Lst = new ArrayList
  
   
(); For (int I = 0; I <20; I ++) {String [] d = {"codecomdata" + I, "col2_data" + I}; lst. add (d);} JSONObject getObj = new JSONObject (); getObj. put ("sEcho", sEcho); // you do not know the usage of this value. If you know this value, please inform getObj. put ("iTotalRecords", lst. size (); // the actual number of rows getObj. put ("iTotalDisplayRecords", lst. size (); // The number of displayed rows, which must be the same as the getObj. put ("aaData", lst. subList (iDisplayStart, iDisplayStart + iDisplayLength); // return getObj in JSON format. toString ();}
  
 

4.




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.