ajax 显示,删除,批量删除,修改反填功能实现
2019-03-20 15:29 .net小跟班(杜) 阅读(1178) 评论(0) 编辑 收藏 举报1、页面代码
<body>
<h1>显示所有员工信息</h1>
<input id="Button1" type="button" value="批量删除" onclick="PatchDel()" />
<div>
<table class="table">
@*标题*@
<thead>
<tr>
<th><input id="CkAll" type="checkbox" /></th>
<th>账号</th>
<th>真实姓名</th>
<th>电话</th>
<th>密码</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
@*内容*@
<tbody id="tbd"></tbody>
</table>
</div>
</body>
2、ajax
<script>
//文档准备就绪函数
$(function () {
lists();
})
//显示信息
function lists() {
$.ajax({
url: "http://localhost:51071/api/User",
type: "get",
success: function (data) {
//清空tbd
$("#tbd").empty();
for (var item in data) {
//进行拼接
$("#tbd").append(
"<tr>" +
//依次获取字段
"<th><input id='Checkbox1' class='Ck' type='checkbox' value='" + data[item].Id + "' /></th>" +
"<th>" + data[item].Name + "</th>" +
"<th>" + data[item].RealName + "</th>" +
"<th>" + data[item].Telphone + "</th>" +
"<th>" + data[item].Pass + "</th>" +
"<th>" + (data[item].Status==0?"禁用":"启用") + "</th>" +
"<th><input id='btndel' type='button' value='修改' onclick='Edit(" + data[item].Id + ")' />" +
"<input id='btnupdate' type='button' value='删除' onclick='Delete(" + data[item].Id + ")' /></th>" +
"</tr>");
}
}
});
}
function Delete(id) {
if (confirm("确认删除?")) {
$.ajax({
url: "http://localhost:1234/api/Student/" + id,
type: "delete",
success: function (data) {
if (data > 0) {
alert("删除成功!共删除了" + data + "条数据");
location.reload();
}
else {
alert("删除失败!");
}
}
});
}
}
// 跳转修改页面
function Edit(id) {
location.href = '/User/Edit/' + id;
}
// 全选
$("#CkAll").click(function () {
$(".Ck").prop("checked", $(this).prop("checked"))
})
//批量删除
function PatchDel() {
if (confirm("确认删除?")) {
var ids = [];
$(".Ck:checked").each(function () {
ids.push($(this).val());
});
$.ajax({
url: "http://localhost:51071/api/User?ids=" + ids,
type: "delete",
success: function (data) {
if (data > 0) {
alert("删除成功!共删除了" + data + "条数据");
location.reload();
}
else {
alert("删除失败!");
}
}
});
}
}
</script>
3、修改
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Edit</title> <link href="~/Content/bootstrap-3.3.7/css/bootstrap.css" rel="stylesheet" /> <script src="~/jquery类库/jquery-3.1.1.js"></script> </head> <body> <div> <h1>修改员工信息</h1> <table align="center"> <tr> <th>员工账号:</th> <td> <input id="txtName" type="text" /> </td> </tr> <tr> <th>员工真实姓名:</th> <td> <input id="txtRelaName" type="text" /> </td> </tr> <tr> <th>员工电话:</th> <td> <input id="txtTel" type="text" /> </td> </tr> <tr> <th>员工密码:</th> <td> <input id="txtPwd" type="text" /> </td> </tr> <tr> <th>状态:</th> <td> <input id="Radio1" type="radio" name="use" value="0" />禁用 <input id="Radio2" type="radio" name="use" value="1" />启用 </td> </tr> <tr> <td></td> <td> <input id="Button1" type="button" value="保存" onclick="Save()" /> </td> </tr> </table> </div> </body> </html> <script> $(function () { ReturnFill(); }) function Save() { var id = @ViewBag.id; var name = $("#txtName").val(); var realName = $("#txtRelaName").val(); var tel = $("#txtTel").val(); var pwd = $("#txtPwd").val(); var sss = ($("#Radio1").prop("checked") ? "false" : "true"); $.ajax({ url: "http://localhost:51071/api/User", type: "put", data: { Id:id, Name: name, RealName: realName, Telphone: tel, Pass: pwd, Status: sss}, success: function (data) { if (data > 0) { alert("修改成功!") if (confirm("返回到员工列表?")) { location.href = "/User/Index" } } else { alert("修改失败!"); } } }); } //反填 function ReturnFill() { var Id = @ViewBag.id; $.ajax({ url: "http://localhost:51071/api/User/" + Id, type: "get", success: function (data) { $("#txtName").val(data[0].Name); $("#txtRelaName").val(data[0].RealName); $("#txtTel").val(data[0].Telphone); $("#txtPwd").val(data[0].Pass); if (data[0].Status == true) { $("#Radio2").prop("checked", true) } else{ $("#Radio1").prop("checked", true) } } }) } </script>