后台java:
[code="java"]
//接受参数page和rows
String page =request.getParameter("page");//每页显示的记录数
String rows = request.getParameter("rows");//当前第几页
String stu_id = request.getParameter("stu_id");
String stu_name = request.getParameter("stu_name");
System.out.println(stu_id);
System.out.println(stu_name);
//当前页
int intPage = Integer.parseInt((page == null || page == "0") ? "1":page);
//每页显示条数
int number = Integer.parseInt((rows == null || rows == "0") ? "10":rows);
//每页的开始记录 第一页为1 第二页为number +1
int start = (intPage-1)*number;
PrintWriter out = response.getWriter();
StudentDAO sd = new StudentDAOImpl();
//得到总共有多少条数据
int totalCount = sd.getCount(stu_id,stu_name);
ArrayList<Student> students= sd.getStudents(start,number,stu_id,stu_name);
Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map
jsonMap.put("total",totalCount);//total键 存放总记录数,必须的
jsonMap.put("rows", students);//rows键 存放每页记录 list
JSONObject result = JSONObject.fromObject(jsonMap);//格式化result 一定要是JSONObject
System.out.println(result);
out.print(result.toString());
out.flush();
out.close();
[/code]
js:[code="js"]
<br> $(function(){<br> $('#dg').datagrid('getPager').pagination({<br> displayMsg:'当前显示从{from}到{to},共{total}记录',<br> onBeforeRefresh:function(pageNumber, pageSize){<br> $(this).pagination('loading');<br> $(this).pagination('loaded');<br> }<br> });<br> });</p> <pre><code></script> </code></pre> <p></head><br> <body></p> <pre><code><table id="dg" title="My Users" class="easyui-datagrid" style="width:700px;height:400px" url="test" pagination="true" toolbar="#toolbar" rownumbers="true" fitColumns="true" singleSelect="true"> <thead> <tr> <th field="id" width="50">id</th> <th field="stu_id" width="50">stu_id</th> <th field="stu_name" width="50">stu_name</th> <th field="stu_password" width="50">stu_password</th> <th field="stu_yuanxi" width="50">stu_yuanxi</th> <th field="stu_phone" width="50">stu_phone</th> <th field="stu_qq" width="50">stu_qq</th> <th field="stu_sex" width="50">stu_sex</th> <th field="stu_age" width="50">stu_age</th> </tr> </thead> </table> </code></pre> <p><div id="toolbar"> <br> <div><br> <span>stu_id:</span><br><br> <input id="stu_id" style="line-height:26px;border:1px solid #ccc"><br><br> <span>stu_name:</span><br><br> <input id="stu_name" style="line-height:26px;border:1px solid #ccc"><br><br> <a href="#" class="easyui-linkbutton" plain="true" onclick="doSearch()">Search</a><br><br> </div> <br> <div> <br> <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">New User</a><br><br> <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">Edit User</a><br><br> <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">Remove User</a><br><br> </div><br> </div> </p> <pre><code><div id="dlg" class="easyui-dialog" style="width:400px;height:280px;padding:10px 20px" closed="true" buttons="#dlg-buttons"> <div class="ftitle">User Information</div> <form id="fm" method="post" novalidate> <div class="fitem"> <label>stu_id:</label> <input name="stu_id" class="easyui-validatebox" required="true"> </div> <div class="fitem"> <label>stu_name:</label> <input name="stu_name" class="easyui-validatebox" required="true"> </div> <div class="fitem"> <label>stu_password:</label> <input name="stu_password"> </div> <div class="fitem"> <label>stu_yuanxi:</label> <!-- validType="email" --> <input name="stu_yuanxi" class="easyui-validatebox" > </div> <div class="fitem"> <label>stu_phone:</label> <input name="stu_phone" class="easyui-validatebox" required="true"> </div> <div class="fitem"> <label>stu_qq:</label> <input name="stu_qq" class="easyui-validatebox" required="true"> </div> <div class="fitem"> <label>stu_sex:</label> <input name="stu_sex" class="easyui-validatebox" required="true"> </div> <div class="fitem"> <label>stu_age:</label> <input name="stu_age" class="easyui-validatebox" required="true"> </div> </form> </div> <div id="dlg-buttons"> <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveUser()">Save</a> <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">Cancel</a> </div> <script type="text/javascript"> var url; function newUser(){ $('#dlg').dialog('open').dialog('setTitle','New User'); $('#fm').form('clear'); url = 'add'; } function editUser(){ var row = $('#dg').datagrid('getSelected'); if (row){ $('#dlg').dialog('open').dialog('setTitle','Edit User'); $('#fm').form('load',row); url = 'edit?id='+row.id; } } function saveUser(){ $('#fm').form('submit',{ url: url, onSubmit: function(){ return $(this).form('validate'); }, success: function(result){ var result = eval('('+result+')'); if (result.success){ $('#dlg').dialog('close'); // close the dialog $('#dg').datagrid('reload'); // reload the user data } else { $.messager.show({ title: 'Error', msg: result.msg }); } } }); } function destroyUser(){ var row = $('#dg').datagrid('getSelected'); if (row){ $.messager.confirm('Confirm','Are you sure you want to remove this user?',function(r){ if (r){ $.post('destroy',{id:row.id},function(result){ if (result.success){ $('#dg').datagrid('reload'); // reload the user data } else { $.messager.show({ // show error message title: 'Error', msg: result.msg }); } },'json'); } }); } } function doSearch(){ $('#dg').datagrid('load',{ stu_id: $('#stu_id').val(), stu_name: $('#stu_name').val(), }); } </script> </code></pre> <p>[/code]</p> <p>前台怎么传值过去,就是说 function doSearch(){<br> $('#dg').datagrid('load',{<br> stu_id: $('#stu_id').val(),<br> stu_name: $('#stu_name').val(),<br> });<br> }</p> <p>这块怎么写url,怎么把值传到后台</p>
你完全可以使用同一个url查询的,只是第一次传过去的参数为0,查询时传过去的参数不为0而已。然后在service层拼接sql调用dao层就可以了,不知道我说的你能否明白
定义 datagrid 时设置了URL,在调用 load 方法不需要传URL,你最后写的doSearch方法中的调用就是正确的参数传递方法。
$('#list').datagrid('options').url = '../../Ashx/News/News_Comment.ashx?func=GetList';
$('#list').datagrid("reload");