ajax分页小功能(table)尚不完善,在更新。

前端js代码:

        autoinit(0,4)
        function refresh(pageindex){
            var parent = document.getElementById("contentlist");
            while(parent.childNodes.length > 2){
                parent.removeChild(parent.lastChild);
            }
            autoinit(pageindex,4)
        }
        
        function autoinit(pageindex,pagesize){
            $.ajax({
                type:"post",
                url:"<%=request.getContextPath()%>/servletpage",
                data:{"pageindex":pageindex,"pagesize":pagesize},
                success:function(returndata){
                    var returndata = eval(returndata);
                    for(var i = 0 ; i < pagesize ; i++){
                        newtr = document.createElement("tr");
                         newtdtitle = document.createElement("td");
                        newtdtitle.innerHTML = returndata[i].title;
                        newtdtime = document.createElement("td");
                        newtdtime.innerHTML = " ";
                        newtduser = document.createElement("td");
                        newtduser.innerHTML = returndata[i].user;
                        newtr.appendChild(newtdtitle);
                        newtr.appendChild(newtdtime);
                        newtr.appendChild(newtduser);
                        document.getElementById("contentlist").appendChild(newtr)
                    }
                }
            });

后台处理:

        int pageindex = Integer.parseInt(request.getParameter("pageindex"));
        int pagesize = Integer.parseInt(request.getParameter("pagesize"));
        try {
            List<Info> infos = new InfoDao().getInfos(pageindex,pagesize);
            JSONArray ja = new JSONArray();
            for(int i = 0 ; i < infos.size(); i++){
                JSONObject jo = new JSONObject();
                jo.put("title", infos.get(i).getTitle());
                jo.put("user", infos.get(i).getPubuser());
                ja.add(jo);
            }
            PrintWriter out = response.getWriter();
            out.print(ja);
        } catch (ClassNotFoundException | SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


你可能感兴趣的:(ajax分页小功能(table)尚不完善,在更新。)