从Laravel中的复选框删除选定的行

时间:2019-08-23 02:13:50

标签: php laravel

我有一个表,其数据从这里的数据库https://imgur.com/Sv4Suo7中检索。我的问题是,我想删除复选框中选择的数据。

我尝试将name =“ ids []”放入我的复选框,但数据仍未发送到我的控制器。我读过一些我需要使用Javascript的地方,但我不知道该怎么做。

观看次数:

        <div class="box-header with-border">

            <div class="box-header-tools pull-left" >

              <a href="{{ url('tasks/create/')}}" class="tips text-green"  title="{{ Lang::get('core.btn_create') }} ">
              <i class="fa fa-plus-square-o fa-2x"></i></a>

              <a href="{{ url('tasks/massDelete')}}"  onclick="" class="tips text-red" title="{{ Lang::get('core.btn_remove') }}">
                    <i class="fa fa-trash-o fa-2x delete_all" data-toggle="confirmation" data-title="{{Lang::get('core.rusure')}}"  data-content="{{ Lang::get('core.rusuredelete') }}" ></i></a>
            </div>


        </div>
        <div class="box-body" >

     <div class="table-responsive" style="min-height:300px; padding-bottom:60px; border: none !important">
    <table class="table table-striped table-bordered " id="{{ $pageModule }}Table">
        <thead>
            <tr>
                <th align="center" class="number"> No </th>
                <th align="center"> <input type="checkbox" class="checkall" id="master" /></th>
                <th align="center">Task</th>    
                <th align="center">Due Date</th>    
                <th align="center">Assigned To</th> 
                <th align="center">Assigned By</th> 
                <th align="center">Status</th>  
                <th align="center">{{ Lang::get('core.btn_action') }}</th>
              </tr>
        </thead>

        <tbody> @foreach($tasks as $task)
                <tr>

                    <td width="30"> {{ ++$i }} </td>
                    <td width="50"><input type="checkbox" class="checkbox" name="ids[]" value="{{$task->id}}" /></td>
                    <td><a href="{{ url('tasks/show/'.$task->id.'')}}" class="tips" title="{{ Lang::get('core.btn_view') }}">{{$task->task_name}} </a></td>
                    <td>{{$task->due_date}}</td>
                    @foreach($users as $user)
                    @if($user->id == $task->assigned_id)<td>{{$user->username}}</td>@endif
                    @endforeach
                    @foreach($users as $user)
                    @if($user->id == $task->assigner_id)<td>{{$user->username}}</td>@endif
                    @endforeach
                    @if($task->status == 0)<td width="90">
                    <span class="label label-block label-info label-sm">Ongoing</span>
                    </td>@endif
                    @if($task->status == 1)<td width="90">
                    <span class="label label-block label-danger label-sm">Cancelled</span>
                    </td>@endif
                    @if($task->status == 2)<td width="90">
                    <span class="label label-block label-success label-sm">Completed</span>
                    </td>@endif
                    <td> 
                        @if($task->status == 0)
                        {!! Form::open(array('url'=>'tasks/completeStatus/'.$task->id, 'class'=>'form-horizontal')) !!}
                        <button type="submit" name="markcomplete" class="btn" ><i class="fa fa-check-circle-o fa-2x"></i></button> 
                        {!! Form::close() !!}
                        @endif
                    </td>
                </tr>
              @endforeach
        </tbody>

    </table>
    <input type="hidden" name="md" value="" />
    </div>

        </div>
    </div>  
</div>

控制器:

public function massDelete(Request $request)
    {
        $gotids = $request->input('ids');

        if($gotids){
            foreach($gotids as $id){
                $task = Tasks::findOrFail($id);
                $task->delete();
            }
        }


        return redirect('tasks');
    }

路线:

Route::get('/tasks/massDelete/', 'TasksController@massDelete');

当我尝试dd($ gotids);时,我希望数据在控制器中。它显示为空。希望任何人都能提供帮助。

1 个答案:

答案 0 :(得分:0)

如果您要使用javascript,则为以下代码

Combining Diacritical Marks for Symbols U+20D0 - U+20FF

刀片文件

 Route::delete('delete-multiple-category', ['as'=>'category.multiple-delete','uses'=>'CategoryController@deleteMultiple']);



  public function deleteMultiple(Request $request){

    $ids = $request->ids;

    Category::whereIn('id',explode(",",$ids))->delete();

    return response()->json(['status'=>true,'message'=>"Category deleted successfully."]);   

} 

 <div class="container">

<h3>PHP Laravel 5.6 - How to delete multiple row with checkbox using Ajax? - HDTuto.com</h3>

@if ($message = Session::get('success'))

<div class="alert alert-success">

    <p>{{ $message }}</p>

</div>

@endif

<button style="margin: 5px;" class="btn btn-danger btn-xs delete-all" data-url="">Delete All</button>

<table class="table table-bordered">

    <tr>

        <th><input type="checkbox" id="check_all"></th>

        <th>S.No.</th>

        <th>Category Name</th>

        <th>Category Details</th>

        <th width="100px">Action</th>

    </tr>

    @if($categories->count())

        @foreach($categories as $key => $category)

            <tr id="tr_{{$category->id}}">

                <td><input type="checkbox" class="checkbox" data-id="{{$category->id}}"></td>

                <td>{{ ++$key }}</td>

                <td>{{ $category->category_name }}</td>

                <td>{{ $category->category_details }}</td>

                <td>

                    {!! Form::open(['method' => 'DELETE','route' => ['category.destroy', $category->id],'style'=>'display:inline']) !!}



                        {!! Form::button('Delete', ['class' => 'btn btn-danger btn-xs','data-toggle'=>'confirmation','data-placement'=>'left']) !!}



                    {!! Form::close() !!}

                </td>

            </tr>

        @endforeach

    @endif

</table>