表未呈现(Yajra数据表)[已解决]

时间:2019-06-01 08:27:16

标签: laravel laravel-5 yajra-datatable laravel-datatables

所以,这是我的控制器

use Yajra\DataTables\Facades\DataTables;
class CategoriasController extends Controller
{
    public function index()
    {
        return Datatables::collection(Categoria::all())->make(true);
    }
 [...]
}

这是我的路线

Route::group(['middleware' => 'auth'], function () {
    Route::group(['middleware' => 'admin'], function () {   
        Route::prefix('admin')->namespace('Admin')->group(function () {
            Route::prefix('categorias')->group(function(){
                Route::get('/', 'CategoriasController@index')->name('curso.index');
[...]
             });
         });
     });
});

这是我的刀片

[...]
<div class="container">
    <table class="table border" id="myTable">
        <thead>
            <tr>
                <th>id</th>
                <th>Nome</th>
                <th>Criado</th>
                <th>Modificado</th>
            </tr>
        </thead>
    </table>
</div>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready( function () {
    $('#myTable').DataTable({
        processing: true,
        serverSide: true,
        ajax: '{!! route('categoria.index') !!}',
        columns: [
                { data: 'id', name:'id'},
                { data: 'nome', name:'nome'},
                { data: 'created_at', name:'created_at'},
                { data: 'updated_at', name:'updated_at'},
            ]
    });
} );
</script>
[...]

这是我的回报:

Print

我正在使用Laravel 5.8.19

composer.json

[...]
"require":{
        "yajra/laravel-datatables": "^1.5",
        "yajra/laravel-datatables-oracle": "~9.0"
}

那为什么不渲染表格呢?我做错了吗? 我已经上网了,但没有找到解决方案,希望您能帮到我

2 个答案:

答案 0 :(得分:0)

您应该为类别创建一个数据表类,并在控制器上使用

在您的数据表类上:

namespace App\DataTables;

use Illuminate\Support\Facades\App;
use Yajra\DataTables\Services\DataTable;

class CategoriasDataTable extends DataTable
{
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     *
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        return datatables($query);
    }

    /**
     * Get query source of dataTable.
     *
     * @param App\Models\Category $model
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(Category $model)
    {
        return $model
            ->newQuery()
            ->select('*');
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\DataTables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->addAction(['width' => '100px'])
                    ->parameters($this->getBuilderParameters());
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            [
                'data'  => 'id',
                'name'  => 'id',
                'title' => 'Id',
            ],
            [
                'data'  => 'name',
                'name'  => 'name',
                'title' => 'Name',
            ],
            [
                'data'  => 'created_at',
                'name'  => 'created_at',
                'title' => 'Created At',
            ],
            [
                'data'  => 'updated_at',
                'name'  => 'updated_at',
                'title' => 'Updated At',
            ],
        ];
    }
}

在您的控制器上:

use App\DataTables\UsersDataTable;
class CategoriasController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @param App\DataTables\CategoriasDataTable $datatable
     *
     * @return \Illuminate\Http\Response
     */
    public function index(CategoriasDataTable $datatable)
    {
        return $datatable->render('admin.categorias.index');
    }
 .......

希望这对您有帮助

答案 1 :(得分:0)

我的问题是调用控制器函数时,它直接返回了数据。我通过输入条件请求(如果请求是ajax)解决了问题,因此,当首次调用该页面时,它将返回视图,并且在加载并且jquery再次发出请求时,控制器将返回数据。

现在

我的控制器

use Yajra\DataTables\Facades\DataTables;

class CategoriasController extends Controller
{
    public function index()
    {
        if(request()->ajax()){
            return Datatables::collection(Categoria::all())->make(true);
        }

        return view('admin.categorias.index');
    }
 [...]
}