2

所以,这是我的控制器

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>
[...]

我得到了这个作为回报:

打印

我正在使用 Laravel5.8.19

作曲家.json

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

那么为什么不是表格渲染呢?是我做错了什么吗?我跑了互联网,我没有找到解决方案,希望你能帮助我

4

2 回答 2

1

我的问题是,当调用控制器函数时,它直接返回数据。如果请求是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');
    }
 [...]
}
于 2019-06-02T04:46:49.093 回答
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');
    }
 .......

我希望这能帮到您

于 2019-06-01T10:40:04.870 回答