Laravel Yajra数据表未显示搜索,分页和过滤器

时间:2019-09-04 08:59:23

标签: javascript php laravel yajra-datatable

我第一次尝试在Laravel中使用数据表,并且遇到了Yajra lib。我正在使用Laravel Framework 5.8.34,mySQL数据库。我正在关注this教程。我无法使其显示搜索,分页等信息,例如其示例: enter image description here

矿井出来像: enter image description here

使用PHP时,我知道这是一个JS问题,但这是我的刀片:

@extends('layouts.app')

@section('content')

<head>
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />
    <link rel="stylesheet" href="//cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css">

    <!-- jQuery -->
    <script src="//code.jquery.com/jquery.js"></script>

    <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
    <!-- Bootstrap JavaScript -->
    <script src="//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>

<div class="container">
<div class="row justify-content-center">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-header">Dashboard</div>

            <div class="card-body">
                @if (session('status'))
                    <div class="alert alert-success" role="alert">
                        {{ session('status') }}
                    </div>
                @endif

                    <div >
                        <div class="panel">
                            <div class="panel-header">
                                <h2 align="center">{{AUth::user()->name}} - {{AUth::user()->paynumber}}</h2>
                                <hr>
                            </div>

                            <div class="panel-body">
                                @if (session('status'))
                                    <div class="alert alert-success">
                                        {{ session('status') }}
                                    </div>
                                @endif

                                    <div >

                                        <div class="col-md-4">
                                            <div class="form-group">
                                                <select name="filter_type_of_leave" id="filter_type_of_leave" class="form-control" required>
                                                    <option value="">Select Gender</option>
                                                    <option value="Male">Male</option>
                                                    <option value="Female">Female</option>
                                                </select>
                                            </div>
                                            <div class="form-group">
                                                <select name="filter_applied_by" id="filter_applied_by" class="form-control" required>
                                                    <option value="">Select Leave Requestor</option>
                                                    @foreach($leaveslist as $requestor)

                                                        <option value="{{ $requestor->applied_by }}">{{ $requestor->applied_by }}</option>

                                                    @endforeach
                                                </select>
                                            </div>

                                            <div class="form-group" align="center">
                                                <button type="button" name="filter" id="filter" class="btn btn-info">Filter</button>

                                                <button type="button" name="reset" id="reset" class="btn btn-default">Reset</button>
                                            </div>
                                        </div>

                                        <div class="table-responsive">
                                            <table id="leave_data" class="table table-bordered table-striped">
                                                <thead>
                                                <tr>
                                                    <th>Pay Number</th>
                                                    <th>Type of Leave</th>
                                                    <th>Days taken</th>
                                                    <th>Date From</th>
                                                    <th>Date To</th>
                                                    <th>Applied By</th>
                                                </tr>
                                                </thead>

                                                <tbody>
                                                @foreach ($leaveslist as $ileave)
                                                    <tr>
                                                        <td>{{ $ileave->paynumber }}</td>
                                                        <td>{{ $ileave->type_of_leave }}</td>
                                                        <td>{{ $ileave->days_taken }} </td>
                                                        <td>{{ $ileave->date_from }}</td>
                                                        <td>{{ $ileave->date_to }}</td>
                                                        <td>{{ $ileave->applied_by }}</td>

                                                    </tr>
                                                @endforeach
                                                </tbody>
                                            </table>
                                        </div>

                                    </div>

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

            </div>
        </div>
    </div>
</div>
</div>
@endsection

<script>
$(document).ready(function(){

    fill_datatable();

    function fill_datatable(filter_type_of_leave = '', filter_applied_by = '')
    {
        var dataTable = $('#leave_data').DataTable({
            processing: true,
            serverSide: true,
            ajax:{
                url: "{{ route('dashboard.index') }}",
                data:{filter_type_of_leave:filter_type_of_leave, filter_applied_by:filter_applied_by}
            },
            columns: [
                {
                    data:'paynumber',
                    name:'paynumber'
                },
                {
                    data:'type_of_leave',
                    name:'type_of_leave'
                },
                {
                    data:'days_taken',
                    name:'days_taken'
                },
                {
                    data:'days_from',
                    name:'days_from'
                },
                {
                    data:'days_to',
                    name:'days_to'
                },
                {
                    data:'applied_by',
                    name:'applied_by'
                }
            ]
        });
    }

    $('#filter').click(function(){
        var filter_type_of_leave = $('#filter_type_of_leave').val();
        var filter_applied_by = $('#filter_applied_by').val();

        if(filter_type_of_leave != '' &&  filter_type_of_leave != '')
        {
            $('#leave_data').DataTable().destroy();
            fill_datatable(filter_type_of_leave, filter_applied_by);
        }
        else
        {
            alert('Select Both filter option');
        }
    });

    $('#reset').click(function(){
        $('#filter_type_of_leave').val('');
        $('#filter_applied_by').val('');
        $('#leave_data').DataTable().destroy();
        fill_datatable();
    });

});
</script>

这是我的控制器:

public function index(Request $request)
{
    if(request()->ajax())
    {
        if(!empty($request->filter_type_of_leave))
        {
            $data = DB::table('leaves')
                ->select('paynumber', 'type_of_leave', 'applied_by', 'days_taken')
                ->where('type_of_leave', $request->filter_type_of_leave)
                ->where('applied_by', $request->filter_applied_by)
                ->get();
        }
        else
        {
            $data = DB::table('leaves')
                ->select('paynumber', 'type_of_leave', 'applied_by', 'days_taken')
                ->get();
            //dd(datatables()->of($data)->make(true));
        }

        return datatables()->of($data)->make(true);

    }

    $leaveslist = DB::table('leaves')
        ->select('paynumber', 'type_of_leave', 'days_taken','date_from','date_to','applied_by')
        ->orderBy('created_at', 'DESC')
        ->get();
//dd($leaveslist);
    return view('dashboard', compact('leaveslist'));
}

Firefox控制台出现了一些错误,但我认为它们不会影响任何事情:

enter image description here

任何帮助将不胜感激。我尝试过thisthisthis,但没有运气,perharps我错过了一些东西。

更新: 1.我删除了foreach语句。 2.确保jQuery已加载,在数据表之前加载了1st,因为如果它们在任何其他JS之后加载,则会出现错误。 3.按照建议设置一个部分。

现在正在加载数据,但将其压缩在一起,必须是Bootstrap ??,现在正在处理该数据 enter image description here

2 个答案:

答案 0 :(得分:0)

问题是JS和CSS冲突。

(在评论中由asker回答)

答案 1 :(得分:0)

知道了,这是JS和CSS冲突的问题