【问题标题】:set entity attribute (selecting a radio button) with ajax in Symfony 2.5在 Symfony 2.5 中使用 ajax 设置实体属性(选择单选按钮)
【发布时间】:2018-09-12 06:49:36
【问题描述】:

我想在我的 Symfony 项目(Symfony 2.5 和 jQuery 3)中集成 Ajax。我想在选择单选按钮时更新实体的属性。现在,我可以获得我选择的行的 id。我已经搜索了如何实现这一点,但我没有成功。

JS 代码

$(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);
             }
         });

     });
});

非常感谢任何帮助。

【问题讨论】:

    标签: ajax symfony symfony-2.5 jquery-3


    【解决方案1】:

    我可以解决它。我遵循了这个page (answer n°8) 的想法,也请记住你的答案。感谢您的帮助。

    控制器代码

    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 代码

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

    树枝代码

    {% 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 %}
    

    【讨论】:

      【解决方案2】:

      您需要一个控制器操作,该操作由您的“update-destacado”路由调用。然后从请求中读取 ID 并更新您的实体。

      【讨论】:

        【解决方案3】:

        您可以在控制器中通过在 ajax 的 url 中调用它来做到这一点:

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

        在控制器功能中,您可以根据需要更新实体

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-05-12
          • 1970-01-01
          • 2016-08-29
          • 2011-04-27
          • 2015-08-02
          • 2023-03-17
          • 2017-12-01
          相关资源
          最近更新 更多