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

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

问题描述

我正在研究网络开发,但我确实遇到了一个问题.

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

我有两个选择按钮,其中显示了我的数据库,问题是我希望第二个选择按钮仅显示基于第一个的结果.有点像如果第一个按钮选择将在数字"和字母"内,第二个在结果中将在1"2"3"内,如果在第一个选择按钮中我们选择数字"和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天全站免登陆