2017-10-11 32 views
0

我正在使用這個website的jquery腳本。使用spring MVC添加動態數據到可搜索的多選jquery MVC

控制器春:

@RequestMapping(value="/method2/submit",method = RequestMethod.POST) 
    public String method3(ModelMap model, @ModelAttribute("data") Data data) { 
     model.addAttribute("data",new Data()); 
     ArrayList<String> array = new ArrayList<String>(); 
     array.add("Variable 1-Definition 1"); 
     array.add("variable 2 - Definition 2"); 
     array.add("variable 3-Definition 3"); 
     model.addAttribute("variableList", array); 
     System.out.print(array); 
     return VIEWS_LOCATION +"/secondPage"; 
    } 
  • JSP頁面:

<script> 
 
var abc = $('.demo').dropdown({ 
 
\t multipleMode : 'label', 
 
\t 
 
}); 
 
console.log("doc load"+abc); 
 

 
</script>
<div class="demo" id="dropdown1"> 
 
<form:select style="display: none" name="" id="values" path="name" multiple="true"> 
 
\t <form:option value="variable11" selected="true">variable1</form:option> 
 
\t <form:option value="variable2" >variable2</form:option> 
 
\t <form:option value="variable3" >variable3</form:option> 
 
\t <form:options items="${variableList}" /> \t \t \t 
 
</form:select> 
 
</div>
該網頁附在下面。所選選項 列表顯示在搜索框

As you can see Variable1-definition1 is a dynamic value, which when clicked cannot be seen in the search box, but it is already selected.

動態數據可以包含高達500個條目將被從文件中讀取。添加3個變量僅用於表示目的。請幫助我將動態數據添加到下拉列表中。謝謝!

+0

參考解決方案以及https://www.mkyong.com/spring-mvc/spring-mvc-dropdown-box-example/ –

回答

0

我找到了答案!

問題與早期的方法是,JS無法找到確切的元素被選中(我不知道,它如何能夠改變所選元素的CSS和搜索結果工作正常)。

因此,我對代碼進行了一些更改,現在工作正常。

控制器:不是發送一個數組,而是傳遞一個hashmap,這使得索引更容易。

@RequestMapping(value="/method2/submit",method = RequestMethod.POST) 
    public String method3(ModelMap model, @ModelAttribute("data") Data data) { 
     model.addAttribute("name",data.getName()); 
     model.addAttribute("data",new Data()); 
     HashMap<String,String> map = new HashMap<String,String>(); 
     map.put("1","Variable 1-Definition 1"); 
     map.put("2","variable 2 - Definition 2"); 
     map.put("3","variable 3-Definition 3"); 
     model.addAttribute("variableList", map); 
     System.out.println(map); 
     return VIEWS_LOCATION +"/secondPage"; 
    } 

<c:forEach var="item" items="${variableList}"> 
 
    <option value="${item.key}">${item.value}</option> 
 
</c:forEach>

另外,在JSP值作爲唯一的號碼這使得JS以識別從下拉選擇的準確元件傳遞。 現在,輸出看起來像, output

0

試試下一個,這應該適合你。

<form:select name="" id="values" path="name" multiple="true" items="${variableList}" />