首页 > 解决方案 > 创建动态 HTML 表格

问题描述

如何使用刀片foreach循环创建动态表格 HTML,如下所示:

在此处输入图像描述

合并名称、父亲姓名和类型等相同的行。

我已经创建了我的表,但是它使第一条记录完成并且重置不完整,而没有合并这样的行:

在此处输入图像描述

我的观点

        <tbody>
        @php
            $rowid = 0;
            $rowspan = 0;
        @endphp
        @foreach($result as $key => $data)
            @php
                $rowid += 1
            @endphp
            <tr>
                @if ($key == 0 || $rowspan == $rowid)
                    @php
                        $rowid = 0;
                        $rowspan = $data->driver_name;
                    @endphp
            <td id="" rowspan="{{$result->count()}}"></td>
            <td id="" rowspan="{{$result->count()}}">{{$data->driver_name}}</td>
            <td id="" rowspan="{{$result->count()}}">{{$data->father_name}}</td>
            <td id="" rowspan="{{$result->count()}}">{{$data->vehicle_type}}</td>
            <td id="" rowspan="{{$result->count()}}">{{$data->plate_no}}</td>
            <td id="" rowspan="{{$result->count()}}">{{$data->serial_number}}</td>
            <td id="" rowspan="{{$result->count()}}">{{$data->description}}</td>
            <td id="" rowspan="{{$result->count()}}">{{$data->created_at}}</td>
            <td id="" rowspan="{{$result->count()}}">{{$data->order_number}}</td>
            <td id="" rowspan="{{$result->count()}}">{{$data->export_number}}</td>
                @endif
            <td id="">{{$data->buy_parts}}</td>
            <td id="">{{$data->quantity}}</td>
            <td id="">{{$data->per_item_price}}</td>
            <td id="">{{$data->total_price}}</td>
                    @if ($key == 0 || $rowspan == $rowid)
                        @php
                            $rowid = 0;
                            $rowspan = $data->driver_name;
                        @endphp
            <td id="" rowspan="{{$result->count()}}">{{$data->export_date}}</td>
            <td id="" rowspan="{{$result->count()}}">{{$data->remarks}}</td>
                        @endif
        </tr>
        @endforeach
        </tbody>

我的控制器:

    public function repair_parts()
    {
        $result=DB::table('parts_orders as a')
            ->select('b.emp_id','c.name as driver_name','c.father_name','d.vehicle_type','d.plate_no','b.id as serial_number'
                ,'b.description','b.created_at','a.order_number','a.export_number','a.buy_parts','a.per_item_price','a.quantity'
                ,'a.total_price','a.export_date','a.remarks')
            ->join('repair_cars as b','a.repair_id','=','b.id','left')
            ->join('drivers as c','b.emp_id','=','c.emp_id','left')
            ->join('vehicles as d','b.vechcal_id','=','d.plate_no','left')
            ->join('locations as e','b.location_id','=','e.id','left')
            ->get();
        return view('Reports/repair_parts_report',compact('result'));
    }

标签: htmllaraveleloquentlaravel-blade

解决方案


你可以试试这个方法。

数据数组

@php
        $rows = [

            [
                'remarks'   => 'remark',
                'date'  => '2019-07-09',
                'total' => [100,200,300],
                'quantity'  => [100,200,300],
                'partname'  => ['a','b','c'],
                'number'    => '123412',
                'ordernumber'   => 'RNX123',
                'orderdate' => '2019-07-09',
                'desc'  => 'some descriptioin',
                'saledate'  => '2019-07-09',
                'datenumber'    => '99884',
                'type'  => 'test',
                'father'    => 'father',
                'name'      =>  'name',
            ],
            [
                'remarks'   => 'remark',
                'date'  => '2019-07-09',
                'total' => [100,200,300],
                'quantity'  => [100,200,300],
                'partname'  => ['a','b','c'],
                'number'    => '123412',
                'ordernumber'   => 'RNX123',
                'orderdate' => '2019-07-09',
                'desc'  => 'some descriptioin',
                'saledate'  => '2019-07-09',
                'datenumber'    => '99884',
                'type'  => 'test',
                'father'    => 'father',
                'name'      =>  'name',
            ],
        ]
    @endphp

刀片部分。

<table border="1">
                <tr>
                    <th>Remarks</th>
                    <th>Date</th>
                    <th>Total</th>
                    <th>Quantity</th>
                    <th>Part Name</th>
                    <th>Number</th>
                    <th>Order Number</th>
                    <th>Order Date</th>
                    <th>Description</th>
                    <th>Sale Date</th>
                    <th>Date Number</th>
                    <th>Type</th>
                    <th>
                        <table border="1">
                            <tr><th colspan="2"> Urdu</th></tr>
                            <tr>
                                <th>Father</th>
                                <th>Name</th>
                            </tr>
                        </table>
                    </th>
                    <th>#</th>
                </tr>
            <tbody>
                @foreach($rows as $key => $row)
                <tr>
                    <td> {{$row['remarks']}}</td>
                    <td> {{$row['date']}}</td>
                    <td>
                         @foreach($row['total'] as $total)
                        <table border="1">
                        <tr>
                            <td>{{ $total}}</td>
                        </tr>
                        </table>
                        @endforeach 
                    </td>
                    <td>
                         @foreach($row['quantity'] as $quantity)
                        <table border="1">
                        <tr>
                            <td>{{ $quantity}}</td>
                        </tr>
                        </table>
                        @endforeach 
                    </td>
                    <td>
                         @foreach($row['partname'] as $partname)
                        <table border="1">
                        <tr>
                            <td>{{ $partname}}</td>
                        </tr>
                        </table>
                        @endforeach 
                    </td>
                    <td> {{$row['number']}}</td>
                    <td> {{$row['ordernumber']}}</td>
                    <td> {{$row['orderdate']}}</td>
                    <td> {{$row['desc']}}</td>
                    <td> {{$row['datenumber']}}</td>
                    <td> {{$row['type']}}</td>
                    <td> {{$row['father']}}</td>
                    <td> {{$row['name']}}</td>
                    <td> {{ $key+1}}</td>

                </tr>
                @endforeach
            </tbody>
        </table>

输出。

以上代码说明

我希望你能从中得到一些想法。


推荐阅读