【发布时间】:2017-08-17 13:56:36
【问题描述】:
我正在尝试用 Rest 中的 2 个不同实体对象列表填充两个不同的选择下拉框。 Json 看起来像这样:
{
"id": 9,
"version": 0,
"emailAddress": "Jon@outlook.com",
"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 是附加到每个员工的项目列表的变量,而员工是联系人中的嵌套对象。任何帮助将不胜感激
$.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>");
});
});
【问题讨论】:
-
这取决于rest和java在哪里?如果单击
<>并将 JSON 作为 JS 对象添加并删除 getJSON,则您拥有显示 minimal reproducible example 所需的 jQuery
标签: jquery json rest drop-down-menu spring-rest