使用Ajax和Symfony2即时搜索

时间:2017-02-03 10:38:25

标签: javascript php symfony

我创建了一个在没有ajax和javascript的情况下运行良好的搜索过滤器,但要获得结果,用户必须单击搜索按钮。我希望在用户输入时得到结果。

这就是我所做的:

的routing.yml

searchFlight:
    path: /search_flight
    defaults:  {  _controller: FLYBookingsBundle:Post:searchtabflightResult }

控制器

    /**
 *
 * @Route("/search_flight", name="searchFlight")
 * @Method("POST")
 */
public function searchtabflightResultAction(Request $request)
{
    $form = $this->createForm(new SearchflightType());
    $request->request->get('query');
        $form->handleRequest($request);
        dump($form);
        $em = $this->getDoctrine()->getManager();
        $entities = $em->getRepository('FLYBookingsBundle:Post')->searchflight($form);




    if ($request->isXmlHttpRequest()) {
        $jsonArray = json_encode(array(
            'status' => 'success', 'entities' => $form
        ));
        $response = new Response($jsonArray);
        return $response;

    }

    return $this->render('FLYBookingsBundle:Post:searchtabflightResult.html.twig', array(
        'form' => $form->createView(),
        'entities' => $entities,
        ));
}

searchtabflightresult.html.twig

<form id="myForm" action="{{ path ('searchFlight') }}" method="POST">

 {{ form_widget(form.from, { 'attr': {'class': 'form-control auto',} }) }}
 {{ form_errors(form.from) }}

 {{ form_widget(form.to, { 'attr': {'class': 'form-control auto1',} }) }}
 {{ form_errors(form.to) }}

 {{ form_widget(form.departuredate) }}
 {{ form_errors(form.departuredate) }}

 {{ form_widget(form.arrivaldate) }}
 {{ form_errors(form.arrivaldate) }}

 {{ form_widget(form.price) }}
 {{ form_errors(form.price) }}

<button style="height: 43px" type="submit" class="full-width icon-check">SEARCH NOW</button>

</form>


      <div class="reloadproducts">

      <i id="spinner" style="display:none; font-size: 20px" class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>

      {% if entities|length != 0 %}
      {% for entity in entities %}

      {{ entity.from }}
      {{ entity.to }}
      {{ entity.price|tva(entity.tva.multiplicate) }}
      {% if entity.departuredate %}{{ entity.departuredate|date('Y-m-d H:i:s') }}{% endif %}
      {% if entity.arrivaldate %}{{ entity.arrivaldate|date('Y-m-d H:i:s') }}

      {% endif %} 

      {% endfor %}

      {% else %}
      <h1 style="color: #707070">The product you loking for doesn't exist</h1>
      {% endif %} 

     </div>

    <script>
        $(document).ready(function() {
            $("#myForm").submit(function(e) {
                $('#spinner').show();
                e.preventDefault();
                var $form = $('#myForm');

                $.ajax({
                    type: "POST",
                    dataType: "json",
                    data: $form.serialize(),
                    cache: false,
                    success: function(response) {
                        $('.reloadproducts').load(window.location + ' .reloadproducts >  *');
                        $('#spinner').hide();
                        console.log(response);
                    },
                    error: function(response) {
                        console.log(response);
                        $('#spinner').hide();
                    }
                });

            });
        });
    </script>

添加

class SearchflightType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {


        $builder



            ->add('from', 'genemu_jqueryselect2_entity', array(
                    'class' => 'FLYBookingsBundle:ListDeparturesArrivals',
                    'property' => 'name',
                    'placeholder' => 'Departure city',
                    'query_builder' => function(EntityRepository $er) {
                        return $er->createQueryBuilder('u')
                            ->where('u.mode_transport = :mode')
                            ->setParameter("mode", "AIRPLANE")
                            ->groupBy('u.code')
                            ->orderBy('u.name', 'ASC');
                    },)
            )
            ->add('to', 'genemu_jqueryselect2_entity', array(
                    'class' => 'FLYBookingsBundle:ListDeparturesArrivals',
                    'property' => 'name',
                    'placeholder' => 'Arrival city',
                    'query_builder' => function(EntityRepository $er) {
                        return $er->createQueryBuilder('u')
                            ->where('u.mode_transport = :mode')
                            ->setParameter("mode", "AIRPLANE")
                            ->groupBy('u.code')
                            ->orderBy('u.name', 'ASC');
                    },)
            )

            ->add('departuredate', DateType::class, array(
                'required' => false,
                'label' => 'Departure date',
                'format' => 'y/M/d',
                'widget' => 'single_text',
                'attr' => [
                    'class' => 'form-control date_timepicker_start input-inline',
                    'placeholder' => 'dd/mm/yyyy',
                ]
            ))
            ->add('arrivaldate', DateType::class, array(
                'required' => false,
                'label' => 'Arrival date',
                'format' => 'y/M/d',
                'widget' => 'single_text',
                'attr' => [
                    'class' => 'form-control date_timepicker_end input-inline',
                    'placeholder' => 'dd/mm/yyyy',
                ]
    ))


            ->add('price', TextType::class, array(
                'required' => false,
                'attr' => [
                    'class'             => 'range',
                    "data-slider-min"   => "5",
                    "data-slider-max"   => "1000",
                    "data-slider-step"  => "0.5",
                    "data-slider-value" => "[5,1000]"
                ]
            ));

    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    parent::setDefaultOptions($resolver);
    $resolver->setDefaults(array(
        // avoid to pass the csrf token in the url (but it's not protected anymore)
        'csrf_protection' => false,
        'data_class' => 'FLY\BookingsBundle\Entity\Post'
    ))

    ;
}

public function getName()
{
    return 'fly_bookingsbundle_searchtabflight';
}
}

1 个答案:

答案 0 :(得分:1)

只是想给你一些建议。

首先,如果您想在用户输入时获得结果,请使用'keyup'方法。

不要使用

if ($this->get('request')->getMethod() == 'POST')

如果您只想将此方法用于POST,请使用注释@Method,example here

如果你使用

dataType: "json",

从控制器返回JsonResponse,例如:

    return JsonResponse::create('status' => 'success', 'entities' => $form);

在这种情况下,您不需要设置标头并执行json_encode。 最后一点可能会帮助您解决问题。祝你好运!

更新:

$('#myForm > input').change(function(e) {
            $('#spinner').show();
            e.preventDefault();

            if (xhr) {
                xhr.abort();
                xhr = null;
            }

            var xhr = $.ajax({
                type: "POST",
                dataType: "json",
                data: $form.serialize(),
                cache: false,
                success: function(response) {
                    $('.reloadproducts').load(window.location + ' .reloadproducts >  *');
                    $('#spinner').hide();
                    console.log(response);
                },
                error: function(response) {
                    console.log(response);
                    $('#spinner').hide();
                }
            });

        });