【问题标题】:Delete multiple selected rows in phalcon删除phalcon中的多个选定行
【发布时间】:2016-04-11 04:56:12
【问题描述】:

我想通过单击删除选定的多行或所有行。 但我想不通。我怎么能用我的代码做到这一点?请编辑我的代码以获得预期结果。

这是我用于选择所有行的 jquery

[Jquery]
function selectAll(status){
$('input[name=slId]').each(function(){
    $(this).prop('checked', status);
});
}

如何让 id 进入控制器以执行删除过程?我的 jquery 没有发送任何 id,我用 var_dump 测试它显示为 NULL。

[Controller]

public function deleteAction()
{
    if($this->request->isPost())
    {
        if($this->session->has('uname'))
        {
            $id = array();
            $id = $this->request->getPost('id');
            $data = Blogs::findByid($id);
            if(!$data->delete())
             {
               echo('Unable to Delete');
             }
        }
    }
}

[volt]

{{ form('blog/delete', 'enctype': 'multipart/form-data') }}
<table class="bloglist">
<thead>
    <tr class="fbold">
           <td>
{{check_field('checkbox','id':'sall','onclick':'selectAll(this.checked)')}}     </td>
        <td>Title</td>
        <td>Author</td>
        <td>Views</td>
        <td>PublishedOn</td>
    </tr>
</thead>
<tbody>
{%for all in ball %}    
    <tr class="zebra">
        <td>{{check_field('slId', 'class':'slId','id':all.id)}}</td>
        <td class="tal">{{all.btitle}}</td>
        <td>{{all.bauthor}}</td>
        <td>{{all.views}}</td>
        <td>{{all.datetime}}</td>
    </tr>   
{% endfor %}        
</tbody>
<tfoot>
    <tr>
        <td colspan="6">{{submit_button('DELETE')}}</td>
    </tr>   
</tfoot>
</table>
{{end_form()}}  

【问题讨论】:

  • 您的表单提交给blog/delball,但您的删除代码在您的deleteAction()
  • 更新我的答案。但实际上它没有得到 ids 以及如何处理多个 ids 数组?
  • 不确定你的控制器代码但是jquery中有语法错误,$('input[name=slId]')应该是$('input[name="slId"]')注意名称值的双引号
  • 您的控制器请求参数id,但您的HTML 没有名称为name="id" 的元素。除了 Volt 代码,你能给我们提供 HTML 代码吗?
  • 也将 jquery 更改为您的指令。但它没有向控制器发送 id。 var_dump 显示 NULL

标签: jquery mysql phalcon


【解决方案1】:

你应该用declared method

{{ form('blog/delete', 'method': 'post') }}

你正在使用它来接收数据:

$id = $this->request->getPost('id');

要测试您是否通过 post 请求控制器,您可以在控制器中扩展您的代码:

if($this->request->isPost())
{
    // ...
} else {
    throw new \Exception('no_post');
}

【讨论】:

    【解决方案2】:

    我只是认为它是这样的,它按预期工作!

    [控制器]

    public function deleteBlogAction()
    {
        if($this->request->isPost() == true)
        {
            if($this->session->has('uname'))
            {
                $ids = $this->request->getPost('item');                
                foreach($ids as $item)
                {
                    $blogs = Blogs::findFirst($item);
    #Erase Post Related Image                    
                    $uploadPath = 'uploads/blogs/';
                    $defaultImg = $uploadPath.'empty.png';
                    $getImg = $uploadPath.$blogs->bimage;
                    if($getImg == true AND $getImg != $defaultImg)
                    {
                        if(@unlink($getImg) == false)
                        {
                            echo('Uploaded Image Cannot Delete');
                        }
                    }
     #Erase Post Related Comments      
                    $deleteC = Comments::findByentry_id($item)->delete();
    #Erase Blog Posts                    
                    $deleteB = Blogs::findFirst($item)->delete();
                }
                if($deleteC != false AND $deleteB != false)
                {
                    $this->flashSession->success("The post &amp; related comments has been deleted.");
                    return $this->response->redirect('blog/getBlog');
                }
                else
                {
                    $this->flashSession->error("Sorry! We are unable to delete.");
                    return $this->response->redirect('blog/getBlog');
                }
            }
            else
            {
                $this->flashSession->error("Unauthorised Access!");
                return $this->response->redirect('blog/getBlog');                
            }
        }
        else
        {
                $this->flashSession->error("Request May Not Posted.");
                return $this->response->redirect('blog/getBlog');            
        }
    }
    

    [伏特]

        {{ form('blog/deleteBlog', 'enctype': 'multipart/form-data') }}
    <table class="bloglist">
        <thead>
            <tr class="fbold">
                <td>{{check_field('item','class':'toggle-button')}}</td>
                <td>Title</td>
                <td>Author</td>
                <td>Views</td>
                <td>PublishedOn</td>
            </tr>
        </thead>
        <tbody>
    {%for all in ball %}    
            <tr class="zebra">
                <td>{{check_field('item[]','value':all.id)}}</td>
                <td class="tal">{{all.btitle}}</td>
                <td>{{all.bauthor}}</td>
                <td>{{all.views}}</td>
                <td>{{all.datetime}}</td>
            </tr>       
    {% endfor %}        
        </tbody>
        <tfoot>
            <tr>
                <td colspan="6">{{submit_button('DELETE')}}</td>
            </tr>   
        </tfoot>
    </table>
    {{end_form()}}  
    

    [jquery]

    $('.toggle-button').click(function(){
        $('input[type="checkbox"]').prop('checked', this.checked)
    });
    

    【讨论】:

      【解决方案3】:

      [控制器]

      public function deleteBlogAction()
      {
          if($this->request->isPost() == true)
          {
              if($this->session->has('uname'))
              {
                  $ids = $this->request->getPost('item');                
                  foreach($ids as $item)
                  {
                      $blogs = Blogs::findFirst($item);
       #Erase Post Related Image                    
                      $uploadPath = 'uploads/blogs/';
                      $defaultImg = $uploadPath.'empty.png';
                      $getImg = $uploadPath.$blogs->bimage;
                      if($getImg == true AND $getImg != $defaultImg)
                      {
                          if(@unlink($getImg) == false)
                          {
                              echo('Uploaded Image Cannot Delete');
                          }
                      }
       #Erase Post Related Comments      
                      $deleteC = Comments::findByentry_id($item)->delete();
      #Erase Blog Posts                    
                      $deleteB = Blogs::findFirst($item)->delete();
                  }
                  if($deleteC != false AND $deleteB != false)
                  {
                      $this->flashSession->success("The post &amp; related comments has been deleted.");
                      return $this->response->redirect('blog/getBlog');
                  }
                  else
                  {
                      $this->flashSession->error("Sorry! We are unable to delete.");
                      return $this->response->redirect('blog/getBlog');
                  }
              }
              else
              {
                  $this->flashSession->error("Unauthorised Access!");
                  return $this->response->redirect('blog/getBlog');                
              }
          }
          else
          {
                  $this->flashSession->error("Request May Not Posted.");
                  return $this->response->redirect('blog/getBlog');            
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-07-01
        • 2016-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多