搜索,排序和分页不适用于数据表Laravel [英] Searching, sorting and paging not working for datatable laravel

查看:86
本文介绍了搜索,排序和分页不适用于数据表Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I'm trying to use the datatable as seen in my code written below. Unfortunately for some unknown reasons the functions for sorting, searching and paging does not work. May I know what did I missed out or the reason for it not working?

@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

推荐答案

尝试一下:

<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>

在tbody下移动 @foreach 语句.它不起作用,因为您一直在< tbody> 内复制 tbody 而不是< tr> .

Move the @foreach statement under tbody. It's not working because you keep duplicating the tbody instead of <tr> inside the <tbody>.

这只是一个建议.不要在视图中执行 query .这不是一个好习惯.在控制器内执行所有查询.您可以将 Laravel关系用于 App \ User :: find($ log-> user_id)->名称.

And this is just a suggestion. Don't do the query at view. it's not a good practice. Do all the query inside controller. You can use Laravel Relationship for App\User::find($log->user_id)->name.

这篇关于搜索,排序和分页不适用于数据表Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆