Symfony 3:我想创建一个滑块,过滤最小值和最大值之间的研究列表

symfony 3 : i want to create a slider that filter research list betwenn min val and max val

本文关键字:最小值 过滤 最大值 之间 列表 创建 一个 Symfony      更新时间:2023-09-26

这是显示范围滑动条和酒店列表的视图我想通过点击范围

来过滤研究
{% block body %}
<div class="container" style="width:800px;">
    <div align="center">
        <input type="range" min="10000" max="55000" step="1000" value="10000" id="min_price" name="min_price" />
        <span id="price_range"></span>
    </div>
    <br /><br /><br />
    <div id="product_loading">
        <div class="col-md-9">
            <div class="row">
                {% for x in Hotels %}
                    <div class="col-sm-4 col-lg-4 col-md-4">
                        <div class="thumbnail">
                            <img src="{{ asset('uploads/brochures/'~x.brochure) }}" alt="">
                            <div class="caption">
                                <h4 class="pull-right">{{ x.price }}</h4>
                                <h4><a href="#">{{ x.titre }}</a>
                                </h4>
                            </div>
                        </div>
                    </div>
                {% endfor %}
            </div>
        </div>
    </div>
</div>
<script>
    $(document).ready(function(){
        $('#min_price').change(function(){
            var price = $(this).val();
            $("#price_range").text("Product under Price Rs." + price);
            $.ajax({
                url:" {{ path('Hotel__') }},
                data:'price' +price ,
                method:"POST",
                success:function(data){
                    $("#product_loading").fadeIn(500).html(data);
                }
            });
        });
    });
</script>
{% endblock %}

我的问题在AJAX !我必须指定URL和数据有什么建议吗?我必须把url上的视图谁让我过滤的结果和日期参数所以我在仓库中执行getlistthotelinfbudget

public function getListHotelInfBudget($budget)
{
    $qb = $this->createQueryBuilder('h');
    $qb->select('h')
        ->where('h.price <= :budget')
        ->setParameter('budget', $budget);
    return $qb->getQuery()->getResult();
}
控制器

public function getListAction($budget)
{
    $em=$this->getDoctrine()->getManager()->getRepository('.............');
    $list =$em->getListHotelInfBudget($budget);
    return $this->render('....Bundle:....:listHotel.html.twig',array('list'=>$list));
}

和路由配置:

Hotel__:
path:     /hotel/{budget}
defaults: { _controller: .......Bundle:.....:getList }

是一个例外在呈现模板的过程中抛出了一个异常("缺少一些强制参数("price") ")来为route

生成URL

你的代码似乎组织得很糟糕。如何将价格传递给getListAction ?

使用预算作为GET参数?还是使用price作为POST参数?

你的错误出现了,因为:

您的Hotel__路由需要生成{budget}

在你的模板中你调用{{ path('Hotel__') }}

所以你必须定义一个预算:

{{ path('Hotel__',{'budget':142}) }}

但我不认为这是有意义的,因为你的预算可能必须来自滑块,而不是来自twig。


EDIT:

可以用data: {price : price }传递价格

然后像这样把它放到控制器中:

use Symfony'Component'HttpFoundation'Request;
...
public function getListAction(Request $request)
{
    $price = $request->get('price');
}

然后,删除所有{budget}出现