Witam
Chciałbym w swoim formularzu mieć możliwość dodania nowej kategorii. Z możliwością wyboru kategorii nadrzędnej. A dokładniej użytkownik w pieszym <select></select> wybiera kategorię główną. Na podstawie tej kategorii pobieram Ajaxem dzieci tej kategorii. Jeżeli są dzieci to AJAX tworzy poniżej kolejnego selecta w którym wybieramy dziecko.

Doszedłem do momentu w którym wysyłam dane AJAXem, a Controller zwraca mi tablice obiektów z dziećmi danej kategorii. I to działa poprawnie

I jak teraz stworzyć nowego selecta z w którym do wyboru będzie lista dzieci zwróconych AJAXem ?

CategoryController.php
  1. <?php
  2.  
  3. namespace Test\AdminBundle\Controller;
  4.  
  5. use Test\AdminBundle\Entity\Category as Category;
  6. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request as Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Serializer\Serializer;
  11. use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
  12. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  13.  
  14.  
  15. class CategoryController extends Controller
  16. {
  17. public function newAction(Request $request)
  18. {
  19. $em = $this->getDoctrine()->getManager();
  20. $categoryRepository = $em->getRepository('TestAdminBundle:Category');
  21.  
  22. $root = $categoryRepository->findOneBy(array('left'=>1));
  23.  
  24. $children = $categoryRepository->getChildren($root);
  25.  
  26. array_unshift($children,$root);
  27. $form = $this->CreateFormBuilder()
  28. ->setAction('')
  29. ->add('id', 'entity', array(
  30. 'class' => 'TestAdminBundle:Category',
  31. 'property' =>'name',
  32. 'choices' => $children
  33. ))
  34. ->add('name','text')
  35. ->getForm();
  36.  
  37. $form->handleRequest($request);
  38.  
  39. if($form->isValid()){
  40. $data = $form->getData();
  41. $newCategory = new Category();
  42. $newCategory->setName($data['name']);
  43.  
  44.  
  45. $em = $this->getDoctrine()->getManager();
  46. $categoryRepository = $em->getRepository('TestAdminBundle:Category');
  47.  
  48. $root = $categoryRepository->findOneBy(array('id'=>$data['id']));
  49.  
  50. $categoryRepository->addChild($root, $newCategory);
  51.  
  52. }
  53. return $this->render('TestAdminBundle:Category:new.html.twig',
  54. 'form' => $form->createView(),
  55. )
  56. );
  57. }
  58.  
  59. public function listAction()
  60. {
  61. return $this->render('TestAdminBundle:Category:list.html.twig');
  62. }
  63.  
  64. public function getChildrenAction()
  65. {
  66. $request = Request::createFromGlobals();
  67. $serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new
  68. JsonEncoder()));
  69. $em = $this->getDoctrine()->getManager();
  70. $categoryRepository = $em->getRepository('TestAdminBundle:Category');
  71.  
  72. $root = $categoryRepository->findOneBy(array('id'=>$request->request->get('id')));
  73.  
  74. $children = $categoryRepository->getChildren($root);
  75. $data = $serializer->serialize($children, 'json');
  76.  
  77. $response = new JsonResponse();
  78. $response->setData($data);
  79.  
  80. return $response;
  81. }
  82. }
  83.  


new.html.twig

  1. {% extends 'TestAdminBundle::base.html.twig' %}
  2. {% block body %}
  3. <div class="row">
  4. <div class="col-md-4">
  5. <ul class="breadcrumb">
  6. <li><i class="fa fa-home"></i><a href="{{ path('Admin') }}">Home</a></li>
  7. <li>Kategorie</li>
  8. <li class="active">Nowa kategoria</li>
  9. </ul>
  10. </div>
  11. <div class="col-md-8">
  12. <div class="top-content">
  13.  
  14. <ul class="list-inline mini-stat">
  15. <li>
  16. <h5>LIKES
  17. <span class="stat-value stat-color-orange"><i class="fa fa-plus-circle"></i> 81,450</span>
  18. </h5>
  19. <span id="mini-bar-chart1" class="mini-bar-chart"></span>
  20. </li>
  21. <li>
  22. <h5>SUBSCRIBERS
  23. <span class="stat-value stat-color-blue"><i class="fa fa-plus-circle"></i> 150,743</span>
  24. </h5>
  25. <span id="mini-bar-chart2" class="mini-bar-chart"></span>
  26. </li>
  27. <li>
  28. <h5>CUSTOMERS
  29. <span class="stat-value stat-color-seagreen"><i class="fa fa-plus-circle"></i> 43,748</span>
  30. </h5>
  31. <span id="mini-bar-chart3" class="mini-bar-chart"></span>
  32. </li>
  33. </ul>
  34. </div>
  35. </div>
  36. <!-- main -->
  37. <div class="content">
  38. <div class="main-header">
  39. <h2>Nowa kategoria</h2>
  40. <em>dodaj nową kategorię ogłoszeń</em>
  41. </div>
  42. <div class="main-content">
  43. <div class="row">
  44. <div class="col-md-10">
  45. <!-- BASIC VALIDATION -->
  46. <div class="widget">
  47. <div class="widget-header">
  48. <h3>Podstawowe informacje o kategorii</h3>
  49. </div>
  50. <div class="widget-content">
  51.  
  52. {{ form_start(form, {'attr':{ 'id':'basic-form', novalidate:'novalidate' }}) }}
  53. <div class="form-group">
  54.  
  55. {{ form_label(form.id, 'Wybierz kategorię nadrzędną') }}
  56. {{ form_widget(
  57. form.id,
  58. { 'attr': {'name':'id', 'class':'form-control id'} }
  59. ) }}
  60. $('.id').change(function(){
  61.  
  62. var id = $('.id').val();
  63. $.ajax({
  64. type: "POST",
  65. url: "{{ path('AjaxAdminCategoryGetChildren') }}",
  66. dataType: 'json',
  67. data: {
  68. id: id
  69. },
  70. success : function(response) {
  71. //co zrobic tu aby w HTML pojawił sie nowy select, tak abym mógl
  72. //go obsluzyc w CategoryController
  73. }
  74.  
  75. })
  76.  
  77. })
  78. </script>
  79.  
  80. </div>
  81. <div class="form-group">
  82. {{ form_label(form.name, 'Nazwa') }}
  83. {{ form_widget(
  84. form.name,
  85. {'attr':{'name':'name', 'class':'form-control', 'id':'text-input', 'placeholder':'Wpisz nazwę kategorii'}}
  86. ) }}
  87. </div>
  88. <button type="submit" class="btn btn-primary">Zapisz</button>
  89. {{ form_end(form) }}
  90. </div>
  91. </div>
  92. <!-- END BASIC VALIDATION -->
  93. </div>
  94.  
  95.  
  96. </div>
  97.  
  98. </div>
  99. </div>
  100. {% endblock %}