【问题标题】:Create dynamic HTML table创建动态 HTML 表格
【发布时间】:2019-11-19 17:49:15
【问题描述】:

如何使用刀片 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'));
    }

【问题讨论】:

    标签: html laravel eloquent laravel-blade


    【解决方案1】:

    你可以试试这个方法。

    数据数组

    @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>
    

    输出。

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

    【讨论】:

    • 感谢您的重播,但添加 @foreach($row['quantity'] as $quantity) &lt;table border="1"&gt; &lt;tr&gt; &lt;td&gt;{{ $quantity}}&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; @endforeach 后显示“不能使用 stdClass 类型的对象作为数组”
    • 这是因为你有对象而不是数组,你可以像$row-&gt;quantity一样访问它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 2011-05-20
    • 1970-01-01
    相关资源
    最近更新 更多