2011-04-29 94 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