今天寫SSM的刪除時的總結

1.刪除模塊的代碼


前提是:我已經把分頁都完成了


信息界面js代碼

<!--用戶信息欄-->
				<div class="b_mes_r_msg">
					<table class="table_stu table table-bordered table-hover success">
						<tr>
							<th>編號</th>
							<th>學號</th>
							<th>姓名</th>
							<th>性別</th>
							<th>年齡</th>
							<th>全選&nbsp;&nbsp;&nbsp;&nbsp;<input id="checkAll" type="checkbox" name="checkAll"></th>
						</tr>
						<c:forEach items="${requestScope.pagemsg.lists}" var="user">
							<tr>
								<td>${user.id}</td>
								<td>${user.number}</td>
								<td>${user.name}</td>
								<td>${user.sex}</td>
								<td>${user.age}</td>
								<td><input type="checkbox" name="check" value="${user.id}"></td>
							</tr>
						</c:forEach>
					</table>

//獲取全選或者非全選框,刪除用戶

<!--  加載script -->
		<script type="text/javascript">
            var i = 1;
            //獲取全選或者非全選框
            $(function(){
                $("#checkAll").click(function(){
                    if(i%2===1){
                        $("input[type='checkbox']").prop("checked",true);
                    }else{
                        $("input[type='checkbox']").prop("checked",false);
                    }
                    i++;
                });
            });
            //刪除選中用戶
			function del() {
				//至少有選中了一項
				var checkedNum = $("input[type='checkbox']:checked").length;
				if (checkedNum == 0){
				    alert("至少選中一項");
				    return;
				}
				if (confirm("確定刪除選中用戶了嗎?")){
				    var userList = new Array();
				    $("input[type='checkbox']:checked").each(function () {
						userList.push($(this).val());
                    });
				    $.ajax({
					    type:"post",
					    url:"${pageContext.request.contextPath}/hello/delUser",
					    contentType:"application/json;charset=utf-8",
					    data:JSON.stringify(userList),
					    dataType:"text",
					    success:function (data) {
							if (data == "OK"){
							    alert("刪除顧客成功");
							    location.reload();
							}else {
							    alert("刪除失敗")
								location.reload();
                            }
                        }
				    });
				}
            }
		</script>

controller類代碼

@RequestMapping("/hello/delUser")
    @ResponseBody
    public String delUser(@RequestBody Integer[] userList){
        int i = service.delUsers(userList);
        if (i>0){
            return "OK";
        }
        return "FAIL";
    }

mapper代碼

<mapper namespace="com.xiao.mapper.UserMapper">
<sql id="Base_Column_List">
        id,number,name,sex,age
    </sql>
<!--刪除用戶-->
    <delete id="delUser" parameterType="integer">
        delete from user where id in 
        <foreach collection="array" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>
</mapper>    

2.複習了Jquery,Ajax,JS

2.1Jquery

  • 通過Jquery獲取input標籤,再使用Input標籤中中帶有屬性爲checkbox屬性的type標籤,如果被選中,接下來使用each來遍歷每一個屬性
    在這裏插入圖片描述
    其中prop是增加元素屬性
    在這裏插入圖片描述

2.2 Ajax

在這裏插入圖片描述

2.3JS不做闡述

3.刪除模塊的**坑**

3.1 首先是Ajax中的問題

  • ajax中前端返回的數據是Json,後端如果想返回數據爲string需要更改dataType信息爲’text’.
  • 在這裏插入圖片描述
  • 在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章