Francisca GV
Francisca GV

Reputation: 389

set entity attribute (selecting a radio button) with ajax in Symfony 2.5

I want to integrate Ajax in my Symfony project (Symfony 2.5 and jQuery 3). I want to update an attribute of an Entity when I select a radio button. For now, I can get the id of the row that I select. I have searched how to implement this, but I have not succeeded.

JS code:

$(document).ready(function(){
    $("input:radio[name=locacion_destacada]").click(function () {
         var id = $(this).parent().parent().attr('dataId');
         alert(id);
         $.ajax({
             url: "/update-destacado",
             type: "POST",
             data: { id : id },
             success: function (response) {
                 alert(response);
             },
             error: function (XMLHttpRequest, textStatus, errorThrown) {
                 alert('Error: ' + errorThrown);
             }
         });

     });
});

Any help is greatly appreciated.

Upvotes: 0

Views: 300

Answers (3)

Francisca GV
Francisca GV

Reputation: 389

I could resolve it. I followed the idea of this page (answer n°8), also keep in mind your answers. Thanks for your help.

Controller code:

public function DestacadoAction(Request $request, $id){

    $em = $this->getDoctrine()->getManager();

    //Encontrar la locacion que ya estaba como destacada y dejarla como destacado=false
    $locacionDestacadaAntigua = $em 
        ->getRepository('FilmsBundle:Locaciones') 
        ->findOneBy(
            array(
                'destacado' => true
            ));

    $locacionDestacadaAntigua->setDestacado(false);
    $em->persist($locacionDestacadaAntigua);
    $em->flush();

    $em = $this->getDoctrine()->getManager();

    //Dejar como destacada la nueva locacion
    $locacionDestacadaNueva = $this->getDoctrine() 
        ->getRepository('FilmsBundle:Locaciones') 
        ->findOneBy(
            array(
                'idLocacion' => $id
            ));
    $locacionDestacadaNueva->setDestacado(true);
    $em->persist($locacionDestacadaNueva);
    $em->flush();

    return new Response("Ha seleccionado la locación \"" . $locacionDestacadaNueva->getNombreLocacion() . "\" como destacada.");
}

JS Code:

$(document).ready(function(){
    $(".button").on("click", function (e) {
        $.post(this.href).done(function (response) {
            alert(response);
            location.reload();
        });
    });
});

Twig code:

{% if locacion.destacado == true %}
    <td align="center">
        <a class="button" href="{{ path('admin_destacado_update', { 'id': locacion.idLocacion }) }}">
            <button class="btn btn-default">
                <i class="glyphicon glyphicon-ok"></i>
            </button>
        </a>
    </td>
{% else %}
    <td align="center">
        <a class="button" href="{{ path('admin_destacado_update', { 'id': locacion.idLocacion }) }}">
            <button class="btn btn-sm">
                <i class="glyphicon glyphicon-remove"></i>
            </button>
        </a>
    </td>
{% endif %}

Upvotes: 0

P.Prochaska
P.Prochaska

Reputation: 49

You need a controller action, which is called by your "update-destacado" route. Then read the ID from the request and update your entity.

Upvotes: 0

Dhia Eddine Farah
Dhia Eddine Farah

Reputation: 260

You can do that in the controller by calling it in the url of ajax :

url : {{ path('your_route', {'id': id})}};

and in the controller function you can update your entity as you like

Upvotes: 0

Related Questions