2016-02-27 69 views
-1

我真的需要幫助,我想從我的數據庫中刪除一行,但每次單擊刪除按鈕時,它只會刪除表中的第一個元素。在點擊「#myModal」(風將變成apear)的鏈接後,變量p.getIdUser()將包含第一個元素的id,而不是選定的元素,而我把這個塊放到「foreach」以避免丟失p.getIdUser()。這是我的代碼。使用jsp與確認風刪除數據庫中的一行

file.jsp

<c:forEach items="${liste}" var="p" > 
        <tr> 
         <td>${p.getIdUser()}</td> 
         <td>${p.getNom()}</td> 
         <td>${p.getPrenom()}</td> 
         <td>${p.getCin()}</td> 
         <td>${p.getPassword()}</td> 
         <td>${p.getDateEnt()}</td> 
         <td>${p.getGrade()}</td> 
         <td>${p.getEmail()}</td> 
         <td>${p.getTel()}</td> 
         <td>${p.getAdr()}</td> 
         <td>${p.getRole()}</td> 
         <td>${p.getCodeSite()}</td> 
         <td> <a href="user.html"><i class="fa fa-pencil"></i></a> 
          <a href="#myModal" role="button" data-toggle="modal">${p.getIdUser()}<i class="fa fa-trash-o"></i></a></td>       
        </tr> 

        <div class="modal small fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
         <div class="modal-dialog"> 
          <div class="modal-content"> 
              <div class="modal-header"> 
              <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> 
              <h3 id="myModalLabel">Delete Confirmation</h3> 
              </div> 
           <div class="modal-body"> 
            <p class="error-text"><i class="fa fa-warning modal-icon"></i>Are you sure you want to delete the user?<br>This cannot be undone.</p> 
           </div> 
           <div class="modal-footer"> 
            <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">Cancel</button> 
            <a href='DeleteUser?id=${p.getIdUser()}'> <button class="btn btn-danger">Delete</button></a> 
           </div> 
           </div> 
         </div> 
        </div> 
</c:forEach>   

回答

0

使用模式彈出了一旁的for循環,因爲你是在循環使用模式彈出所以沒有創建模式,彈出窗口的名稱相同,或當你點擊做你的任務你最後一個模型彈出打開---

這樣做.........從Shouting Code找到解決方案 調用Js函數(帶參數userId)來設置模型userId在model-彈出 解決方案:

<c:forEach items="${emailBatchList}" var="emailBatchVar" 
    varStatus="statusVar"> 
    <pg:item> 
     <tr> 
     <td>${emailBatchVar.date }</td> 
     <td>${emailBatchVar.campaignType }</td> 
     <td>${emailBatchVar.campaignName }</td> 
     <td>${emailBatchVar.noOfEmailsSent }</td> 
     <td>${emailBatchVar.deliveryStatus }</td> 
     <td>${emailBatchVar.successfullyDelivered }</td> 
     <td>${emailBatchVar.sentBy }</td> 
     <td>${emailBatchVar.timeOfDispatch }</td> 
     <td> 
      <div class="deliveryDetail${statusVar.count}" 
       hidden="hidden">${emailBatchVar.deliveryDetails}</div> 
      <a data-toggle="modal" data-target="#viewDetails" 
       onclick="campaignReport(${statusVar.count});"> <i 
       class="fa fa-area-chart fa-lg btn-view"></i> 
      </a> 
     </td> 
     </tr> 
    </pg:item> 
</c:forEach> 


<!-- Modal PopUp Div For Delivery Details--> 
<div id="viewDetails" class="modal fade adjust-modal" 
    role="dialog"> 
    <div class="modal-dialog"> 
     <!-- Modal content--> 
     <div class="modal-content"> 
     <div class="modal-header"> 
      <button type="button" class="close" data-dismiss="modal" 
       aria-label="Close"> 
      <span aria-hidden="true">×</span> 
      </button> 
      <h4 class="modal-title" id="myModalLabel">Delivery 
       Details 
      </h4> 
     </div> 
     <div class="modal-body"> 
      <!-- Content start --> 
      <div class="tab-content emailReport"> 
       <!-- content will be added dynamicaly --> 
      </div> 
      <!-- Content end --> 
     </div> 
     <div class="modal-footer"> 
      <button type="button" class="btn btn-default" 
       data-dismiss="modal">Close</button> 
     </div> 
     </div> 
    </div> 
</div> 
<!-- Modal Popup Div (view-details) end --> 

JS

function campaignReport(index) { 
    $('.tab-content').empty(); 
    var deliveryDetail = $.parseJSON($('.deliveryDetail'+index).text()); 
    var tabContent = ''; 
    if(deliveryDetail != null && deliveryDetail != '') 
    { 
     tabContent = '<div class="row"><div class="col-sm-6"><div class="table-responsive"><table class="table">' 
      +'<tr><td>Requested Mails:</td><td>'+deliveryDetail['requests'] 
     +'</td></tr><tr><td>Deliverd Mails:</td><td>'+deliveryDetail['delivered']+'</td></tr><tr><td>' 
     +'Blocked Mails:</td><td>'+deliveryDetail['blocks']+'</td></tr><tr><td>' 
     +'Bounced Mails:</td><td>'+(deliveryDetail['bounces']+deliveryDetail['bounce_drops']) 
     +'</td></tr><tr><td>' 
     +'Spam Mails:</td><td>'+(deliveryDetail['spam_reports']+deliveryDetail['spam_report_drops']) 
     +'</td></tr><tr><td>' 
     +'Invalid Mails:</td><td>'+deliveryDetail['invalid_emails']+'</td></tr>' 
     +'</table></div></div></div>'; 
     $('.tab-content.emailReport').append(tabContent); 
    } 
}