我正在寻找帮助,以根据以前在选择按钮中所做的选择显示数据库的一部分 [英] I'm looking for help to display a part of my database according to my previous choices in a select button

查看:65
本文介绍了我正在寻找帮助,以根据以前在选择按钮中所做的选择显示数据库的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究网络开发,并且确实遇到了问题。

i'm study the web developpement and i'm actuelly stuck with a problem.

我有两个选择按钮,用于显示我的数据库,我希望第二个选择按钮仅显示基于第一个选择的结果。有点像如果第一个选择按钮在 Numbers和 Letters内部,而第二个选择在 1, 2, 3内部,如果在第一个选择按钮中选择 Numbers和 a如果选择字母,则为 b, c。我目前仅在第二个数据库中完整显示整个数据库,并且不进行排序。我尝试了很多事情,但并没有真正工作,这就是为什么我来这里寻求帮助或提示。

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.

我目前正在使用Symfony 3.4和ubuntu进行开发。

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.

推荐答案

您可以使用类似的javascript来做到这一点:

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

控制器

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

希望有帮助!

这篇关于我正在寻找帮助,以根据以前在选择按钮中所做的选择显示数据库的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆