0

我有这个 javascript 函数来删除该行,但该函数不起作用

$(document).ready(function()
{
    $('table#example td a.delete').click(function()
    {
        if (confirm("Are you sure you want to delete this row?"))
        {
            var id = $(this).parent().parent().attr('id');
            var data = 'id=' + id ;
            var parent = $(this).parent().parent();

            $.ajax(
            {
                   type: "POST",
                   url: "supprimerkpi",
                   data: data,
                   cache: false,

                   success: function()
                   {
                        parent.fadeOut('slow', function() {$(this).remove();});

                        // sets specified color for every odd row
                        $('table#example tr:odd').css('background',' #FFFFFF');
                   }
             });
        }
    });

在我的页面 html 中:

<a href="#" class="delete" style="color:#FF0000;">

在我的控制器中

$repository = $this->getDoctrine()->getEntityManager()->getRepository('AdminBlogBundle:Condkpi'); $id=$this->getRequest()->query->get('id');
$em = $this->getDoctrine()->getEntityManager();
$uti=$repository->findOneBy(array('id' => $id));
$em->remove($uti);
$em->flush();
4

2 回答 2

0

在你的routing.yml

delete_data:
    path:     /delete
    defaults: { _controller: AcmeDemoBundle:Default:delete}

在你的ajax调用url参数中根据这个改变

var id = $(this).parent().parent().attr('id');

var data = 'id=' + id ;


 var parent = $(this).parent().parent();

        $.ajax(
        {
               type: "POST",
               url: "{{ path('delete_data') }}",
               data: {id :id },
               cache: false,

               success: function()
               {
                    parent.fadeOut('slow', function() {$(this).remove();});

                    // sets specified color for every odd row
                    $('table#example tr:odd').css('background',' #FFFFFF');
               }
         });

在你的AcmeDemoBundle/Controller/DeafultControlller.php

public function deleteAction(Request $request)
{
   $id = $request->get('id');
   $repository = 
    $this->getDoctrine()->getEntityManager()->getRepository('AdminBlogBundle:Condkpi'); 
   $em = $this->getDoctrine()->getEntityManager();
   $uti=$repository->findOneById($id);
   $em->remove($uti);
   $em->flush();
}
于 2014-09-27T07:42:51.363 回答
0

POST您正在通过方法发送“id” 。所以,你需要改变:

$id=$this->getRequest()->query->get('id');

进入:

$id=$this->getRequest()->request->get('id');

此外,您可以更改:

$uti=$repository->findOneBy(array('id' => $id));

进入:

$uti=$repository->find($id);

..作为find()使用主键搜索实体...

在旁注中,什么是“supprimerkpi”?这不可能是有效的目标网址,对吧?:)

于 2012-10-24T21:21:35.950 回答