单击多个复选框删除记录

时间:2019-05-22 11:00:54

标签: php jquery ajax laravel

我想删除在复选框中选中的一条记录,并且还要删除一条整个记录。

这是用户可以在其中选择所有复选框的选项卡。

<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>

此表结构

<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>

这是脚本

$(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);
            }
        });
    });
});

已删除,控制器。

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::post('/deleteMultipleMail','HomeController@deleteMultipleMail');

单击复选框删除所有记录,否则仅删除那些将被删除的记录。

1 个答案:

答案 0 :(得分:1)

在您的js文件中:

$('.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);
        }
    });
});

在您的控制器中:

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