Javascript Symfony-FormType-动态选择
我有一个包含1个EntityType字段的表单,该表单必须包含第二个EntityType字段的选项,该字段未映射到第一个实体中,如下所示: ServicePlaceType.php:Javascript Symfony-FormType-动态选择,javascript,php,symfony,Javascript,Php,Symfony,我有一个包含1个EntityType字段的表单,该表单必须包含第二个EntityType字段的选项,该字段未映射到第一个实体中,如下所示: ServicePlaceType.php: public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('placetype', EntityType::class, array( "c
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('placetype', EntityType::class, array(
"class" => "AppBundle:PlaceType",
"choice_label" => "place",
"mapped" => false
))
->add('idplace', EntityType::class, array(
"class" => "AppBundle:Place",
"choice_label" => "place"
))
->add('...');
桌子
+---------+--------------+---------------+-----------+
| Service | ServicePlace | Place | PlaceType |
+---------+--------------+---------------+-----------+
| | id | | |
+---------+--------------+---------------+-----------+
| | idplace > | < id | |
+---------+--------------+---------------+-----------+
| id > | < idservice | idPlaceType > | < id |
+---------+--------------+---------------+-----------+
| service | | place | placetype |
+---------+--------------+---------------+-----------+
有人知道如何获取这些数据吗?或者如何通过另一种方式动态筛选选项
谢谢你的帮助 您可以使用更简单一点的库:
首先,我们将用于渲染位置类型id的生成器稍微更改为使用choice\u attr选项:
接下来,在模板中:
{# ... #}
{{ form(form) }}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script>
// when the 1st <select> was changed, then update the 2nd
// from current value and data-type option attribute.
$(document).on('change', '#form_placetype', function () {
var $idplace = $('#form_idplace'),
// current value
placetype = $(this).val(),
// select available options from current value
$available = $idplace.find('option[data-type="' + placetype + '"]');
// deselect when the 1st <select> has changed.
$idplace.val('');
// hide no available options from current value
$idplace.find('option').not($available).hide();
// show available options from current value
$available.show();
});
// Update 2nd <select> on page load.
$('#form_placetype').trigger('change');
</script>
请尝试使用“选择标签”=>“placeType”,谢谢您的建议。我考虑过了,但我需要标签是地名!成功了吗现在就看吧:x。伊塞鲁托的解决方案对我来说是个好办法选择是解决方案!我现在想拥抱你:
$builder
->add('placetype', EntityType::class, array(
"class" => "AppBundle:PlaceType",
"mapped" => false
))
->add('idplace', EntityType::class, array(
"class" => "AppBundle:Place",
'choice_attr' => function ($place) {
// output: <option data-type="...">...</option>
return array('data-type' => $place->getPlaceType()->getId());
},
))
{# ... #}
{{ form(form) }}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script>
// when the 1st <select> was changed, then update the 2nd
// from current value and data-type option attribute.
$(document).on('change', '#form_placetype', function () {
var $idplace = $('#form_idplace'),
// current value
placetype = $(this).val(),
// select available options from current value
$available = $idplace.find('option[data-type="' + placetype + '"]');
// deselect when the 1st <select> has changed.
$idplace.val('');
// hide no available options from current value
$idplace.find('option').not($available).hide();
// show available options from current value
$available.show();
});
// Update 2nd <select> on page load.
$('#form_placetype').trigger('change');
</script>