0

I want to delete selected multiple rows or all rows on a single click. But i cant figure it. how could i do this with my code? please edit my code for expected result.

Here is my jquery for select all rows

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

How to get id's in to controller to execute delete process? My jquery does not sends any id, i tested with var_dump its shows 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()}}  
munaz
  • 166
  • 2
  • 14
  • Your form submits to `blog/delball` but your delete code is in your `deleteAction()` – Timothy Apr 11 '16 at 07:05
  • update my answer. but really its not getting ids and how to process multiple ids array? – munaz Apr 11 '16 at 07:55
  • Not sure of your controller code but there is a syntax mistake in jquery, `$('input[name=slId]')` should be `$('input[name="slId"]')` note the double quotes for name value – Rajshekar Reddy Apr 11 '16 at 07:57
  • Your controller requests the parameter `id`, but your HTML doesn't have an element with the name `name="id"`. Can you give us the HTML code in addition to the Volt code? – Timothy Apr 11 '16 at 08:00
  • also change jquery as your instruction. but its not sending id to controller. var_dump shows NULL – munaz Apr 11 '16 at 08:02

3 Answers3

1

You should it with declared method:

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

as of you are using it to receive data:

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

To test if you are requesting controller with post, you can extend your code in controller:

if($this->request->isPost())
{
    // ...
} else {
    throw new \Exception('no_post');
}
yergo
  • 4,761
  • 2
  • 19
  • 41
0

I just figure it like this and its working as expected!

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

[volt]

    {{ 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)
});
munaz
  • 166
  • 2
  • 14
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');            
    }
}
munaz
  • 166
  • 2
  • 14