簡體   English   中英

如何從DOM到javascript獲取數組列表?

[英]How can I obtain an array list from DOM into javascript?

我不知道如何獲取來自Spring Controller的Objects arrayList。

這是我的控制器,它正在返回對象的數組列表

控制者

@RequestMapping(value = "hello", method = RequestMethod.GET) public ModelAndView showMessage(Principal p,HttpServletResponse response) { ModelAndView mv = new ModelAndView("mapa"); response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); mv.addObject("postossaude", Lists.newArrayList(PostosSaude.findAll())); return mv; }

這是Postosaude.java對象,其中包含我需要救援的所有信息。

@Entity
public class Postosaude {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String nome_posto_saude;
private double latitude;
private double longitude;
private String endereco;

public Postosaude()
{}
//gets and sets...
}

`

這是我的mapa.html,它正在接收列表(我使用Thymeleaf獲取列表)。

mapa.html

  <!DOCTYPE html> <html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> var NodeListPostosSaude = document.getElementsByName('postossaude'); // ?? I want to obtain the "postossaude" list alert(NodeListPostosSaude.length); </script> </head> <body> <p th:text="${#vars.get('postossaude')}"> </p> //I check that the list is arriving OK (it prints the three objects that are in my list: //"[palmaslab.mapas.repository.Postosaude@e7228c7a, palmaslab.mapas.repository.Postosaude@478808dc, palmaslab.mapas.repository.Postosaude@f35bea7d]" </body> </html> 

因此,我想將此列表獲取到我的腳本中,但我不知道該怎么做。 您知道任何JavaScript方法或jQuery方法來獲取該方法嗎?

我的目標是,使用此數組“對象”列表(其中的另一個是帶有地理坐標的對象)來制作一個像這樣自定義的googlemaps

根據評論中的討論,這是我的解決方案-JSBin

的HTML

<div id="container">
  <!-- P tags generated -->
  <p>Data Value 1</p>
  <p>Data Value 2</p>
  <p>Data Value 3</p>
  <p>Data Value 4</p>
</div>


JS

var container = document.getElementById('container');
var p = container.getElementsByTagName('p');

var array = [];

for (i = 0; i < p.length; i++) {
    array.push(p[i].innerHTML)
}

console.log(array)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM