duanliyi5997 2015-09-17 04:47
浏览 26
已采纳

一般用于设定列数的子数据和foreach的方法

I have a general approach question, although my code is in Laravel Blade. I have data with child data. Each Parent record is represented by row, but each child data is to be displayed in the same row, just in adjacent columns. There are a set number of columns. Each parent will have varying child records. But child data needs to be displayed in particular columns.

I can't do this:

foreach($children as $data)
    if(data belongs)
        show content
    else
        show blank form
    endif
endforeach

otherwise i'll have fewer td's than needed.

The best I came up with is:

<tr>
    <td> Location </td>
    <td>
        $colA = false

        @foreach($children as $data)
            @if(child belongs in column A)
                {{$child->content}}

                @colA = true
            @endif()
        @end foreach

        @if($colA == false)
            display blank form input
        @endif()

    </td>
    <td>
        $colB = false

        @foreach($children as $data)
            @if(child belongs in column B)
                {{$child->content}}

                @colB = true
            @endif()
        @end foreach

        @if($colB == false)
            display blank form input
        @endif()

    </td>


//-------repeat for 6 more times


</tr>

but this seems really inefficient. Especially since i'll have up to a hundred parent records being displayed at once each with 8 "children-columns". So the foreach loop would run 800 times....

Any help at making this more efficient would be greatly appreciated.

Thank you

  • 写回答

1条回答 默认 最新

  • dongna2498 2015-09-17 21:20
    关注

    You could pre-process your data in one pass to order it for easier consumption by your template.

    So in your route/controller/whatever before the template (or alternatively inside the template):

    $preProcessed = [];
    for ($parents as $parent) {
        $orderedChildren = [];
        for ($parent->children as $child) {
            $childColumnIndex = ... <-- determine which column this child should be in
            $orderedChildren[$childColumnIndex] = $child;
        }
        $preProcessed[] = [$parent, $orderedChildren];
    }
    

    Then in your template, looping over pairs of [parent, orderedChildren]:

    <tr>
        <td> Location </td>
        <td>
            @if(isset($orderedChildren[0]))
                $orderedChildren[0]->content();
            @else
                display blank form input
            @endif
        </td>
        [other columns]
    </tr>
    

    Actually, at this point, you can also loop on the column indices instead of copying the html for each one.

    There's actually an even better approach available if you can order the children arrays when doing the database query. If you can do this, you can remove the pre-ordering step but the template rendering code will be more complicated. In pseudo-code:

    for each parent:
        $nextChildIndex = 0
        for each column:
            if ($nextChildIndex < length($parent->children) &&
                $parent->children[$nextChildIndex] belongs to this column)
    
                $parent->children[$nextChildIndex]->content();
                $nextChildIndex++;
            else
                display blank form input
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条