使用 ajax symfony 更新 select2
Update select2 with ajax symfony
我制作了一个创建新 select 选项的表单,现在我想用 ajax 更新 select 输入类型,这样我就不必重新加载整个页面。
我试过的方法是这样的:
控制器returns这个
return new JsonResponse(['data'=>$em->getRepository('AppBundle:ClasificacionAO')->findAll()]);
而 Ajax 是
$.ajax({
type: $(this).attr('method'),
url: $(this).attr('action'),
data: $(this).serialize(),
success: function(data) {
// optionally check if the response is what you wanted
//if (data.response == 'deleted') {
//
$('select').select2({
data: data
});
//also tried with the id: paciente_form_clasificacionAO
//$('paciente_form_clasificacionAO').select2({
// data: data
// });
//}
}
})
select 选项只有在我刷新整个页面后才会更新,如何修复它以显示创建的新 select 选项?
我设法解决了这个问题。问题是控制器没有以有效的方式传递数组。之后就是用javascript.
里面的数据了
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('AppBundle:ClasificacionAO')->findAll();
foreach ($entities as $entity) {
$response1[] = array(
'key' => $entity->getIdentificadorAO(),
// other fields
);
$response2[] = array(
'value' => $entity->getId(),
// other fields
);
}
return new JsonResponse(([$response1,$response2]));
Ajax
$.ajax({
type: $(this).attr('method'),
url: $(this).attr('action'),
data: $(this).serialize(),
success: function(data) {
dataOptions = data[0];
dataIds = data[1];
// Get the raw DOM object for the select box
select = document.getElementById('paciente_form_clasificacionAO');
// Clear the old options
select.options.length = 0;
// Load the new options
// Or whatever source information you're working with
for (var index = 0; index < dataOptions.length; index++) {
option = dataOptions[index];
opt1 = document.createElement("option");
opt1.text = option['key'];
opt1.value = dataIds[index]['value'];
//console.log(opt1);
select.options.add(opt1);
}
}
})
我制作了一个创建新 select 选项的表单,现在我想用 ajax 更新 select 输入类型,这样我就不必重新加载整个页面。
我试过的方法是这样的:
控制器returns这个
return new JsonResponse(['data'=>$em->getRepository('AppBundle:ClasificacionAO')->findAll()]);
而 Ajax 是
$.ajax({
type: $(this).attr('method'),
url: $(this).attr('action'),
data: $(this).serialize(),
success: function(data) {
// optionally check if the response is what you wanted
//if (data.response == 'deleted') {
//
$('select').select2({
data: data
});
//also tried with the id: paciente_form_clasificacionAO
//$('paciente_form_clasificacionAO').select2({
// data: data
// });
//}
}
})
select 选项只有在我刷新整个页面后才会更新,如何修复它以显示创建的新 select 选项?
我设法解决了这个问题。问题是控制器没有以有效的方式传递数组。之后就是用javascript.
里面的数据了 $em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('AppBundle:ClasificacionAO')->findAll();
foreach ($entities as $entity) {
$response1[] = array(
'key' => $entity->getIdentificadorAO(),
// other fields
);
$response2[] = array(
'value' => $entity->getId(),
// other fields
);
}
return new JsonResponse(([$response1,$response2]));
Ajax
$.ajax({
type: $(this).attr('method'),
url: $(this).attr('action'),
data: $(this).serialize(),
success: function(data) {
dataOptions = data[0];
dataIds = data[1];
// Get the raw DOM object for the select box
select = document.getElementById('paciente_form_clasificacionAO');
// Clear the old options
select.options.length = 0;
// Load the new options
// Or whatever source information you're working with
for (var index = 0; index < dataOptions.length; index++) {
option = dataOptions[index];
opt1 = document.createElement("option");
opt1.text = option['key'];
opt1.value = dataIds[index]['value'];
//console.log(opt1);
select.options.add(opt1);
}
}
})