问题
i'm study the web developpement and i'm actuelly stuck with a problem.
I have two select buttons in which my database is displayed, the concern being that I would like the second select button to display only the results based on the first one. A bit like if the first button select would have inside "Numbers" and "Letters" and the second in consequences would have inside "1" "2" "3" if in the first select button we choose "Numbers" and "a" "b" "c" if we choose "Letters". I currently only have the full display of my entire database in the second and it does not do the sorting. I tried a lot of thing but not realy working this is why i come here to have some help or tips.
I'm currently working with Symfony 3.4 and on ubuntu.
Here is the code for my first select button
<select id="sel1" class="formDevis" >
<option> Cliquez pour choisir </option>
{% for categorie in categories %}
<option>{{ categorie.nomCategories }}
</option>
{% endfor %}
</select>
Here is the code for my second select button
<select id="prod1" class="formDevis">
<option> Cliquez pour choisir </option>
<option>Non spécifié</option>
{% for produit in produits %}
<option>{{ produit.nomProduits }}
</option>
{% endfor %}
</select>
And here is the code i use in my controller
/**
* Lists all devis entities.
*
* @Route("/", name="admin_devis_index")
* @Method("GET")
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$devis = $em->getRepository('DevisBundle:Devis')->findAll();
$produits = $em->getRepository('DevisBundle:Produits')->findAll();
$categories = $em->getRepository('DevisBundle:Categories')->findAll();
return $this->render('categories/devis.html.twig', array(
'produits' => $produits,
'devis' => $devis,
'categories' => $categories
));
}
I tried to have on the second button select the display of my database according to the first button select but I managed to have the complete display.
回答1:
You can do this using javascript like this:
$('#select_input').change(function () {//this is your main select
var categorySelect = $(this);
$.ajax({//here you ask the backend for the dependent objects
url: Routing.generate('dependent_products', {id: categorySelect.val()}),
type: "GET",
success: function (products) {
var productsSelect = $("#products_select"); //this is your target
productsSelect.html('');
productsSelect.append('<option value> Select a product of ' + categorySelect.find("option:selected").text() + ' ...</option>');
$.each(products, function (key, product) {//here you build the dependent select
productsSelect.append('<option value="' + product.id + '">' + product.name + '</option>');
});
},
error: function (err) {
alert("An error ocurred while loading data ...");
}
});
});
Next step i configure routing and controller action
dependent_subcategories:
path: /dropdowns/{id}/subcategories
defaults: { _controller: "AppBundle:Util:returnProductsOf" }
methods: GET
options:
expose: true
The controller
public function returnProductsOfAction(Request $request, Categories $categories)
{
return new JsonResponse($this->getDoctrine()->getRepository('BackendBundle:Products')->findByCategory($categories));
}
Notice that if you are using this inside a form you need to add Form Events
Hope it helps!
来源:https://stackoverflow.com/questions/55633474/im-looking-for-help-to-display-a-part-of-my-database-according-to-my-previous-c