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
<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.