doubai9014 2019-05-13 03:48
浏览 30

如何使用yajra Datatables删除数据(Como eliminar datos usando Datatables Yajra)

i have a problem with delete data from my view, i'm using Yajra Datatables in my project with laravel 5.5

The edit is working really good, but my delete data is not working!

i have configuration in

Web.php api.php my controller my view my actions.php

Please friend, i need fix this problem in my project...

My Web.php with auth


    Route::post('clientes/store', 'ClientesController@store')->name('clientes.store')
        ->middleware('permission:clientes.create');
    Route::get('admin/clientes', 'ClientesController@index')->name('clientes.index')
        ->middleware('permission:clientes.index');
    Route::get('admin/clientes/create', 'ClientesController@create')->name('clientes.create')
        ->middleware('permission:clientes.create');
    Route::put('admin/clientes/{cliente}', 'ClientesController@update')->name('clientes.update')
        ->middleware('permission:clientes.edit');
    Route::get('admin/clientes/{cliente}', 'ClientesController@show')->name('clientes.show')
        ->middleware('permission:clientes.show');
    Route::delete('admin/clientes/{cliente}', 'ClientesController@destroy')->name('clientes.destroy')
        ->middleware('permission:clientes.destroy');

    Route::get('admin/clientes/{cliente}/edit', 'ClientesController@edit')->name('clientes.edit')
        ->middleware('permission:clientes.edit');

my api.php here i create the configuration for datatable.

Route::get('clientes', function (){
    return datatables()
        ->eloquent(GastosComunes\Clientes::query())
        ->addColumn('btn', "admin.clientes.actions")
        ->rawColumns(['btn'])
        ->toJson();
});

my function destroys in my controller

 public function destroy($ID){
        $Clientes = Clientes::FindOrFail($ID);
        $result = $Clientes->delete();
        if ($result){
            return response()->json(['success' => 'true']);
        }else{   
            return response()->json(['success' => 'false']);
        }
    }

My view

<body>
    <input id="token" name="_token" type="hidden" value="{{ csrf_token() }}"/>
<div class="col-lg-8 col-md-8 col-sm-8 col-xs-12">
        <h3> Listado de nuestros clientes @can('clientes.create')<a href="clientes/create"> <button class="btn btn-success">Nuevo</button></a>@endcan</h3>
    </div>
<body>
<div class="table-responsive">
    <table id="clientes" class="table table-striped table-bordered table-condensed table-hover" style="width:100%">
        <thead>
            <tr>
                <th width="20px">ID</th>
                <th>Rut</th>
                <th>Nombre</th>
                <th>Comuna</th>
                <th>Direccion</th>
                <th>Correo</th>
                <th>Telefono</th>
                <th>Acciones</th> 
            </tr>    
        </thead>                   
    </table>
</div>   

@push('scripts')             
<script>
$(document).ready(function() {
        $('#clientes').DataTable({
            "language":{
                "sProcessing":     "Procesando...",
                "sLengthMenu":     "Mostrar _MENU_ registros",
                "sZeroRecords":    "No se encontraron resultados",
                "sEmptyTable":     "Ningún dato disponible en esta tabla",
                "sInfo":           "Mostrando registros del _START_ al _END_ de un total de _TOTAL_ registros",
                "sInfoEmpty":      "Mostrando registros del 0 al 0 de un total de 0 registros",
                "sInfoFiltered":   "(filtrado de un total de _MAX_ registros)",
                "sInfoPostFix":    "",
                "sSearch":         "Buscar:",
                "sUrl":            "",
                "sInfoThousands":  ",",
                "sLoadingRecords": "Cargando...",
                "oPaginate": {
                    "sFirst":    "Primero",
                    "sLast":     "Último",
                    "sNext":     "Siguiente",
                    "sPrevious": "Anterior"
                },
                "oAria": {
                    "sSortAscending":  ": Activar para ordenar la columna de manera ascendente",
                    "sSortDescending": ": Activar para ordenar la columna de manera descendente"
                }
            },
            processing: true,
            serverSide: true,  
            info: false,
            autoWidth: false,
            select: true,
            "ajax": "{{url('api/clientes')}}",
            "columns": [
                {data: 'ID', name: 'ID'},
                {data: 'Rut', name: 'Rut'},
                {data: 'Nombre', name: 'Nombre'},
                {data: 'Comuna', name: 'Comuna'},
                {data: 'Direccion', name: 'Direccion'},
                {data: 'Correo', name: 'Correo'},
                {data: 'Telefono', name: 'Telefono'},
                {data: 'btn'}
            ]
        });

});

$("body").on("click", "#clientes-table #btnEliminar", function(event) {
            event.preventDefault();
            var idsele = $(this).attr("value");
            var token= $("token").val();
            alertify.confirm("Eliminar registro", "¿Deseao eliminar el registro? !Al eliminar este regirtos, se eliminaran todos los datos relacionados al mismo!",
            function() {                
                $.ajax({
                    url: 'clientes/'+idsele,
                    headers: {'X-CSRF-TOKEN': token},
                    type: 'DELETE',
                    dataType: 'json',
                    success: function(data) {
                        if (data.success == 'true') {
                            toastr["success"]('!El registro se elimino exitosamente!');
                            $datable.ajax.reload();
                        }
                        else {
                            toastr["error"]('!El registro no se pudo eliminar!');
                            $datable.ajax.reload();   
                        }
                    }
                });
            },
            function () {
                alertify.notify('Sin acción')
            }
        );
});
</script>
@endpush
</body>

And my actions.blade.php file where i have the call to edit or delete

<a href="{{ route('clientes.edit',$ID) }}"    class="btn btn-primary btn-sm"><i class="fa fa-edit"></i></a>

<a href="{{ route('clientes.destroy',$ID) }}" class="btn btn-danger btn-sm"><i class="fa fa-trash"></i> </a>      

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
    • ¥20 关于URL获取的参数,无法执行二选一查询
    • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
    • ¥15 marlin编译错误,如何解决?
    • ¥15 有偿四位数,节约算法和扫描算法
    • ¥15 VUE项目怎么运行,系统打不开
    • ¥50 pointpillars等目标检测算法怎么融合注意力机制
    • ¥20 Vs code Mac系统 PHP Debug调试环境配置
    • ¥60 大一项目课,微信小程序
    • ¥15 求视频摘要youtube和ovp数据集