參考網址:
javascript
http://www.cnblogs.com/firesnow/archive/2013/04/07/3006508.htmlhtml
http://xqqing79.iteye.com/blog/1219426java
HTML代碼:web
<script type="text/javascript"> $(document).ready(function() { getDataTables(); }); function getDataTables(){ var title=$("input[name='gjzSelect']").val(); var selectType=$("#selectType").val(); var startTime=$("#c10").val(); var endTime=$("#c11").val(); var t = $('#example').dataTable({ "processing" : true, "serverSide" : true, "language" : { "url": '<%=basePath%>/resources/zh_CN.json', }, "sAjaxSource" : "<%=basePath%>jiankong/selectSensitiveWordMonitor.do", "fnServerData": retrieveData, "searching": true, "columns" : [ /* {"data": "id"}, */ {"data": "sensitivewordUrl"}, /* {"data": "sensitivewordLevel"}, {"data": "sensitivewordName"}, {"data": "sensitivewordHandleInfo"}, */ {"data": "sensitivewordContent"}, {"data": "taskId"}, ], }); function retrieveData(sSource, aoData, fnCallback) { $.ajax({ //這個就是請求地址對應sAjaxSource url : sSource, //這個是把datatable的一些基本數據傳給後臺,好比起始位置,每頁顯示的行數 data : {"startTime":startTime, "aoData":JSON.stringify(aoData)}, type : 'POST', dataType : 'json', async : false, success : function(result) { //把返回的數據傳給這個方法就能夠了,datatable會自動綁定數據的 fnCallback(result); }, error : function(msg) { alert("服務器發生錯誤請稍後再試!"); } }); } } </script>
服務器端代碼ajax
public void selectSensitiveWordMonitor(){ User user = (User) request.getSession().getAttribute("loginUser"); Map map = new HashMap(); Map mapList = new HashMap(); Map<String, String> mapData = new HashMap<String, String>(); StringBuffer taskIds = new StringBuffer(); List list = new ArrayList(); map.put("userName", user.getUsername()); //獲得站點數據 List<UserWebSite> websiteList = service.selectWebsite(map); if(websiteList.size() > 0){ for (UserWebSite userWebSite : websiteList) { Map mapBack = new HashMap(); mapList.put("websiteid", userWebSite.getId()); //獲得敏感詞數據 List<SensitiveWord> sensitiveWordList = service.selectSensitiveWord(mapList); if( sensitiveWordList.size() > 0 ){ taskIds.append("'").append(sensitiveWordList.get(0).getTaskId()).append("',"); } } } if(taskIds.length() > 0){ mapData.put("taskIds", taskIds.toString().substring(0, taskIds.length()-1)); }else{ mapData.put("taskIds", "NULL"); } String aoData = request.getParameter("aoData"); JSONArray jsonarray = JSONArray.fromObject(aoData); String sEcho = null; // 記錄操做的次數 每次加1 String iDisplayStart ="0"; // 起始索引 String iDisplayLength = "0"; // 每頁顯示的行數 int orderColumn = 0; String orderDir = "asc"; String search = ""; 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.getString("value"); if (obj.get("name").equals("iDisplayLength")) iDisplayLength = obj.getString("value"); if (obj.get("name").equals("sSortDir_0")) orderDir = obj.getString("value"); if (obj.get("name").equals("iSortCol_0")) orderColumn = obj.getInt("value"); if (obj.get("name").equals("sSearch")) search = obj.getString("value"); } String orderSql = ""; //String[] cols = {"id", "sensitivewordUrl", "sensitivewordLevel","sensitivewordName","sensitivewordHandleInfo","sensitivewordContent","taskId"}; if(orderColumn != -1){ switch(orderColumn){ /* case 0: orderSql = " order by id ".concat(orderDir); break;*/ case 0: orderSql = " order by sensitivewordUrl ".concat(orderDir); break; case 1: orderSql = " order by sensitivewordLevel ".concat(orderDir); break; /* case 3: orderSql = " order by sensitivewordName ".concat(orderDir); break; case 4: orderSql = " order by sensitivewordHandleInfo ".concat(orderDir); break; case 5: orderSql = " order by sensitivewordContent ".concat(orderDir); break; */ case 2: orderSql = " order by taskId ".concat(orderDir); break; } } Integer recordsTotal = 0; //表的總記錄數 Integer recordsFiltered = 0; //條件過濾後記錄數 recordsTotal = service.getUserSensitiveWordInfoSum(mapData); //List<SensitiveWordInfo> sensitiveWordInfoList = service.selectSensitiveWordInfoFiltered(map); List<String> sArray = new ArrayList<String>(); if(!search.equals("")){ /*sArray.add(" id LIKE '%" + search + "%'");*/ sArray.add(" sensitivewordUrl LIKE '%" + search + "%'"); /* sArray.add(" sensitivewordLevel LIKE '%" + search + "%'"); sArray.add(" sensitivewordName LIKE '%" + search + "%'"); sArray.add(" sensitivewordHandleInfo LIKE '%" + search + "%'"); */ sArray.add(" sensitivewordContent LIKE '%" + search + "%'"); sArray.add(" taskId LIKE '%" + search + "%'"); } String sumSQLWhere = ""; if(sArray.size() == 1){ sumSQLWhere = sArray.get(0); }else if (sArray.size() > 1){ for(int i=0; i<sArray.size()-1; i++){ sumSQLWhere += sArray.get(i) + " OR "; } sumSQLWhere += sArray.get(sArray.size()-1); } List<SensitiveWordInfo> lists = new ArrayList<SensitiveWordInfo>(); mapData.put("start", iDisplayStart); mapData.put("length", iDisplayLength); mapData.put("orderSql", orderSql); if(search.length() > 0){ mapData.put("sumSQLWhere", sumSQLWhere); recordsFiltered = service.getUserSensitiveWordInfoSum(mapData); lists = service.selectSensitiveWordInfoFiltered(mapData); }else{ recordsFiltered = recordsTotal; lists = service.selectSensitiveWordInfoFiltered(mapData); } lists = service.selectSensitiveWordInfoFiltered(mapData); JSONObject json = new JSONObject(); json.put("sEcho", sEcho); json.put("aaData", lists); json.put("iTotalRecords", recordsTotal); json.put("iTotalDisplayRecords", recordsFiltered); outString(json.toString()); }
public void outString(String str) { try { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); Writer out = response.getWriter(); out.write(str); out.flush(); out.close(); } catch (IOException e) { log.error(e.getMessage(), e); } }
dataTables頁面進行分頁,其HMTL代碼以下:json
$.ajax({ url:'<%=basePath%>jiankong/selectSensitiveWord.do', cache:false, data:{ 'startTime':startTime, 'endTime':endTime, 'title' :title }, dataType:'json', error:function (e){ }, success:function (data){ var json=data; html+='<table id="selectSensitiveWord_table" class="display" cellspacing="0" width="100%"><thead><tr><th width=120px>異常發生時間</th><th>監控對象</th><th></th><th>敏感字</th></tr></thead>'; html+='<tbody>'; for(var i = 0; i < json.length; i++){ if(json[i].sensitiveWordInfoList != '0'){ for(var j=0;j<json[i].sensitiveWordInfoList.length;j++){ html+='<tr>'; html+='<td>'+ getDateTime(json[i].securityBugTime) +'</td>'; html+='<td>'+ json[i].websiteAddress +'</td>'; html+='<td>'+ json[i].sensitiveWordInfoList[j].sensitivewordLevel +'</td>'; html+='<td>'+ json[i].sensitiveWordInfoList[j].sensitivewordContent +'</td>'; html+='</tr>'; } } } html+='</tbody>'; html+='</table>'; $("#selectSensitiveWord").html(html); var table = $('#selectSensitiveWord_table').dataTable({ "bLengthChange": false, "iDisplayLength": 10, "language" : { "url": '<%=basePath%>resources/zh_CN.json', }, "searching" : false, }); } });