我如何制作带有许多foreach的嵌套表

时间:2019-03-14 16:12:04

标签: php html laravel foreach

我需要制作一个包含纯游览的报告,但是正如您在excel中可以看到的那样,但是它对我不起作用,我希望它看起来像这样 Expected image

但这就是我的最终目的

Resulting image

<table class="table">
        <thead>
          <tr>
            <th scope="col">Verificador</th>
            <th scope="col">ETAPA</th>
            <th scope="col">ACTIVIDAD GENERAL</th>
            <th scope="col">ACTIVIDAD</th>
            <th scope="col">PRODUCTO A ENTREGAR</th>
            <th scope="col">EVIDENCIA</th>
          </tr>
        </thead>
        <tbody>
            @foreach ($project->administrators as $administrator)
                <td>  {{$administrator->name}}</td>
                @foreach($project->phases as $phase)            
                    <td>{{$phase->name}}</td>

                    @foreach ($phase->activities as $activity)

                        @if ($activity->parent_id==null)

                            <td><p style="background: red;" >{{$activity->name}}</p></td>
                        @endif

                        @foreach($activity->activities as $especifics)
                                <td>{{$especifics->name}}</td>  
                            @foreach($especifics->products as $product)
                                 <td>{{$product->name}}</td>
                                @foreach($product->productdetails as $productdetail)
                                <td>{{$productdetail->name}}</td>
                                    @foreach($productdetail->evidence as $evidence )
                                    <td>{{$evidence->path}}</td>
                                    @endforeach
                                @endforeach    
                            @endforeach 
                        @endforeach    
                    @endforeach 
                @endforeach 

            @endforeach 
        </tbody>
</table>

2 个答案:

答案 0 :(得分:0)

在线进行一些研究可以帮助您解决问题,请仔细阅读以获取解决方案 https://www.yourhtmlsource.com/tables/nestingtables.html

使用服务器端语言对表格进行网状处理需要基本的html知识,例如,如果我可以使用您的代码,建议您像这样编辑它

    <table class="table">
    <thead>
      <tr>
        <th scope="col">Verificador</th>
        <th scope="col">ETAPA</th>
        <th scope="col">ACTIVIDAD GENERAL</th>
        <th scope="col">ACTIVIDAD</th>
        <th scope="col">PRODUCTO A ENTREGAR</th>
        <th scope="col">EVIDENCIA</th>
      </tr>
    </thead>
    <tbody>
        @foreach ($project->administrators as $administrator)
            <td> 
          //you may want to have the second table inside the first table here
                 <table>
                   <tr><td> {{$administrator->name}}</td>
            @foreach($project->phases as $phase)            
                <td>{{$phase->name}}</td>
                 </tr>
                 </table>            
             //close the second table

                @foreach ($phase->activities as $activity)

                    @if ($activity->parent_id==null)

                        <td><p style="background: red;" >{{$activity->name}}</p></td>
                    @endif

                    @foreach($activity->activities as $especifics)
                            <td>{{$especifics->name}}</td>  
                        @foreach($especifics->products as $product)
                             <td>{{$product->name}}</td>
                            @foreach($product->productdetails as $productdetail)
                            <td>{{$productdetail->name}}</td>
                                @foreach($productdetail->evidence as $evidence )
                                <td>{{$evidence->path}}</td>
                                @endforeach
                            @endforeach    
                        @endforeach 
                    @endforeach    
                @endforeach 
            @endforeach 

        @endforeach 
    </tbody>

我只添加了一张表,因为我正努力让您传递其他数据,但是正如您所看到的,嵌套主要要求一个表来了解将在现场wgat中放置的内容,就像您需要了解的那样

  1. 什么表将包含什么
  2. 什么是父表等等

答案 1 :(得分:0)

enter image description here希望这可以帮助您实现您真正想要的,但是我只是用HTML完成的。

<html>
<body>
    <h1>table in table
    <table border="2">
        <tr>
        <th>1</th>
        <th>2</th>
        <th>3</th>
        <th>4</th>
        </tr>
        <tr>
        <td>In head 1</td>
        <td>In head 2</td>
        <td>in head 3</td>
        <td>
            <table border="1">
                <tr>
                <td>In head 4.1</td>
                <td>In head 4.2</td>
                <td>In head 4.3</td>
                <td>In head 4.4</td>
                </tr>
                <tr>
                <td>11</td>
                <td>22</td>
                <td>33</td>
                <td>44</td>
                </tr>
            </table>
        </td>
        </tr>
        <tr>
        </tr>
    </table>
</body>

<html>