删除 - 批量操作 - Laravel

时间:2017-11-13 16:06:12

标签: javascript jquery laravel

我正在尝试删除批量操作。(一次删除多个项目)。现在使用下面的代码,所选项目将被删除但仍会返回alert('Whoops Something went wrong!!');为什么会发生这种情况?

HTML

    <button style="margin-bottom: 10px" class="btn delete_all" data-url="{{ url('item/deleteAll') }}">Delete All Selected</button> 



 <tbody>
    @foreach($items as $item)
    <tr id="{{$item->id}}" >
    <td><input type="checkbox" class="sub_chk" data-id="{{$item->id}}"></td>
    </tr>
    @endforeach
    </tbody>

JS

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-confirmation/1.0.5/bootstrap-confirmation.min.js"></script>

    <script type="text/javascript">
    $(document).ready(function () {

        $('#master').on('click', function(e) {
         if($(this).is(':checked',true))  
         {
            $(".sub_chk").prop('checked', true);  
         } else {  
            $(".sub_chk").prop('checked',false);  
         }  
        });

        $('.delete_all').on('click', function(e) {

            var allVals = [];  
            $(".sub_chk:checked").each(function() {  
                allVals.push($(this).attr('data-id'));
            });  

            if(allVals.length <=0)  
            {  
                alert("Please select row.");  
            } 

            else {  

                var check = confirm("Are you sure you want to delete this row?");  
                if(check == true){  

                    var join_selected_values = allVals.join(","); 

                    $.ajax({
                        url: $(this).data('url'),
                        type: 'GET',
                        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                        data: 'ids='+join_selected_values,

                        success: function (data) {
                            if (data['success']) 
                            {
                                $("#" + data['tr']).slideUp("slow");
                                alert(data['success']);
                            } 
                            else if (data['error']) 
                            {
                                alert(data['error']);
                            } 
                            else 
                            {   //this is where the issue is
                                alert('Whoops Something went wrong!!');
                            }
                        },
                        error: function (data) {
                            alert(data.responseText);
                        }
                    });

                  $.each(allVals, function( index, value ) 
                  {
                      $('table tr').filter("[data-row-id='" + value + "']").remove();
                  });
                }  
            }  
        });

        $('[data-toggle=confirmation]').confirmation({
            rootSelector: '[data-toggle=confirmation]',
            onConfirm: function (event, element) {
                element.trigger('confirm');
            }
        });

        $(document).on('confirm', function (e) {
            var ele = e.target;
            e.preventDefault();

            $.ajax({
                url: ele.href,
                type: 'GET',
                headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                success: function (data) {
                    if (data['success']) 
                    {
                        $("#" + data['tr']).slideUp("slow");
                        alert(data['success']);
                    } 
                    else if (data['error']) {
                        alert(data['error']);
                    } 
                    else 
                    {
                        alert('Whoops Something went wrong!!');
                    }
                },
                error: function (data) {
                    alert(data.responseText);
                }
            });

            return false;
        });
    });
</script>

路线

Route::get('item/deleteAll','controller@deleteAll');    

控制器

 public function deleteAll(Request $request)
     {
         $ids = $request->ids; 
         DB::table("items")->whereIn('id',explode(",",$ids))->delete(); 
         return redirect('item/all')->with('status','Items Successfully deleted!');

     }

1 个答案:

答案 0 :(得分:1)

由于这是一个ajax调用,你应该返回类似

的内容
return ['success' => 1];

来自你的控制器,而不是重定向。

一个例子:

    public function deleteAll(Request $request)
{
    $ids = $request->ids;
    DB::table("items")->whereIn('id',explode(",",$ids))->delete();

    if(request()->ajax()) {
        return ['success' => 1];
    }

    return redirect('item/all')->with('status','Items Successfully deleted!');
}