删除 phalcon 中的多个选定行
Delete multiple selected rows in phalcon
我想通过单击删除 select 多行或所有行。
但我想不通。我怎么能用我的代码做到这一点?请编辑我的代码以获得预期结果。
这是我的 jquery select 所有行
[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()}}
我就是这样想的,它按预期工作!
[控制器]
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 & 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)
});
你应该 declared method:
{{ form('blog/delete', 'method': 'post') }}
当您使用它接收数据时:
$id = $this->request->getPost('id');
要测试您是否使用 post 请求控制器,您可以在控制器中扩展代码:
if($this->request->isPost())
{
// ...
} else {
throw new \Exception('no_post');
}
[控制器]
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 & 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');
}
}
我想通过单击删除 select 多行或所有行。 但我想不通。我怎么能用我的代码做到这一点?请编辑我的代码以获得预期结果。
这是我的 jquery select 所有行
[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()}}
我就是这样想的,它按预期工作!
[控制器]
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 & 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)
});
你应该 declared method:
{{ form('blog/delete', 'method': 'post') }}
当您使用它接收数据时:
$id = $this->request->getPost('id');
要测试您是否使用 post 请求控制器,您可以在控制器中扩展代码:
if($this->request->isPost())
{
// ...
} else {
throw new \Exception('no_post');
}
[控制器]
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 & 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');
}
}