0

I want to delete a record which is selected in the checkbox and also deleted a whole record as well.

This is the tab where user can select checkbox for select all.

<button type="checkbox" id="checkall" class="btn btn-default btn-sm checkbox-toggle">
    <i class="fa fa-square-o" ></i>
</button>
<div class="btn-group">
    <button type="submit" class="btn btn-default btn-sm" >
        <i class="fa fa-trash-o"></i>
    </button>
</div>

<!-- /.btn-group -->
<a href="{{ route('home.notificationbox') }}">
    <button type="button" class="btn btn-default btn-sm">
        <i class="fa fa-refresh"></i>
    </button>
</a>

This the table structure

<div class="table-responsive mailbox-messages">
    <table id="mailplan" class="table table-hover-row table-striped-row{{ count($admin_notifications) > 0 ? 'datatable' : '' }}" data-page-length="25">
        <tbody>
            @if (count($admin_notifications) > 0)

                <?php $count=1; ?>
                @foreach ($admin_notifications as $admin_notification)

                    @if($admin_notification['is_read'] == 1)            
                        <tr class="table-dark-row" data-entry-id="{{ $admin_notification['notification_id'] }}">
                            <td>
                                <a href="">{{ $count++ }}</a>
                            </td>
                            <td>
                                <input type="checkbox" class="checkbox" 
                                    onclick="delete_admin_notification({{ $admin_notification['notification_id'] }});"
                                    name = "deleteMultipleMail[]" 
                                    value = "{{ $admin_notification['notification_id' ]}}">
                            </td>
                            <td class="mailbox-name">
                                <a href="#" onclick="showInDetail('{{ $admin_notification['message_type'] }}','{{ $admin_notification['message_view_id'] }}','{{ $admin_notification['notification_msg_id'] }}')">
                                    {{ $admin_notification['message_from'] }}
                                </a>
                            </td>
                            <td class="mailbox-subject">
                                <b>{{ $admin_notification['title'] }}</b> - {{ $admin_notification['message'] }}
                            </td>
                            <td class="mailbox-date">
                                {{ $admin_notification['duration'] }}
                            </td>
                        </tr>
                    @else

                        <tr data-entry-id="{{ $admin_notification['notification_id'] }}">
                            <td>
                                <a href="">{{ $count++ }}</a>
                            </td>
                            <td>
                                <input type="checkbox" class="checkbox" 
                                    onclick = "delete_admin_notification({{ $admin_notification['notification_id']}});"
                                    value = "{{ $admin_notification['notification_id' ]}}">
                            </td>
                            <td class="mailbox-name">
                                <a href="#" onclick="showInDetail('{{ $admin_notification['message_type'] }}','{{ $admin_notification['message_view_id'] }}','{{ $admin_notification['notification_msg_id'] }}')">
                                    {{ $admin_notification['message_from'] }}
                                </a>
                            </td>
                            <td class="mailbox-subject">
                                <b>{{ $admin_notification['title'] }}</b> - {{ $admin_notification['message'] }}
                            </td>
                            <td class="mailbox-date">
                                {{ $admin_notification['duration'] }}
                            </td>
                        </tr>
                    @endif                                  
                @endforeach
            @else
                <tr>
                    <td colspan="12">No Subscription Plan Available</td>
                </tr>
            @endif                                  
        </tbody>

    </table>
    <!-- /.table -->
</div>

Here is the script

$(function () {

    $('#check_all').on('click', function(e) {

        if($(this).is(':checked',true)) {

            $(".checkbox").prop('checked', true);  
        } else { 
            $(".checkbox").prop('checked',false);  
        }
    });

    $('.checkbox').on('click',function() {

        if($('.checkbox:checked').length == $('.checkbox').length) {
            $('#check_all').prop('checked',true);
        } else {
            $('#check_all').prop('checked',false);
        }
    });

    $('.delete-all').on('click', function(e) {
        var idsArr = []; 
        var strIds = idsArr.join(","); 
        $.ajax({

            url: "/deleteMultipleMail",
            type: 'POST',
            headers: {'X-CSRF-TOKEN':token},
            data: 'ids='+strIds,
            success: function (data) {
                if(response['success']){
                window.location = response['redirect_url']; 
                } else {
                    alert('Whoops Something went wrong!!');
                }
            },
            error: function (data) {
                alert(data.responseText);
            }
        });
    });
});

This is deleted, controller.

public function delete_admin_notification(Request $request)
{
    if(!empty($request)) {

        $adminNotification=AdminNotification::find($request['notification_id'])->delete();
        return redirect()->back();
    } else {
        return false;
    }   
}
public function deleteMultipleMail(Request $request)
{
    dd($request);
    $delId = $request->input('deleteMultipleMail');
    AdminNotification::whereIn('notification_id' , $delId)->delete();
    return redirect()->back();
}

Route page

Route::post('/deleteMultipleMail','HomeController@deleteMultipleMail');

Delete all record on clicking the checkbox or those who are selected only that would be deleted.

6
  • Does it dump and die on the first line of deleteMultipleMail? Or don't you get in that function? Commented May 22, 2019 at 11:11
  • I just update a delete_at column with the help of "->delete()" it just update a delete_at column Commented May 22, 2019 at 11:13
  • Sorry, I do not understand what you're saying Commented May 22, 2019 at 11:15
  • On clicking a checkbox I didn't get an id for storing to the var idsArr = []; var strIds = idsArr.join(","); Commented May 22, 2019 at 11:25
  • Yeah that's the problem but my question is, does he do the dd() on the first line of deleteMultipleMail? It's a yes/no question Commented May 22, 2019 at 12:01

1 Answer 1

1

In your js file:

$('.delete-all').on('click', function(e) {
    var idsArr = []; 

    $('.checkbox').each(function(){
        var isChecked = $(this).prop('checked');

        if(isChecked){
            idsArr.push($(this).val());
        }
    });

    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    $.ajax({
        url: "/deleteMultipleMail",
        type: 'POST',
        data:{
            idsArr: idsArr
        },
        success: function (response) {
            if(response.success){
             window.location = response.redirect_url; 
            } else {
                alert('Whoops Something went wrong!!');
            }
        },
        error: function (data) {
            alert(data.responseText);
        }
    });
});

In your controller:

public function deleteMultipleMail(Request $request)
{
    $post = $request->all();

    AdminNotification::whereIn('notification_id' , $post['idsArr'])->delete();

    return response()->json(['success' => true, 'redirect_url' => 'your redirect url']);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.