您的位置:首页 > Web前端 > JavaScript

js实现对选中的多条记录进行删除操作

2014-08-18 19:39 399 查看
javascript代码:

<script type="text/javascript">
function deleteCategories(){
var primaryIDs=document.getElementsByName("id");
var ids="";
for (var i=0; i<primaryIDs.length; i++){
if(primaryIDs[i].type=="checkbox"&&primaryIDs[i].checked==true){
ids+=primaryIDs[i].value + ";";
}
}
if(ids=="")
{
alert("请选择要删除的信息!");
return;
}
else
{
confirm("真的要删除吗?");
}
document.getElementsByName("selectedIds")[0].value=ids;
document.categoryListForm.action="${pageContext.request.contextPath}/category.do?method=deleteCategories";
document.categoryListForm.submit();
}
</script>
html代码:
<form method="post" action="" name="categoryListForm">
<input type="hidden" name="selectedIds" />
<table width="80%" border=1 align="center" cellpadding="0"
cellspacing="0" bordercolor="#ccccff" bgcolor="#ccddee">
<tr>
<th>
<input type="checkbox" name="checkbox0" id="xx" onClick="chk()" />
</th>
<th>
Line Number
</th>
<th>
Category ID
</th>
<th>
Category Name
</th>
<th>
Category Descn
</th>
<th>
CreateTime
</th>
<th>
PublishDate
</th>
<th>
Action
</th>

</tr>
<c:forEach items="${categoryList}" var="category" varStatus="status">
<tr>
<td align="center">
<input type="checkbox" name="id" value="${category.id}">
</td>
<td align="center">
${status.count}
</td>
<td align="center">
${category.id }
</td>
<td align="center">
<a
href="${pageContext.request.contextPath}/category.do?method=viewCategory&categoryId=${category.id }"
class="text_link">${category.name}</a>
</td>
<td align="center">
${category.descn}
</td>
<td align="center">
${category.createTime}
</td>
<td align="center">
${category.publishDate}
</td>
<td align="center">
<a
href="${pageContext.request.contextPath}/category.do?method=deleteCategory&categoryId=${category.id }">delete</a>
</td>
</tr>
</c:forEach>
</table>
<p align="center">
<input type="button" value="return"
onclick="self.location='${pageContext.request.contextPath}'" />
<input type="button" value="Delete Selected"
onclick="deleteCategories()" />
<input type="button" value="Add Category" onclick="toAddCategory()" />
</p>
</form>

java代码:
/**
* 根据id删除多个分类。
*
* @param selectedIds
* @return
* @throws Exception
*/
public String deleteCategories(@Read(key = "selectedIds")
String selectedIds) throws Exception {
StringTokenizer token = new StringTokenizer(selectedIds, ";");
while (token.hasMoreTokens()) {
long id = Long.parseLong(token.nextToken());
categoryService.deleteCategoryById(id);
}

List<Category> categoryList = new ArrayList<Category>();
categoryList = categoryService.listCategory();
this.getAttributeUtil().setAttribute("categoryList", categoryList);

return "deleteSuccess";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐