【发布时间】:2022-01-04 11:14:03
【问题描述】:
我有 2 个下拉选项:
- 第一个服务类型
- 第二个服务 类型是服务的一个属性,服务与解决方案有单对多关系 当您选择一个类型时,第二个选择被填充(Ajax)与选择的类型相关的服务。然后提交按钮将显示所选服务的解决方案(此“提交部分尚未实现”) 我按照symfony doc tutorial 执行此操作,但是当我在第一次选择中选择类型时,我的第二个下拉菜单消失了
这里是 mySolutionsChooseType.php:
<?php
namespace App\Form;
use App\Entity\Service;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class SolutionsChooseType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$types = [
"Conception et développement d'applications" => "Conception et développement d'applications",
"Conseil et prestations en architecture des infrastructures et systèmes d'informations" =>
"Conseil et prestations en architecture des infrastructures et systèmes d'informations",
"Fourniture et maintenance d'applications bureautiques" => "Fourniture et maintenance d'applications bureautiques",
"Hébergement, maintenance et diffusion d'applications métiers" => "Hébergement, maintenance et diffusion d'applications métiers",
"Sécurité informatique" => "Sécurité informatique",
"Services informatiques" => "Services informatiques",
"Services collaboratifs" => "Services collaboratifs",
"Services de communications" => "Services de communications",
"Stockage centralisé et sécurisé de données" => "Stockage centralisé et sécurisé de données",
"Support transversal" => "Support transversal",
];
$builder
->add('type', ChoiceType::class, [
'placeholder' => 'Choisissez un type de service',
'choices' => $types,
]);
};
}
如果有人可以提供帮助。提前致谢。
我的树枝视图,里面有 js 代码,供参考:
{% extends 'base.html.twig' %}
{% block title %}
Liste des Solutions par Service (et par Type)
{% endblock %}
{% block body %}
<div class="text-center">
<h1>Liste des Solutions</h1>
</div>
<div class="container">
<div class="row">
<h2>Voici la liste des solutions, groupées par Type de Service:</h2>
</div>
<div class="row">
<div class="col-lg">
{{ form_start(form, {'attr': {'id': 'solutions_choose'} }) }}
{{ form_end(form) }}
</div>
<div class="col-md-3">
<div class="input-group">
<div class="input-group-append">
Ajouter une solution
<a href="{{ path('solution_create') }}">
<button class="btn btn-outline-primary" type="submit">
<i class="fa fa-plus"></i></button>
</a>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block javascripts %}
{{ parent() }}
<script>
$(document).ready(function() {
var type = $('#solutions_choose_type');
type.change(function () {
var data = $('#solutions_choose_type option:selected').val();
$.ajax({
url: '/solution/ajax',
type: 'POST',
//type: form.attr('method'),
data: {"type": data},
success: function (html) {
// Replace current position field ...
$('#solutions_choose_services').replaceWith(
// ... with the returned one from the AJAX response.
$(html.responseText).find('#solutions_choose_services')
// Position field now displays the appropriate positions.
)
}
});
//alert (form.attr('action'));
console.log(data);
//console.log(form);
});
});
</script>
{% endblock %}
选择的类型被变量数据正确传递(我可以通过 console.log(data); 看到)
我的解决方案控制器.php:
class SolutionController extends AbstractController
{
/**
* @Route("/ajax", name="ajax")
*
*/
public function UpdateSolutions(ServiceRepository $servrepo, Request $request): Response
{
$form = $this->createForm(SolutionsChooseType::class);
$type = $request->request->get('type');
// dd($type);
$services = $servrepo->findBy([
'Type' => $type,
]);
$form->add('services', ChoiceType::class, [
'placeholder' => 'Choisir un service',
'choices' => $services,
]);
return $this->renderForm("solution/SolutionsAll.html.twig", [
'form' => $form,
]);
}
【问题讨论】:
-
line 33: function (FormEvent $event, EntityManagerInterface $entityManager)函数名在哪里????