laravel根据数据动态生成表头和表格数据

约定不等于承诺〃 2024-04-17 05:45 173阅读 0赞

需求:根据数据动态配置表头,动态适配表头数据
如下图,两个表的表头和数据一一对应,都为动态生成;
完成功能思想:1.找出数据中的存在的最长表头,即所有数据都为表头的子集表头
2.渲染数据时候,根据表头的字段进行帅选要显示的数据(用循环判断实现)
在这里插入图片描述

在这里插入图片描述

后端配置数据:

  1. public function show($productid)
  2. {
  3. $products = Product::orderBy('created_at', 'desc')->get(); //发送给前端
  4. $versions = Versionindex::where('product_id', $productid)->with('versiondetail', 'versionatt')->get();
  5. $version_ids = $versions->pluck('id');
  6. $version_types = VersionGroup::where('product_id', $productid)->get();//模板配置表头
  7. $att_keys = VersionAttache::whereIn('versionindex_id', $version_ids)->groupBy('key')->get();//自定义配置表头
  8. return view('admin.hwtc.vers.show', compact('products', 'productid', 'version_types', 'att_keys', 'versions'));
  9. }

前端数据帅选:

  1. <table class="layui-table">
  2. <thead>
  3. <tr>
  4. <th>序号</th>
  5. @foreach($version_types as $type)
  6. <th>{
  7. {$type->version_name}}</th>
  8. @endforeach
  9. @foreach($att_keys as $key)
  10. <th>{
  11. {$key->key}}</th>
  12. @endforeach
  13. <th>创建者</th>
  14. <th>创建时间</th>
  15. <th>是否启用</th>
  16. <th>是否冻结</th>
  17. <th>操作</th>
  18. </tr>
  19. </thead>
  20. <tbody>
  21. @foreach($versions as $k=>$item)
  22. <tr data-id="{
  23. {$item->id}}">
  24. <td>{
  25. {$k+1}}</td>
  26. @foreach($version_types as $type)
  27. @foreach($item->versiondetail as $detail)
  28. @if($detail->version_type == $type->version_type)
  29. <td>{
  30. {@$detail->version_value}}</td>
  31. @endif
  32. @endforeach
  33. @endforeach
  34. @foreach($att_keys as $key)
  35. @foreach(@$item->versionatt as $att)
  36. @if($att->key == $key->key)
  37. <td>{
  38. {@$att->value}}</td>
  39. @endif
  40. @endforeach
  41. @if(@$item->versionatt->pluck('key')->contains($key->key))
  42. @else
  43. <td></td>
  44. @endif
  45. @endforeach
  46. <td>{
  47. {$item->created_by}}</td>
  48. <td>{
  49. {$item->created_at}}</td>
  50. <td>{!! is_status($item->enabled,'enabled') !!}</td>
  51. <td>@if($item->freezed == 1)<i class="layui-icon is_something" data-attr="freezed"></i> @elseif($item->freezed == 0)
  52. <i class="layui-icon is_something" data-attr="freezed"></i> @endif
  53. </td>
  54. <td class="td-manage">
  55. <a lay-tips="编辑版本"
  56. onclick="WeAdminShow('编辑版本','{
  57. {route('hwtc.versionindex.edit',$item->id)}}')"
  58. href="javascript:;">
  59. <i class="layui-icon"></i>
  60. </a>
  61. <a lay-tips="删除版本" href="{
  62. {route('hwtc.versionindex.destroy', $item->id)}}"
  63. data-method="delete"
  64. data-token="{
  65. {csrf_token()}}" data-confirm="确认删除吗?"
  66. class="btn btn-white btn-xs" lay-event="del"><i
  67. class="layui-icon"></i></a>
  68. </td>
  69. </tr>
  70. @endforeach
  71. </tbody>
  72. </table>

发表评论

表情:
评论列表 (有 0 条评论,173人围观)

还没有评论,来说两句吧...

相关阅读