2016-04-11 25 views
0

我想通過一次單擊來刪除所選的多行或所有行。 但我不能確定它。我怎麼能用我的代碼做到這一點?請編輯我的代碼以獲得預期結果。在phalcon中刪除多個選定的行

這裏是我的選擇所有的行

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

jQuery的如何獲得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()}} 
+0

您的表單提交至'blog/delball',但您的刪除代碼位於您的'deleteAction()' – Timothy

+0

更新我的答案。但真的它沒有得到ids和如何處理多個ids數組? – munaz

+0

不確定你的控制器代碼,但在jquery中有語法錯誤,'$('input [name = slId]')'應該是'$('input [name =「slId」]')'注意雙引號對於名稱值 –

回答

0

[Controller]

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');    
    } 
} 
0

我只是想像它,它的工作如預期!

[控制器]

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) 
}); 
1

應將其與declared method

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

爲你使用它來接收數據:

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

要測試,如果你要請求與柱控制器,你可以在控制器擴展代碼:

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