简体   繁体   中英

How to get data from List in JSTL with autocomplete spring mvc

I'm using JSTL and Jquery to get data into autocomplete, list of data will get from List in Spring mvc

My action:

List<Map> mapList = googleMapLocationService.getAllGoogleMapLocations();
model.put("mapList", mapList);

jquery/jsp:

<script>
  $( function() {
  var availableTags =
      <c:forEach items="${mapList}" var="map">
      [         
          "<c:out value="${map.address}"/>"         
      ];
      </c:forEach>
  $( "#tags" ).autocomplete({
    source: availableTags
  });
 } );

  <div style="width:320px ;margin-left: -38px; margin-top: -24px">
    <input id="tags" path="tags" />
  </div>

UPDATED: (try to replace 'address' is 'id')

<script>
$( function() {
var availableTags = []
    <c:forEach items="${mapList}" var="map">
        availableTags.push("<c:out value="'${map.id}', "/>");
    </c:forEach>
 $( "#tags" ).autocomplete({
   source: availableTags
 });
} );

RESULT:

在此处输入图片说明

When I type a 'keyword' any, it seem can not list on autocomplete

How can I fix the problem ? Thank so much !

This code generates the javascript array structure. Try checking this.

 <script>
  $( function() {
  var availableTags =
      <c:forEach items="${mapList}" var="map" varStatus="totalCount">
      [         
          <c:out value="'${map.address}'">
          </c:out>
          <c:if test="${totalCount.count lt fn:length(mapList)}">
          <c:out value=",">
          </c:out>
          </c:if>      
      ];
      </c:forEach>
  $( "#tags" ).autocomplete({
    source: availableTags
  });
 } );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM