2016-01-03 55 views
2

我正在創建一個顯示數據庫中某些記錄的頁面(請參閱屏幕截圖)。每一行都有一個編輯按鈕,它打開一個引導模式,用於輸入註釋和改變用戶的狀態。點擊完成按鈕後,我必須使用AJAX更新數據庫中的更改。但是它沒有正常工作。我似乎無法理解發生了什麼問題。 Displaying records from database Modal for adding comments and changing status of user使用AJAX從引導模式更新數據庫

這是我的jsp頁面...

<%@page import="java.sql.ResultSet"%> 
    <%@page import="java.sql.Statement"%> 
    <%@page import="java.sql.Connection"%> 
    <%@page import="java.util.ArrayList"%> 

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
    <html lang="en"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
    <title>Insert title here</title> 
    <link rel="stylesheet" 
    href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> 
<script 
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script 
    src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> 
<script type="text/javascript" 
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> 
<% 
    String loginIdEdit = ""; 
%> 



        **<script> 
         $(function() { 
          //twitter bootstrap script 
          $("button#save").click(function(e) { 

           $.ajax({ 

            type : "POST", 
            url : "defaulterUpdater.jsp", 
            data : $('form.contact').serialize(), 
            success : function(msg) { 

            }, 
            error : function() { 
             alert("Failed"); 
            } 
           }); 
          }); 
         }); 
        </script>** 

<script type="text/javascript"> 
    function myFun() { 
     alert("welcome"); 
     var demo = document.createElement('div'); 
     demo.innerHTML = demo.innerHTML 
       + "<tr><input type='text' name='mytext'><tr>"; 
    } 
</script> 
</head> 
<body> 
    <div class="container"> 
     <form method="post" action="RegistrationServlet"> 

      <% 
       response.setHeader("Cache-Control", "no-cache"); 
       response.setHeader("Cache-Control", "no-store"); 
       response.setHeader("Pragma", "no-cache"); 
       response.setDateHeader("Expires", 0); 
       Connection con = null; 

       con = DBManager.getConnection(); 
       Statement st = con.createStatement(); 

       String userName = ""; 
       ArrayList<AutoDefaulterVO> defaulterList = null; 

       HttpSession session2 = request.getSession(); 
       if (session2.getAttribute("first_name") == null) { 
        out.println("Your session expired....."); 
      %> 



      <a href="Home.jsp">Please Login Again to continue</a> 
      <% 
       } else { 
        defaulterList = (ArrayList<AutoDefaulterVO>) session2 
          .getAttribute("autoDefaulterList"); 
        userName = session2.getAttribute("first_name").toString(); 
      %> 

      <form action='LogoutServlet' method=POST> 
       <table align="right" style="width: 100%"> 
        <tr> 
         <td align="left"><input type="submit" name="logout" 
          value="Home" onclick="form.action='AdminHome';"> 
         </td> 
         <td align="right"><input type="submit" name="logout" 
          value="Logout" onclick="form.action='LogoutServlet'"> 
         </td> 
        </tr> 
       </table> 
      </form> 



      <h3 align="center">Auto Defaults</h3> 
      <table border="1" style="width: 100%"> 
       <br> 
       <br> 


       <h3 align="center"> 
        Welcome 
        <%=userName%></h3> 

       <table border="1" style="width: 100%" class="table table-hover"> 
        <tr> 
         <th>Default Status</th> 
         <th>Borrower Name</th> 
         <th>Borrower Rating</th> 
         <th>Accural Status</th> 
         <th>Bank Number</th> 
         <th>Account Number</th> 
         <th>Days Past Due</th> 
         <th>Comments</th> 
        </tr> 

        <% 
         for (int i = 0; i < defaulterList.size(); i++) { 
           AutoDefaulterVO defaulter = new AutoDefaulterVO(); 
           defaulter = defaulterList.get(i); 
           loginIdEdit = defaulter.getDefaulterLoginID(); 
           System.out.println("Printing only auto defaulter in jsp "); 
        %> 
        <tr> 
         <%-- <td><%=defaulter.getDefaultStatus()%></td> --%> 

         <td>Auto Defaulter</td> 
         <td><%=defaulter.getBorrowerName()%></td> 
         <td><%=defaulter.getBorrowerRating()%></td> 
         <td><%=defaulter.getAccuralStatus()%></td> 
         <td><%=defaulter.getBankNumber()%></td> 
         <td><%=defaulter.getAccountNumber()%></td> 
         <td><%=defaulter.getDaysPastDue()%></td> 
         <%-- <td><%=loginIdEdit%></td> --%> 
         <td> 
          <% 
           ResultSet rs = st 
               .executeQuery("select * from aip_comments"); 
             while (rs.next()) { 
              System.out.println("Auto defaulter loginId printing-->" 
                + defaulter.getDefaulterLoginID()); 
              String loginId = rs.getString("login_id"); 
              System.out.println("databse loginId printing-->" 
                + rs.getString("login_id")); 
              if (defaulter.getDefaulterLoginID().equals(
                rs.getString("login_id"))) { 
          %> <%=rs.getString("comments")%> <% 
    } 
      } 
%> 
         </td> 
         <td> 
          <!-- <form name="editForm" action="edit.jsp"> 
          <button type="button" data-toggle="modal" class="btn btn-default" data-target="#myModal">Edit</button> 

          --> 
          <form class="contact"> 


           <button href="#edit-modal" type="button" data-toggle="modal" 
            class="btn btn-default" data-target="#<%=loginIdEdit %>">Edit</button> 
           <!-- <a data-toggle="modal" data-target="#<%=loginIdEdit%>" href="#">Edit</a> --> 

           <!-- <input type="submit" name="editButton" value="Edit"> --> 
           <!--<input type="hidden" id="edit" name="edit" value="<%=loginIdEdit%>" />--> 
         </td> 

         <div class="modal fade" id="<%=loginIdEdit%>" role="dialog"> 
          <div class="modal-dialog"> 
           <div class="modal-content"> 
            <div class="modal-header"> 
             <button type="button" class="close" data-dismiss="modal">&times;</button> 
             <h4 class="modal-title"> 
              Edit status and add comment for 
              <%=defaulter.getBorrowerName()%></h4> 
            </div> 

            <div class="modal-body"> 


             <div class="form-group"> 
              <label for="sel1"><span></span>Select status:</label> <select 
               class="form-control" id="sel1" name="sel1"> 
               <option>Validate Error</option> 
               <option>Auto Weaver</option> 
               <option>ReDefault</option> 
              </select> 

             </div> 
             <div class="form-group"> 
              <label for="defaultercomment"><span></span>Add 
               comment:</label> <input type="text" class="form-control" 
               name="defaultercomment" id="defaultercomment"<%-- value="<%=loginIdEdit%>" --%>> 
             </div> 
            </div> 
            <div class="modal-footer"> 
             <button type="button" id="save" name="save" 
              class="btn btn-default" data-dismiss="modal" align="centre">Done</button> 
            </div> 

           </div> 
          </div> 
         </div> 
         </form> 
         <% 
          } 
         %> 
        </tr> 

       </table> 




       <% 
        } 
       %> 

</body> 
</html> 

AJAX URL重定向到下面的jsp頁面,defaulterUpdater.jsp在這裏我只打印值,看看它們是否正確檢索。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<% String selection = request.getParameter("sel1"); 
String comment = request.getParameter("defaultercomment"); 

System.out.println("selection-"+selection+"comment--"+comment); 
%> 


</body> 
</html> 

的值被接受爲僅正在被打印空值的行的第一row..For休息。任何幫助將不勝感激。謝謝!

+0

您的點擊事件觸發btnSave點擊模式? –

+0

是的,它被觸發了......正確的值僅通過ajax傳遞給第一行。對於其他行,傳遞空值。 –

回答

0

我得到的代碼工作。基本上需要一個「完成」按鈕的唯一標識符,這是javascript的規則。因此它不適用於所有行。 我給唯一的ID的形式 並提交所謂的onclick方法.. 的onClick =「callAjax(this.form)」

所以現在它的工作。感謝大家的幫助!

+0

太好了,看起來你已經解決了它,但是你不認爲你會爲每個編輯按鈕重新使用你的表單,如果有100行,那麼將會有100個表單,這可能會導致渲染性能下降。 –

+0

是的,這是真的..但我迫切需要一個解決方案。必須考慮更好的東西。 –

0

您正在將表格中的模態錯誤。通過表格語法是錯誤的。您必須將該模式從表格中移出並在引導程序的shown.bs.modal事件中調用ajax。

由於您需要重複按鈕。保持打開模式的按鈕的硬連線ID,同時保留一個data-attribute-id來唯一標識點擊的按鈕,這將幫助你獲得有關在列表中點擊哪個按鈕的詳細信息(當按鈕對每一行重複時,你的data-attribute-id將是該行的loginid)。

然後寫的jQuery與事件shown.bs.modal,像

$("#modalId").on("shown.bs.modal", function(e){ 
var btnClicked= $(e).relatedTarget(); 
var btnClickedId= btnClicked.data("attribute-id"); // id of clicked button 

// here more code see attached link below for more 

} 

我已經在asp.net mvc的貼similer答案以前,但你將有想法的情況是similer給你。 請訪問我的問題的答案dynamically created bootstrap3 modals do not work

希望這將有助於想法解決這個問題。

@Update科刪除您的一些代碼清潔視圖

<script type="text/javascript"> 

    $(document).ready(function() { 
     $('#editModal').on('show.bs.modal', function (event) { 
      var button = $(event.relatedTarget);//Button which is clicked 
      var clickedButtonId= button.data('loginidedit');//Get loginidedit of the button or get other variable same 
      $("#loginid").val(clickedButtonId);//Put here id of the btn clicked which has attribute for loginidedit 
    //you can get here other required filed or make ajax call to get other values formt he database for     this loginidedit. 

     }) 
    }); 
</script> 



<body> 
    <div class="container"> 

//Table here 
      <table border="1" style="width: 100%" class="table table-hover"> 
        <% 
         for (int i = 0; i < defaulterList.size(); i++) { 
           AutoDefaulterVO defaulter = new AutoDefaulterVO(); 
           defaulter = defaulterList.get(i); 
           loginIdEdit = defaulter.getDefaulterLoginID(); 
           System.out.println("Printing only auto defaulter in jsp "); 
        %> 
        <tr> 
         <td> 
           <button href="#edit-modal" type="button" data-toggle="modal" 
            class="btn btn-default" data-target="#editModal" data-loginidedit = "<%=loginIdEdit %>">Edit</button> 
         </td> 
        </tr> 
         <% 
          } 
         %> 
       </table> 



//Modal here 
         <div class="modal fade" id="editModal" role="dialog"> 
          <div class="modal-dialog"> 
           <div class="modal-content"> 
            <div class="modal-header">          
            </div> 
           <form class="contact"> //give action, method attribute here or make ajax call on save button clicked, 

          <input type="hidden" name="????" id="loginid">// the modal on shown will fill this value. 

            <div class="modal-body"> 
             <div class="form-group"> 
             <label for="sel1"><span></span>Select status:</label> <select 
              class="form-control" id="sel1" name="sel1"> 
              <option>Validate Error</option> 
              <option>Auto Weaver</option> 
              <option>ReDefault</option> 
             </select> 

            </div> 
            <div class="form-group"> 
             <label for="defaultercomment"><span></span>Add 
              comment:</label> <input type="text" class="form-control" 
              name="defaultercomment" id="defaultercomment"<%-- value="<%=loginIdEdit%>" --%>> 
            </div> 
            </div> 
            <div class="modal-footer"> 
             <button type="button" id="save" name="save" 
              class="btn btn-default" data-dismiss="modal" align="centre">Done</button> 
            </div> 
           </form> 
           </div> 
          </div> 
         </div> 

</body> 
</html> 



      $(function() { 
          //twitter bootstrap script 
          $("button#save").click(function(e) { 

           $.ajax({ 

            type : "POST", 
            url : "defaulterUpdater.jsp", 
            data : $('form.contact').serialize(), 
            success : function(msg) { 

            }, 
            error : function() { 
             alert("Failed"); 
            } 
           }); 
          }); 
         }); 
+0

好的。謝謝....會嘗試你的解決方案! –

+0

阿尼爾可以請你詳細說明一下嗎? –