2011-04-29 85 views
2

所以我不确定如何标记这一个,但基本上我有一个动态加载的HTML表,其中包含可以删除的值,目前我正在使用用户的复选框决定删除哪一行。这是我到目前为止的代码:帮助从HTML表中删除字段

 <table id="comments" align="center" width="59%"> 
    <thead> 
     <th class="headerClass" width="7%">Delete</th> 
     <th width="15%" class="headerClass">Date</th> 
     <th class="headerClass" width="15%">Employee</th> 
     <th class="headerClass">Comment</th> 
    </thead> 
     <tbody>   
<% 
    while (result.next()) { 

     commentDate = StringUtils.defaultString(result.getString("commentDate")); 
     commentUser = StringUtils.defaultString(result.getString("cName")); 
     comment = StringUtils.defaultString(result.getString("xbs_comment")); 

%> 

     <tr> 
     <td class="normal"><input type="checkbox" class="checkbox" /></td> 
     <td class="normal"><%=commentDate%></td> 
     <td class="normal"><%=commentUser%></td> 
     <td class="normal" width="68%"><%=comment%></td>     
     </tr> 
    </tbody>  
</table>  
    <label for="comment"><h4>Add a Comment:</h4></label> 
    <textarea cols="105" id="comment" name="comment" rows="4"></textarea> 
<br /> 

<div align="center" class="submit"> 
    <input class="submit" width="10%" type="submit" name="submit" id="submit" value="Submit" /> 
</div> 

基本上我有意见表中,用户可以使用提交按钮添加评论,但我想他们也可以使用支票框删除评论。我是否需要另一个带有删除按钮的表格标签,或者我可以只使用这一个提交按钮?另外,如果我确实留在复选框中,我怎么让我的程序(函数和servlet)的后端知道哪些被检查?谢谢你的帮助!!

+0

我添加了jsp和java标签,所以在这里有专业知识的人会看到这些,并可以帮助你。我知道这些标签需要解决您的问题。 – ace 2011-04-30 08:48:39

回答

2

你可以做同样的提交。也可以通过ajax删除评论,然后您可以在删除成功后重新加载页面。

以下是使用相同的提交按钮删除评论的一些方法。我假设用户有权删除表中的所有评论。

but I would like them to also be able to use the check box to delete comments 

要做到这一点,您必须在复选框上为每个评论指定一个值。通常表示每个评论的行中的主键或ID。

对每个复选框使用单个名称来执行多重删除。下面

comments.jsp

<form name="commentForm" action="addDelete.jsp"> 
    <div> 
<table> 
    <thead> 
     <tr> 
      <th>No.</th><th>Date</th><th>User</th><th>Comments</th> 
     </tr> 
    </thead> 
    <tbody> 
     <% 
      for(Comment cmnt : commentList){ 
     %> 
      <tr> 
       <td><input type="checkbox" value="<%=cmnt.getCmntId()%>" name="cmntId" /></td> 
       <td><%=cmnt.getCmntDate()%></td> 
       <td><%=cmnt.getCmntUser()%></td> 
       <td><%=cmnt.getCmntComment()%></td> 
      </tr> 
     <%    
      } 
     %> 
    </tbody> 
</table> 
<textarea cols="50" rows="10" name="newComment"> 
</textarea> 
<br /> 
<input type="submit" value="Delete" /> 
<input type="hidden" name="userId" value="Id_of_the_user"> 
</div> 
</form> 

样品这仅仅是一个例子,所以更注重其中有一个复选框的部分。

addDelete.jsp

现在的adddelete.jsp,你可以有不同的功能两个查询。首先是添加新评论,其次是评论的删除。

要获取要删除的评论列表,请将其存储在数组中。还有其他领域。

String[] cmntIds = request.getParameterValue("cmntId"); //This store all the cmntId that are checked in the previous checkbox. 
String newComment = request.getParameterValue("newComment"); 
String userId = request.getParameterValue("userId"); //This can be in a session 

//Some function you may need 
deleteComment(cmntIds); //Execute a query to delete comment using the cmntIds 
inserNewComment(newComment, userId); //Execute a query to add the new comment using newComment and userId if there is a comment attached. 

除了这部分,我希望你能处理你需要做什么功能来做你想做的事情。

+0

这是巨大的,谢谢。我有两个问题,但我不确定如何执行特定的“for”循环。我只需要(评论cmnt:commentList){.....或我需要包括在我以前的while循环中的其他东西?另外,你是否说addDelete.jsp将是我的servlet并使用两个函数来添加/删除? – Dan 2011-05-02 13:43:36

+0

有没有办法使用与“for”语句不同的东西?我不认为我正在运行正确的Java来使用该语法.... – Dan 2011-05-02 14:52:12

+0

你也可以用'while循环'来做到这一点。我只是让一个有数据的对象在我的例子中使用。我更习惯于'for',所以我使用了这个。如果你能够运行'while',我想不需要在你的代码中添加其他东西,除非你想从Java的其他实用程序。然后你将需要添加/导入它的包。 addDelete。jsp实际上并不是一个servlet,尽管你可以使用一个servlet来转发这里的响应和请求。但是你也可以在你的servlet中使用它,就像MVC模式一样。如果您打算使用我的示例代码,您可以使用两个函数。 – ace 2011-05-03 02:14:16