php 具有服务类的Laravel数据表
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 具有服务类的Laravel数据表相关的知识,希望对你有一定的参考价值。
<?php
namespace App\DataTables;
use App\User;
use Yajra\DataTables\Services\DataTable;
class UsersDataTable extends DataTable
{
/**
* Build DataTable class.
*
* @param mixed $query Results from query() method.
* @return \Yajra\DataTables\DataTableAbstract
*/
public function dataTable($query)
{
return datatables($query)
->addColumn('action', $this->getActionButton());
}
/**
* Get query source of dataTable.
*
* @param \App\User $model
* @return \Illuminate\Database\Eloquent\Builder
*/
public function query(User $model)
{
return $model->newQuery()->select('id', 'name', 'email', 'created_at');
}
/**
* Optional method if you want to use html builder.
*
* @return \Yajra\DataTables\Html\Builder
*/
public function html()
{
return $this->builder()
->columns($this->getColumns())
->minifiedAjax()
->addColumn([
// 'name' => 'roles',
'data' => 'id',
'title' => 'Roles',
'render' => 'function(){
return data;
}',
])
->addAction(['width' => '80px'])
->parameters($this->getBuilderParameters());
}
/**
* Get columns.
*
* @return array
*/
protected function getColumns()
{
return [
'id',
'name',
'email',
'created_at',
// 'roles',
];
}
/**
* Get filename for export.
*
* @return string
*/
protected function filename()
{
return 'Users_' . date('YmdHis');
}
protected function getBuilderParameters()
{
return [
'dom' => 'Bfrtip',
'order' => [[0, 'desc']],
'buttons' => [
// 'export',
'print',
'reload',
],
];
}
private function getActionButton()
{
$edit_route = '/admin/users/{{$id}}/edit';
$action = '<div class="btn-group">
<a href="' . $edit_route . '" class="btn btn-sm btn-primary" data-toggle="tooltip" title="Edit">
<i class="fa fa-pencil-alt"></i>
</a>
<button type="button" class="btn btn-sm btn-danger delete" data-toggle="tooltip" title="Delete">
<i class="fa fa-times"></i>
</button>
</div>';
return $action;
}
}
<?php
namespace App\Http\Controllers\Admin;
use App\DataTables\UsersDataTable;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;
use Yajra\DataTables\Facades\DataTables;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(UsersDataTable $dataTable)
{
return $dataTable->render('admin.users.index');
}
}
@extends('layouts.backend')
@section('css_before')
<link rel="stylesheet" href="{{ asset('js/plugins/datatables/dataTables.bootstrap4.css') }}">
<link rel="stylesheet" href="{{ asset('js/plugins/datatables/buttons-bs4/buttons.bootstrap4.min.css') }}">
@endsection
@section('js_after')
<!-- Page JS Plugins -->
<script src="{{ asset('js/plugins/datatables/jquery.dataTables.min.js') }}"></script>
<script src="{{ asset('js/plugins/datatables/dataTables.bootstrap4.min.js') }}"></script>
<script src="{{ asset('js/plugins/datatables/buttons/dataTables.buttons.min.js') }}"></script>
<script src="{{ asset('js/plugins/datatables/buttons/buttons.print.min.js') }}"></script>
<script src="{{ asset('js/plugins/datatables/buttons/buttons.html5.min.js') }}"></script>
<script src="{{ asset('js/plugins/datatables/buttons/buttons.flash.min.js') }}"></script>
<script src="{{ asset('js/plugins/datatables/buttons/buttons.colVis.min.js') }}"></script>
<script src="/vendor/datatables/buttons.server-side.js"></script>
<!-- Page JS Code -->
{!! $dataTable->scripts() !!}
@endsection
@section('content')
<!-- Hero -->
<div class="bg-body-light">
<div class="content content-full">
<div class="d-flex flex-column flex-sm-row justify-content-sm-between align-items-sm-center">
<h1 class="flex-sm-fill font-size-h2 font-w400 mt-2 mb-0 mb-sm-2">Manage Users</h1>
<nav class="flex-sm-00-auto ml-sm-3" aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item">Examples</li>
<li class="breadcrumb-item active" aria-current="page">Blank</li>
</ol>
</nav>
</div>
</div>
</div>
<!-- END Hero -->
<!-- Page Content -->
<div class="content">
<!-- Your Block -->
<div class="block block-rounded block-bordered">
<div class="block-header block-header-default">
<h3 class="block-title">List Of Users</h3>
</div>
<div class="block-content">
{!! $dataTable->table() !!}
</div>
</div>
<!-- END Your Block -->
</div>
<!-- END Page Content -->
@endsection
Route::prefix('admin')->name('admin.')->group(function () {
// users routes
Route::get('users', 'Admin\UserController@index')->name('users.index');
});
以上是关于php 具有服务类的Laravel数据表的主要内容,如果未能解决你的问题,请参考以下文章