我想填充兩個不同的選擇下拉框和2個不同的實體對象列表。 JSON的是這樣的:使用json填充下拉列表中的嵌套java對象和列表
{
"id": 9,
"version": 0,
"emailAddress": "[email protected]",
"employee": {
"id": 5,
"version": 0,
"firstName": "Jon",
"lastName": "Snow",
"background": "King in the North",
"projectList": [
{
"id": 9,
"version": 0,
"projectName": "Ruling the North",
"clientName": null,
"fieldRate": null
},
{
"id": 10,
"version": 0,
"projectName": "Destroying the White Walkers",
"clientName": null,
"fieldRate": null
}
]
},
"addressList": [
{
"id": 11,
"version": 0,
"streetAddress": "Winterfell",
"zip": null,
"state": null,
"city": "Westeros"
},
{
"id": 12,
"version": 0,
"streetAddress": "Castle Black",
"zip": null,
"state": null,
"city": "Deep North"
}
]
}
這裏是我的JS: 所有的對象都是我創建的類的Java對象。 我的目標是用項目列表和地址列表填充2個選擇框,因爲每個聯繫人都具有特定於自己的項目。 addressList是附加到每個聯繫人的地址列表的變量,projectList是附加到每個員工的項目列表的變量,而employee是聯繫人內的嵌套對象。任何幫助將不勝感激
$.getJSON('/api/contact/', {
ajax: 'true'
}, function (data) {
//console.log(data)
$.each(data, function(index, single) {
var fullName = single.employee.firstName + " " + single.employee.lastName
$('#contact-table').find('tbody')
.append("<tr>" +
"<td>" + single.id + "</td>" +
"<td>" + single.employee.firstName + " " + single.employee.lastName + "</td>" +
"<td>" + single.emailAddress + "</td>" +
"<td>" + single.employee.background + "</td>" +
"<td>" + "<select class='form-control'><options>single.employee.projectList</options></select>" + "</td>" +
"<td>" + "<select class='form-control'><option>single.addressList</option></select>" + "</td>" +
"<td>" + "<button onclick='editEmployee(" + single.id + ")'>Edit</button>" + "</td>" +
"<td>" + "<button data-toggle='modal' data-target='#confirmDeleteModal' data-record-id='" + single.id + "'>Delete</button>" + "</td>" +
"</tr>");
});
});
這取決於休息和java在哪裏?如果你點擊''''並添加JSON作爲一個JS對象並移除getJSON,那麼你需要顯示一個[mcve]的jQuery – mplungjan