【问题标题】:DataTable in Laravel not workingLaravel 中的数据表不起作用
【发布时间】:2017-12-24 16:56:07
【问题描述】:

我正在尝试在 Laravel 中实现 DataTables,但它不起作用。表:

<table class="table table-striped" id="Clientes">
        <thead>
          <tr>
            <th>ID</th>
            <th>Nombre</th>
            <th>Domicilio</th>
          </tr>
        </thead>
        <tbody>

        </tbody>
</table>

脚本:

  $(document).ready(function() {
    $('#Clientes').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": '{{ route('getClientes') }}',
        "columns": [
          {"data": 'id', "name": 'id'},
          {"data": 'nombre', "name": 'nombre'},
          {"data": 'domicilio', "name": 'domicilio'}
        ]
    } );
} );

还有路线:

Route::get('/api/clientes', [
        'as'    => 'getClientes',
        'uses'  => function(){
        $clientes = Cliente::select(['id','nombre','domicilio'])->get();
        return Datatables::of($clientes)->make();}
    ]);

表格显示错误: DataTables 警告:表 id=Clientes - Ajax 错误。有关此错误的更多信息,请参阅http://datatables.net/tn/7

但我无法解决问题。谁能帮帮我?

更新

错误是: message": "找不到类 'Yajra\Datatables\Facades\DataTables'", "异常": "Symfony\Component\Debug\Exception\FatalThrowableError",

【问题讨论】:

  • 也许是愚蠢的问题,但您是否包含了“提供者”=> [ ..., Yajra\DataTables\DataTablesServiceProvider::class, ] 'aliases' => [ ..., 'DataTables' = > Yajra\DataTables\Facades\DataTables::class, ] 在你的配置中?
  • 是的,我添加了。问题是该类是“DataTables”,我称之为“Datatables”

标签: jquery laravel datatables


【解决方案1】:

最后不要使用get()

代替

$clientes = Cliente::select(['id','nombre','domicilio'])->get();

使用

$clientes = Cliente::select(['id','nombre','domicilio']);

【讨论】:

    【解决方案2】:

    数据表服务器端 查看

                                    <table  class="table table-striped display nowrap" id="brand">
                                        <thead class=" text-primary">
                                        <th>
                                            ID
                                        </th>
                                        <th style="width: 85%">
                                            Brand
                                        </th>
    
                                        <th>
                                            Active
                                        </th>
                                        </thead>
                                        <tbody style="font-size: 12px; font-weight: bold">
    
                                    
                                        </tbody>
                                    </table>
    <script>
    $(document).ready(function() {
        $('#brand').DataTable( {
            dom: 'Bfrltip',
            pageLength: 10,
            lengthMenu: [ 5, 10, 20, 50, 100, 200, 500, 1000,2000,3000,4000, 50000, 100000],
            // processing:true,
            // serverSide:true,
            "oLanguage": {
                sLengthMenu: " Length of rows _MENU_ ",
            },
    
            ajax:{
                url:"{{ url('Main/fetch_brand') }}",
                dataType: "json",
                type:"POST",
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                // error:function()
                // {
                //     alert("Something went wrong. Please refresh the page.");
                //     location.reload();
                // }
    
            },
    
            order:[],
    
            buttons: [
                {
                    extend:    'copyHtml5',
                    text:      '<i class="fa fa-files-o"></i>',
                    titleAttr: 'Copy',
                    className: 'btn btn-outline-primary ',
                    exportOptions: {
                        columns: "thead th:not(.noExport)"
                    }
    
                },
                {
                    extend:    'excelHtml5',
                    text:      '<i class="fa fa-file-excel"></i>',
                    titleAttr: 'Excel',
                    className: 'btn btn-outline-primary ',
                    exportOptions: {
                        columns: "thead th:not(.noExport)"
                    }
    
                },
    
                {
                    extend:    'csvHtml5',
                    text:      '<i class="fa fa-file-csv"></i>',
                    titleAttr: 'CSV',
                    className: 'btn btn-outline-primary ',
                    exportOptions: {
                        columns: "thead th:not(.noExport)"
                    }
    
    
                },
                {
                    extend:    'pdfHtml5',
                    text:      '<i class="fa fa-file-pdf"></i>',
                    titleAttr: 'PDF',
                    className: 'btn btn-outline-primary ',
                    exportOptions: {
                        columns: "thead th:not(.noExport)"
                    }
    
    
                }
            ]
        } );
    } );
    
    </script>
    

    控制器

        function brand(){
        if(session('role')){
        $obj = new  BackendModel;
        $data['data']=$obj->get_brand();
    
            return view('brand',$data);
        }else{
            return view('login');
        }
    }
    function fetch_brand(Request $request){
    
        $obj = new  BackendModel;
        $callData= $obj->call_brand();
        $totalData = count($obj->count_call_brand());
        $counter = 1;
        foreach ($callData as $row) {
            $callData = array();
            $callData[] = $counter;
            $callData[] = $row->name;
            $callData[] = '  <button type="button"  class="btn btn-sm btn-primary editfrom" data-id="{{ $row->id }}"  data-toggle="modal" data-target="#exampleModal1">
                                                    <i class="fas fa-edit"></i>
                                                </button>
                                                <span class="btn btn-sm  btn-danger"><i class="fas fa-trash"></i></span>';
    
            $data[] = $callData;
            $counter++;
        }
        $output = array(
            "draw"              =>  intval($request->input('draw')),
            "recordsTotal"      =>  intval($totalData),
            "recordsFiltered"   =>  intval($totalData),
            "data"              =>  $data
        );
        echo json_encode($output);
    
    
    }
    

    型号

       public function call_brand(){
            $calling = 0;
            $query = $this->get_data_brand($calling);
    
            return $query;
        }
        public function count_call_brand(){
            $calling = 1;
            $query = $this->get_data_brand($calling);
    
            return $query;
        }
        public function get_data_brand($calling){
    
            if(!empty($_POST["order"]))
            {
                DB::orderBy($_POST['order']['0']['column'], $_POST['order']['0']['dir']);
            }
    
    
            if($calling==1){
                if(isset($_POST["length"]) && $_POST["length"]  != -1)
                {
                   DB::limit($_POST['length'], $_POST['start']);
                }
            }
            if(isset($_POST["search"]["value"]))
            {
                $fields  = Schema::getColumnListing('brand');
    
                foreach ($fields as $field)
                {
                    if($field!="id" ){
                        DB::where($field, 'like', '%'.$field.'%');
    
    
                    }
    
                }
            }
    
    
            $query = DB::table('brand')->get();
    
    
    
            return $query;
    
        }
    

    【讨论】:

      猜你喜欢
      • 2017-04-14
      • 1970-01-01
      • 2021-09-21
      • 2020-05-03
      • 2016-01-18
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多