Ajax實(shí)現(xiàn)動(dòng)態(tài)顯示并操作表信息的方法
在jsp連接數(shù)據(jù)庫訪問并顯示數(shù)據(jù)庫信息時(shí),使用Ajax利用json對象會(huì)在頁面不刷新的情況下獲取到數(shù)據(jù)。但若是要顯示數(shù)據(jù)庫表中的信息,就需要?jiǎng)討B(tài)的生成表的行以及單元格。并且對每一行的操作也是需要?jiǎng)討B(tài)綁定的。
今天分享給各位的是完成在對數(shù)據(jù)庫表信息的顯示、增加、刪除、修改。顯示時(shí)通過用HTML代碼來控制table行的增加。修改和刪除是通過button的onclick()事件完成的。onclick()的參數(shù)也是動(dòng)態(tài)改變的,這樣的話在操作時(shí)就可以知道是要對哪一行進(jìn)行操作了。修改的方法中又用到修改HTML代碼使普通<td>變?yōu)?lt;input>并獲取到原始值作為輸入框的默認(rèn)值,在輸入框失去焦點(diǎn)后自動(dòng)保存數(shù)據(jù)。并再把<input>變?yōu)?lt;td>
代碼很詳細(xì),希望能對你有所幫助。
js文件內(nèi)容如下:
$(function () {
$.ajaxSetup({
async:false
});
var url = "/Task/Fenlei"; //servlet的url
data = {};
data.flag = "all";
$.post(url,data,function (result) {
for(var i=0;i<result.getAll.length;i++){
var id = result.getAll[i].fenlei_Id;
var name = result.getAll[i].fenlei_Name;
var newrow = "<tr id="tr"+id+""><td>"+result.getAll[i].fenlei_Id+"</td><td id="td"+id+"">"+result.getAll[i].fenlei_Name+
"</td><td><button onclick="del("+id+")"">刪除</button><button onclick="edit("+id+")">修改</button></td></tr>"
$("#AllInfo tr:last").after(newrow);
}
},"json");
$("#add").click(function () {
addData={};
var name = $("#name").val();
addData.name = name;
addData.flag = "add";
$.post(url,addData,function (result) {
var id = result.aFenlei.fenlei_Id;
var name = result.aFenlei.fenlei_Name;
var newrow = "<tr id="tr"+id+""><td>"+result.aFenlei.fenlei_Id+"</td><td id="td"+id+"">"+result.aFenlei.fenlei_Name+
"</td><td><button onclick="del("+id+")">刪除</button><button onclick="edit("+id+")">修改</button></td></tr>"
$("#AllInfo tr:last").after(newrow);
},"json");
});
});
function del(id) {
console.log(id);
var url = "/Task/Fenlei";
delData = {};
delData.flag = "delete";
delData.id = id;
$.post(url,delData,function (result) {
if(result) {
alert("刪除成功");
$("#tr"+id).remove();
} else {
alert("刪除失敗");
}
},"json");
};
function edit(id) {
var url = "/Task/Fenlei";
editData = {};
editData.flag = "update";
var oldname = $("#td"+id).text();
$("#td"+id).html("<input type="text"class="Input" id="new" name="FenleiName" value=""+oldname+""/>");
$("#new").blur(function () {
var newname = $(".Input").val();
editData.newname = newname;
console.log(newname);
$("#td"+id).html("<td id="td"+id+"">"+newname+"</td>");
$.post(url,editData,function(result){
if(result) {
alert("修改成功");
} else {
alert("修改失敗");
}
},"json");
});
}
jsp頁面代碼如下:
<%@include file="../inc/top.jsp"%>
<script src="Fenlei.js"></script>
<div>
<h3 align="center">項(xiàng)目管理信息表</h3>
<div>
<img src="#" />
</div>
<div>
分類名稱:<input type="text" id="name"/>
<button type="button" id="add">添加</button>
</div>
<tableid="AllInfo">
<tr>
<th>分類Id</th>
<th>分類名稱</th>
<th>操作</th>
</tr>
</table>
</div>
<%@include file="../inc/bottom.jsp"%>
處理的servlet內(nèi)容如下:
public class FenleiServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String name = request.getParameter("name"); //項(xiàng)目分類名稱
String flag = request.getParameter("flag");
String id = request.getParameter("id"); //項(xiàng)目分類Id
FenleiService cs = new FenleiService();
JSONObject json = new JSONObject();
if("all".equals(flag)) {
List<FenleiBean> list = cs.getAll(); //獲取所有的項(xiàng)目分類信息
json.put("getAll",list);
response.getWriter().print(json.toJSONString());
}
if("add".equals(flag)) { //增加操作。
FenleiBean cb = cs.add(name);
json.put("aFenlei",cb);
response.getWriter().print(json.toJSONString());
}
if("delete".equals(flag)) { //刪除操作
boolean result = cs.delete(id);
System.out.println(flag);
System.out.println(result);
if(result){
json.put("result",result);
json.put("msg","刪除成功");
response.getWriter().print(json.toJSONString());
System.out.println(json.toJSONString());
} else {
json.put("result",result);
json.put("msg","刪除失敗");
response.getWriter().print(json.toJSONString());
}
}
if("update".equals(flag)) { //更新信息
System.out.println(flag);
String newname = request.getParameter("newname");
System.out.println("---------------update newname"+newname);
boolean result = cs.update(newname);
if(result){
json.put("result",result);
json.put("msg","修改成功");
response.getWriter().print(json.toJSONString());
System.out.println(json.toJSONString());
} else {
json.put("result",result);
json.put("msg","修改失敗");
response.getWriter().print(json.toJSONString());
}
}
}
}
</pre><pre name="code">
以上這篇Ajax實(shí)現(xiàn)動(dòng)態(tài)顯示并操作表信息的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持。
相關(guān)文章:
1. 使用AJAX(包含正則表達(dá)式)驗(yàn)證用戶登錄的步驟2. layui Ajax請求給下拉框賦值的實(shí)例3. 使用FormData進(jìn)行Ajax請求上傳文件的實(shí)例代碼4. AJAX實(shí)現(xiàn)數(shù)據(jù)的增刪改查操作詳解【java后臺(tái)】5. 詳談ajax返回?cái)?shù)據(jù)成功 卻進(jìn)入error的方法6. Ajax實(shí)現(xiàn)表格中信息不刷新頁面進(jìn)行更新數(shù)據(jù)7. 解決AJAX返回狀態(tài)200沒有調(diào)用success的問題8. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算9. 告別AJAX實(shí)現(xiàn)無刷新提交表單10. laravel ajax curd 搜索登錄判斷功能的實(shí)現(xiàn)

網(wǎng)公網(wǎng)安備