2017-04-08 75 views
0

我是新的Ajax,我如何用新的ArrayList替換$ {phList},以便Ajax可以幫助我更新內容而不刷新整個頁面。Ajax替換新的ArrayList沒有刷新頁面

ajax將觸發控制器檢索數據並存儲在arraylist中,然後將arraylist傳遞給ajax。 Ajax將更新jsp中的內容。如果從數據庫中檢索到記錄,下一步forEach循環將生成具有值的列和複選框。

任何解決方案,並希望此解決方案可以幫助他人。謝謝!

myJsp.jsp

<select id="selected_year" name="selected_year" class="form-control"> 
    <c:forEach var="line" items="${yearlist}"> 
     <c:choose> 
      <c:when test="${refreshInd ge 0 and line eq refreshInd}"> 
       <option selected="selected"><c:out value="${line}" /> 
       </option> 
      </c:when> 
      <c:otherwise> 
       <option><c:out value="${line}" /></option> 
      </c:otherwise> 
     </c:choose> 

    </c:forEach> 
</select> 

<c:forEach var="ph" items="${phList}" varStatus="PHstatus"> 
     <div class="row"> 
     <div class="col-md-4"> 
      <form:input path="phList[${PHstatus.index}].holidayDesc" class="form-control col-md-5" value="${ph.holidayDesc}" /> 
     </div> 

     <div class="col-md-3"> 
      <form:input path="phList[${PHstatus.index}].startDate" class="form-control" value="${ph.startDate}" id="calendar1" placeholder="dd/mm/yyyy" onkeypress="return noAlphabet(event)" /> 
     </div> 

     <div class="col-md-3"> 
      <form:input path="phList[${PHstatus.index}].endDate" class="form-control" value="${ph.endDate}" id="calendar2" placeholder="dd/mm/yyyy" onkeypress="return noAlphabet(event)" /> 
     </div> 

     <div class="col-md-2"> 
      <form:checkbox path="phList[${PHstatus.index}].checkboxDel" value="" class="cbposition" /> 
     </div> 
     </div> 
     <br> 
    </c:forEach> 

後我改變selected_year觸發AJAX,它的工作很好,但:成功的函數(響應)不能工作。我想刪除現有的$ {phList}並用新的ArrayList更新,在上面的jsp中替換$ {phList} show。

myJavascript.js

$(function($) { 
    $("#selected_year").change(function(){ 
     var selectedText = $(this).find("option:selected").text(); 
     var $form = $(this); 
     var action = $form.find('.send').val(); 

     var list[]; 
     var array[]; 

     $.ajax("holiday/pubholiday.json?" , { 
      method: "GET", 
      accepts: "application/json", 
      dataType: "json", 
      data: $form.serialize() + '&selectedText=' + selectedText, 
      success: function(response) { 
       $("#phList").remove() 
       $(JSON.stringify(response)) 
    //how to pass the response which is my new arraylist to replace the ${phList} 
       }, 
     }).done(function(data) { 
      console.log(data) 
      alert("Data Sent" + selectedText) 
     }) 
     .fail(function(jqXHR, textStatus, errorThrown) { 
      var errorMessage = ""; 
      if (jqXHR.status == 401) { 
       errorMessage = "Your session has expired. Please <a href=\"<spring:url value='/login'/>\">login</a> again."; 
      }else if(jqXHR.status == 500){ 
       console.log(jqXHR.status); 
        console.log(jqXHR.responseText); 
        console.log(thrownError); 
      }else { 
       try { 
        var errorJson = JSON.parse(jqXHR.responseText); 
        errorMessage = errorJson.error; 

       } catch (e) { 
       errorMessage = errorThrown || textStatus; 
      } 
      } 
      }); 
    }) 
}); 

在數組列表這是對象模型存儲。每條記錄都將包含對象模型數據。

model.java

public class PubHoliday { 
    private int year; 
    private String holidayID; 
    private String holidayDesc; 
    private String startDate; 
    private String endDate; 
    private boolean checkboxDel; 
    private String selected_year; 
    private int refreshInd; 

    public int getYear() { 
     return year; 
    } 
    public void setYear(int year) { 
     this.year = year; 
    } 
    public String getHolidayID() { 
     return holidayID; 
    } 
    public void setHolidayID(String holidayID) { 
     this.holidayID = holidayID; 
    } 
    public String getHolidayDesc() { 
     return holidayDesc; 
    } 
    public void setHolidayDesc(String holidayDesc) { 
     this.holidayDesc = holidayDesc; 
    } 
    public String getStartDate() { 
     return startDate; 
    } 
    public String getEndDate() { 
     return endDate; 
    } 
    public void setStartDate(String startDate) { 
     this.startDate = startDate; 
    } 
    public void setEndDate(String endDate) { 
     this.endDate = endDate; 
    } 
    public boolean getCheckboxDel() { 
     return checkboxDel; 
    } 
    public void setCheckboxDel(boolean checkboxDel) { 
     this.checkboxDel = checkboxDel; 
    } 
    public String getSelected_year() { 
     return selected_year; 
    } 
    public void setSelected_year(String selected_year) { 
     this.selected_year = selected_year; 
    } 
    public int getRefreshInd() { 
     return refreshInd; 
    } 
    public void setRefreshInd(int refreshInd) { 
     this.refreshInd = refreshInd; 
    } 
} 
+0

請更具體地說明問題。你提供了相當多的代碼,但沒有提及什麼可行或不需要改變 – charlietfl

回答

0

首先,你需要用DIV ID,例如爲包裝<c:forEach var="ph" items="${phList}" varStatus="PHstatus">

<div id="list_holidays"> 
<c:forEach var="ph" items="${phList}" varStatus="PHstatus"> 
    ....... 
</c:forEach> 
</div> 

然後在成功的Ajax代碼 - 空div#list_holidays - 獲取結果,每行創建新的div,追加到div#list_holidays

success: function(response) { 
    var wrapper = $('#list_holidays'); 
    wrapper.html(''); 
    //->foreach response result 
    // -> $('div').html(your template).appendTo(wrapper) 
} 
+0

它工作的一半。在我嘗試包裝後,它覆蓋了我的整個網頁,並且循環模板是重複的。 –

0

JSP是服務器端渲染。這意味着,當接收到請求時,服務器使用變量${phList}來呈現動態HTML頁面並將該頁面發送給瀏覽器。 Ajax是您的JavaScript坐在瀏覽器中提出的請求。因此,瀏覽器無法直接瞭解或更改變量$(phList}並影響模板。

但是,你所說的可以實現。在下列方式之一

方法1(簡單)

實現holiday/pubholiday.asp與僅此而不是JSON響應。

<c:forEach var="ph" items="${phList}" varStatus="PHstatus"> 
    <div class="row"> 
    <div class="col-md-4"> 
     <form:input path="phList[${PHstatus.index}].holidayDesc" class="form-control col-md-5" value="${ph.holidayDesc}" /> 
    </div> 
    ... 
    <br> 
</c:forEach> 

添加一個容器元素的數組項是這樣的

<div id="container" > 
    <c:forEach var="ph" items="${phList}" varStatus="PHstatus"> 
    ... 
    </c:forEach> 
</div> 

更改主頁您的Ajax請求到這一點。當您收到pubholiday.jsp的響應時,請將內容#container替換爲從ajax收到的內容。

... 
$.ajax("holiday/pubholiday.asp?" , { 
     method: "GET", 
     accepts: "application/html", 
     dataType: "json", 
     data: $form.serialize() + '&selectedText=' + selectedText, 
     success: function(response) { 
      // This is where magic happens 
      $("#container").html(response); 
     }, 
    }).done(function(data) { 
     console.log(data) 
     alert("Data Sent" + selectedText) 
    }) 
... 

方法2

如果需要的響應是隻JSON,在success callback閱讀JSON,併產生通過連接字符串html的自己,然後更換#container元素。

方法3(推薦)

用戶一些客戶端呈現庫狀聚合物或把手來呈現JSON成html。

+0

舊陣列列表$ {phList}仍然存在,不能由我的新陣列列表替換。 –

+0

你遵循哪種方法。你能解釋更多關於你嘗試過的東西嗎,所以我可以幫忙 –