English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Introduction
Based on SpringMVC+Bootstrap+DataTables implements server-side pagination, fuzzy search (not DataTable Search), and page asynchronous refresh.
Note: The sp:message tag uses SpringMVC internationalization
Effect
DataTable Table
Keyword Query
Custom Keyword Query, Not DataTable Search
Code
HTML Code
Query Condition Code
<!-- Query, Add, Batch Delete, Export, Refresh --> <div class="row-fluid"> <div class="pull-right"> <div class="btn-group"> <button type="button" class="btn btn-primary btn-sm" id="btn-add"> <i class="fa fa-plus"></i> <sp:message code="sys.add"/> </button> <button type="button" class="btn btn-primary btn-sm" id="btn-delAll"> <i class="fa fa-remove"></i> <sp:message code="sys.delAll"/> </button> <button type="button" class="btn btn-primary btn-sm" id="btn-export"> <i class="fa fa-sign-in"></i> <sp:message code="sys.export"/> </button> <button type="button" class="btn btn-primary btn-sm" id="btn-re"> <i class="fa fa-refresh"></i> <sp:message code="sys.refresh"/> </button> </div> </div> <div class="row"> <form id="queryForm" action="<%=path%>/goodsType/list" method="post"> <div class="col"}-xs-2"> <input type="text" id="keyword" name="keyword" class="form-control input-sm" placeholder="<sp:message code="sys.keyword"/>"> </div> <button type="button" class="btn btn-primary btn-sm" id="btn-query"> <i class="fa fa-search"></i> <sp:message code="sys.query"/> </button> </form> </div> </div>
Data table code
<table id="dataTable" class="table table-striped table-bordered table-hover table-condensed" align="center"> <thead> <tr class="info"> <td><input type="checkbox" id="checkAll"></td> <th><sp:message code="sys.no"/></th> <th><sp:message code="goods.type.name.cn"/></th> <th><sp:message code="goods.type.name.en"/></th> <th><sp:message code="sys.create.time"/></th> <th><sp:message code="sys.update.time"/></th> <th><sp:message code="sys.oper"/></th> </tr> </thead> </table>
JS code
DataTables initialization, server-side data request, query condition encapsulation
<!-- page script --> <script> $(function () { //Add, modify asynchronous submission address var url = ""; var tables = $("#dataTable").dataTable({ serverSide: true,//Pagination, fetching data, and so on are all placed on the server side processing: true,//When loading data, whether to display "loading" pageLength: 10, //number of data items loaded for the first time ordering: false, //Sorting operations are performed on the server side, so it can be turned off. pagingType: "full_numbers", autoWidth: false, stateSave: true,//Maintain pagination state, used in combination with comTable.fnDraw(false); searching: false,//disable datatables search ajax: { url: "<%=path%>/goodsType/getData", dataSrc: "data", data: function (d) { var param = {}; param.draw = d.draw; param.start = d.start; param.length = d.length; var formData = $("#queryForm").serializeArray();//serialize the data in the form into an array formData.forEach(function (e) { param[e.name] = e.value; }); return param;//custom parameters to be passed. }, }, columns: [//corresponding to the sequence in the above thead {"data": null,"width":"10px"}, {"data": null}, {"data": 'typeNameCn' }, {"data": 'typeNameEn' }, {"data": 'createTime', "render":function(data,type,full,callback) { return data.substr(0,19) } }, {"data": 'updateTime', defaultContent: "", "render":function(data,type,full,callback) { if(data != null && data != ""){ return data.substr(0,19) } return data; } } }, {"data": null,"width":"60px"} ], //operation button columnDefs: [ { targets: 0, defaultContent: "<input type='checkbox' name='checkList'>" }, { targets: -1, defaultContent: "<div class='btn-group'>"+ ""+ ""+ "
Java code
Controller handling method, responsible for querying the table data required by the page, formatting Json and returning it.
@RequestMapping(value="/goodsType/getData", produces = "text/json;charset=UTF-8") @ResponseBody public String getData(HttpServletRequest request, QueryCondition query) { DatatablesView<GoodsType> dataTable = goodsTypeService.getGoodsTypeByCondition(query); dataTable.setDraw(query.getDraw()); String data = JSON.toJSONString(dataTable); return data; }
Return Json data format
{ "data": [{ "createTime": "2016-10-27 09:42:28.0", "typeId": 96824775296417986, "typeIdStr": "96824775296417986", "typeNameCn": "食品", "typeNameEn": "Foods", "updateTime": "2016-10-28 13:00:24.0" }, { "createTime": "2016-10-27 09:42:27.0", "typeId": 96824775296417979, "typeIdStr": "96824775296417979", "typeNameCn": "汽车" "typeNameEn": "Cars123", "updateTime": "2016-10-27 09:51:24.0" }], "draw": 1, "recordsFiltered": 17, "recordsTotal": 17 }
DatatablesView, defined according to the formatting requirements of DataTables
public class DatatablesView<T> { private List<T> data; //data corresponds to "dataSrc" loaded by datatales private int recordsTotal; private int recordsFiltered; private int draw; public DatatablesView() { } public int getDraw() { return draw; } public void setDraw(int draw) { this.draw = draw; } public List<T> getData() { return data; } public void setData(List<T> data) { this.data = data; } public int getRecordsTotal() { return recordsTotal; } public void setRecordsTotal(int recordsTotal) { this.recordsTotal = recordsTotal; this.recordsFiltered = recordsTotal; } public int getRecordsFiltered() { return recordsFiltered; } public void setRecordsFiltered(int recordsFiltered) { this.recordsFiltered = recordsFiltered; } }
Service business processing class, mainly counts the number of records based on the query conditions and queries the data list of the current page
public DatatablesView<GoodsType> getGoodsTypeByCondition(QueryCondition query) { DatatablesView<GoodsType> dataView = new DatatablesView<GoodsType>(); //Build query conditions WherePrams where = goodsTypeDao.structureConditon(query); Long count = goodsTypeDao.count(where); List<GoodsType> list = goodsTypeDao.list(where); dataView.setRecordsTotal(count.intValue()); dataView.setData(list); return dataView; }
The Dao layer is the basic database query operation, and the details are omitted...
End
The search conditions here only use keyword fuzzy search. According to business needs, other query conditions can be dynamically added, and the background needs to make corresponding processing.
The following is what the editor introduces to everyone based on SpringMVC+Bootstrap+DataTables implements table server-side pagination and fuzzy search, hoping it will be helpful to everyone. If you have any questions, please leave me a message, and the editor will reply to everyone in time!
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report any violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.