【问题标题】:Searching, sorting and paging not working for datatable laravel搜索,排序和分页不适用于数据表 laravel
【发布时间】:2016-01-08 11:06:19
【问题描述】:

所以我正在尝试使用下面编写的代码中的数据表。不幸的是,由于一些未知的原因,排序、搜索和分页功能不起作用。我可以知道我错过了什么或它不起作用的原因吗?

@extends('app')
@section('header')
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.datatables.net/1.10.10/css/dataTables.bootstrap.min.css" rel="stylesheet">
@stop
@section('content')

<h1 class="page-header">View Log Information</h1>
<br/>

<div class="container">
    <div class="col-xs-12 col-md-12" style="overflow:auto">
    <table class="table table-hover" id="logTable">
        <thead>
        <tr>
            <th>
                User
            </th>

            <th>
                Log Description
            </th>
            <th>
                Time Stamp
            </th>
        </tr>
        </thead>
        @foreach ($logs as $log)
        <tbody>
            <tr>
                <td>
                    {{ App\User::find($log->user_id)->name }}
                </td>

                <td>
                    {{ $log->log_description }}
                </td>
                <td>
                    {{ date("F d Y - g:i a",strtotime("$log->created_at")) }}
                </td>
            </tr>
        </tbody>
        @endforeach
    </table>
    </div>
</div>

@stop

@section('scripts')
    <!-- Bootstrap Based Data Table Plugin Script-->
    <script src="http://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
    <script src="https://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.10/js/dataTables.bootstrap.min.js" type="text/javascript"></script>
    <script>
    $(document).ready(function() {
        $('#logTable').DataTable();
    } );
    </script>
@stop

【问题讨论】:

    标签: php laravel datatable


    【解决方案1】:

    试试这个:

    <table class="table table-hover" id="logTable">
        <thead>
        <tr>
            <th>
                User
            </th>
    
            <th>
                Log Description
            </th>
            <th>
                Time Stamp
            </th>
        </tr>
        </thead>
    
        <tbody>
            @foreach ($logs as $log)
            <tr>
                <td>
                    {{ App\User::find($log->user_id)->name }}
                </td>
    
                <td>
                    {{ $log->log_description }}
                </td>
                <td>
                    {{ date("F d Y - g:i a",strtotime("$log->created_at")) }}
                </td>
            </tr>
            @endforeach
        </tbody> 
    </table>
    

    将@foreach 语句移到tbody 下。它不起作用,因为您在&lt;tbody&gt; 中不断复制tbody 而不是&lt;tr&gt;。

    这只是一个建议。不要在视图中执行query。这不是一个好习惯。在控制器内执行所有查询。您可以将Laravel Relationship 用于App\User::find($log-&gt;user_id)-&gt;name。

    【讨论】:

    • 非常感谢! =) 啊,是的,感谢您提醒查询部分。是的控制器..谢谢=)
    猜你喜欢
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 2014-11-10
    • 2018-11-02
    相关资源
    最近更新 更多