博客列表 >datatable 配置演示、后台编辑、多语言设置

datatable 配置演示、后台编辑、多语言设置

赵大叔
赵大叔原创
2022年03月12日 16:41:14715浏览

datatable 配置演示

  • 复制示例源码
  • 引入相关 css、js 文件
  • 设置配置项,渲染数据

渲染数据

  1. $(function () {
  2. $("#example1").DataTable({
  3. responsive: true,
  4. autoWidth: false,
  5. language: {
  6. processing: "处理中...",
  7. lengthMenu: "显示 _MENU_ 项结果",
  8. zeroRecords: "没有匹配结果",
  9. info: "显示第 _START_ 至 _END_ 项结果,共 _TOTAL_ 项",
  10. infoEmpty: "显示第 0 至 0 项结果,共 0 项",
  11. infoFiltered: "(由 _MAX_ 项结果过滤)",
  12. infoPostFix: "",
  13. search: "搜索:",
  14. searchPlaceholder: "搜索...",
  15. url: "",
  16. emptyTable: "表中数据为空",
  17. loadingRecords: "载入中...",
  18. infoThousands: ",",
  19. paginate: {
  20. first: "首页",
  21. previous: "上页",
  22. next: "下页",
  23. last: "末页",
  24. },
  25. aria: {
  26. paginate: {
  27. first: "首页",
  28. previous: "上页",
  29. next: "下页",
  30. last: "末页",
  31. },
  32. sortAscending: "以升序排列此列",
  33. sortDescending: "以降序排列此列",
  34. },
  35. thousands: ".",
  36. },
  37. stateSave: true,
  38. stateDuration: 0,
  39. paging: "true",
  40. rowId: "cou_id",
  41. ajax: {
  42. url: "getDataTable",
  43. type: "POST",
  44. dataType: "JSON",
  45. dataSrc: "tableData",
  46. },
  47. serverSide: true,
  48. columns: [
  49. {
  50. data: "cou_id",
  51. name: "课程编号",
  52. },
  53. {
  54. data: "title",
  55. name: "课程标题",
  56. orderable: false,
  57. width: "150px",
  58. },
  59. {
  60. data: "pic",
  61. name: "课程封面",
  62. orderable: false,
  63. render: function (data, type, row, meta) {
  64. return "<img style='width:200px' src='" + row.pic + "'>";
  65. },
  66. },
  67. {
  68. data: "info",
  69. name: "课程简介",
  70. orderable: false,
  71. width: "550px",
  72. },
  73. {
  74. targer: 4,
  75. orderable: false,
  76. render: function (data, type, row, meta) {
  77. var html = "";
  78. html +=
  79. '<a class="btn btn-primary" href="edit?id=' +
  80. row.cou_id +
  81. '"><i class="fas fa-edit" title="编辑"></i></a>';
  82. html += "&nbsp;&nbsp;";
  83. html +=
  84. '<a style="color:#fff" class="btn btn-danger" onclick="layer.confirm(\'移除后列表将不显示,可以从回收站找回?\', function(index){recycle(\'' +
  85. row.cou_id +
  86. '\');layer.close(index);}); "><i class="fas fa-recycle" title="移到回收站"></i></a>';
  87. return html;
  88. },
  89. },
  90. ],
  91. });
  92. });

回收课程

  1. function recycle(cou_id) {
  2. $.ajax({
  3. url: "recycle",
  4. type: "POST",
  5. dataType: "json",
  6. data: { id: cou_id },
  7. success: (res) => {
  8. if (res.code == 1) {
  9. $("#" + cou_id).remove();
  10. layer.msg(res.msg, { icon: 1 });
  11. } else {
  12. layer.msg(res.msg, { icon: 5 });
  13. }
  14. },
  15. });
  16. }

多语言配置

  • 打开中间件多语言配置: 全局middleware.php,添加\think\middleware\LoadLangPack::class,;
  • html 中使用{$Think.lang.user.home}引用语言变量

语言包

  1. <?php
  2. return [
  3. 'user' => [
  4. 'welcome' => 'Welcome back',
  5. 'login' => 'Sign up',
  6. 'logout' => 'Log out',
  7. 'register' => 'Sign in',
  8. 'home'=>'home',
  9. 'en'=>'en',
  10. 'cn'=>'cn'
  11. ],
  12. 'webtitle' => 'PBack-stage Management'
  13. ];
  14. return [
  15. 'user' => [
  16. 'welcome' => '欢迎回来',
  17. 'login' => '用户登录',
  18. 'logout' => '用户登出',
  19. 'register' => '用户注册',
  20. 'home'=>'首页',
  21. 'en'=>'英文',
  22. 'cn'=>'中文'
  23. ],
  24. 'webtitle' => '后台管理'
  25. ];

多语言参数,cookie 设置

  1. public function langswitch()
  2. {
  3. $lang = input('get.lang') ?? 'zn';
  4. // dd($lang);
  5. $lang = strtolower($lang);
  6. switch ($lang) {
  7. case 'zn':
  8. cookie('think_lang', 'zh-cn');
  9. break;
  10. case 'en':
  11. cookie('think_lang', 'en-us');
  12. break;
  13. default:
  14. cookie('think_lang', 'zh-cn');
  15. break;
  16. }
  17. }

[http://help10086.cn/admin/home]
测试账号密码:(amdin,123456)

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议