ASCII码 ASCII码

laravel yajra插件 datatable的使用详解

发布于:2022-03-25 09:31:11  栏目:技术文档

这篇文章主要介绍了laravel yajra插件 datatable的使用详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

安装laravel框架

命令行cd进入指定目录下,执行composer create-project --prefer-dist laravel/laravel datatable

在指定目录下创建最新的laravel项目框架安装yajra插件

命令行cd进入项目根目录下,执行

composer require yajra/laravel-datatables-oracle

安装yajra datatables软件包发布yajra datatables软件包

打开config/app.php文件,修改providers和aliases配置

'providers' => [ .... Yajra\DataTables\DataTablesServiceProvider::class, ] 'aliases' => [ .... 'DataTables' => Yajra\DataTables\Facades\DataTables::class, ]

  1. <!DOCTYPE html>
  2. <html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Laravel-datatable</title>
  7. <!-- Fonts -->
  8. <link href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap" rel="external nofollow" rel="stylesheet">
  9. <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.23/datatables.min.css" rel="external nofollow" />
  10. <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
  11. <script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.23/datatables.min.js"></script>
  12. <style>
  13. body {
  14. font-family: 'Nunito';
  15. }
  16. </style>
  17. </head>
  18. <body class="antialiased">
  19. {{\Carbon\Carbon::now()}}
  20. <table id="example">
  21. <thead>
  22. <tr>
  23. <th></th>
  24. <th>姓名</th>
  25. <th>生日</th>
  26. <th>性别</th>
  27. <th>工作</th>
  28. <th>电话</th>
  29. <th>邮箱</th>
  30. <th>地址</th>
  31. </tr>
  32. </thead>
  33. </table>
  34. </body>
  35. <script>
  36. $(document).ready(function (){
  37. let datatable = $('#example').DataTable({
  38. searching:false,
  39. paging:false,
  40. ajax:{
  41. url:"{{route('getData')}}",
  42. },
  43. columns:[
  44. {
  45. data:"id",
  46. name:"id",
  47. },
  48. {
  49. data:"name",
  50. name:"name",
  51. },
  52. {
  53. data:"birthday",
  54. name:"birthday",
  55. },
  56. {
  57. data:"sex",
  58. name:"sex",
  59. },
  60. {
  61. data:"job",
  62. name:"job",
  63. },
  64. {
  65. data:"tel",
  66. name:"tel",
  67. },
  68. {
  69. data:"email",
  70. name:"email",
  71. },
  72. {
  73. data:"address",
  74. name:"address",
  75. },
  76. ],
  77. });
  78. });
  79. </script>
  80. </html>

创建控制器

cmd执行

php artisan make:controller DatatableController

设定路由并编辑控制器

  1. //web.php文件
  2. Route::get('/datatable',[App\Http\Controllers\DatatableController::class,'index']);
  3. Route::get('/datatable',[App\Http\Controllers\DatatableController::class,'getData'])->name('getData');
  4. //控制器
  5. <?php
  6. namespace App\Http\Controllers;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\DB;
  9. class DatatableController extends Controller
  10. {
  11. public function index(){
  12. return view('welcome');
  13. }
  14. public function getData(){
  15. $datas = DB::table('user')->select('*')->get();
  16. return datatables()->of($datas)
  17. ->editColumn('id', '<input type="hidden" value="{{$id}}"><input type="checkbox" name="select">')->editColumn('name', '{{$name}}')
  18. ->editColumn('birthday', '{{$birthday}}')->editColumn('sex', '{{$sex}}')
  19. ->editColumn('job', '{{$job}}')->editColumn('tel', '{{$tel}}')
  20. ->editColumn('email', '{{$email}}')->editColumn('address', '{{$address}}')
  21. ->escapeColumns([])->make(true);
  22. }
  23. }

效果图

到此这篇关于laravel yajra插件 datatable的使用详解的文章就介绍到这了。

相关推荐
阅读 +