我有一个对象数组,这些对象也有一个对象数组。我正在使用ajax请求获取它并希望使用该JSON数据进行选择。无法解决这个问题)我已经尝试了很多例子。
Object service_problems:
Array[3]
0: Object
id: 2
name: "bla bla"
problems: Array[2]
0: Object
id: 3,
name: 'blab bla'
1: Object
id: 4,
name: 'bla blae'
根据这些数据,我想用optgroup和选项进行选择:
<select>
<optgroup label="service_problems.name">
<option value="3">blab bla</option>
<option value="4">bla blae</option>
</optgroup>
</select>
我的PHP代码:
if ($request->ajax()) {
$services = DeviceType::find($request->type_id);
$services = $services->services;
$service_with_problems = [];
function findServiceProblems($id) {
$service = Service::find($id);
return $service->serviceProblems;
}
foreach ($services as $service) {
$service_problems = findServiceProblems($service->id);
array_push($service_with_problems, [
'id' => $service->id,
'name' => $service->name,
'service_problems' => $service_problems
]);
}
return response()->json(['service_problems' => $service_with_problems]);
}
帮助pls)
答案 0 :(得分:0)
解决了问题)
jQuery.map(data['service_problems'], function (problem) {
options += '<optgroup label="'+ problem.name +'">';
jQuery.map(problem.problems, function (data) {
options += '<option>'+ data.name +'</option>';
});
options += '</optgroup>';
});