[基础]由复选框实现的批量删除

批量删除:

选中多个复选框:

每一条记录:<input type="checkbox" name="delete" value="${Hotel.id}"/>
 
function doDelete(){
				var inputs = document.getElementsByName("delete");
				var flag = false;
				var ids = "";	
				for(var i =0 ; i<inputs.length;i++ ){
					if(inputs[i].checked){
						flag = true;
						if(ids == ""){
							ids = inputs[i].value;
						}else{
							ids += ","+inputs[i].value;
						}
					}
				}
				if(!flag){
					alert("Please select the data you want to delete");
					return;
				}
				if(!confirm("Are you confirm to delete?")) return;
				$("doAction").value = "delete";
				document.forms[0].action += "?ids=" + ids;
				document.forms[0].submit();
			}

 删除时先由Id查出对象,再删除对象,即可实现删除!

 

else if("delete".equalsIgnoreCase(doAction)){
			String deleteIds = request.getParameter("deleteIds");
			String[] ids = deleteIds.split(",");
			List<UnsubscribeEmail> list = (List<UnsubscribeEmail>)session.getAttribute(MyConstants.UN_SUBSCRIBE_EMAIL); //数据列表
			for(String str : ids){
				Integer id = Integer.parseInt(str);
				UnsubscribeEmail u = unsubscribeEmailFacade.findById(id);
				if(u!=null){
					unsubscribeEmailFacade.delete(u);
					for(UnsubscribeEmail uEmail : list){
						if(uEmail.getId().equals(id)){
							list.remove(uEmail);   //将要删除的数据的对象从列表中删除
							break;
						}
					}
				}
			}
			request.setAttribute(MyConstants.MESSAGE_KEY, "Delete Success.");
			session.setAttribute(MyConstants.UN_SUBSCRIBE_EMAIL, list);
		}
		return "success";
	}
 
public BookingHotel findById(String id) throws Exception {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(BookingHotel.class);
		detachedCriteria.add(Expression.eq("id", id));
		List<BookingHotel> list = this.getHibernateTemplate().findByCriteria(detachedCriteria);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

比较基础的东西。。。

你可能感兴趣的:(复选框)