【问题标题】:Preventing a hyperlink from looping with the rest of the table?防止超链接与表格的其余部分循环?
【发布时间】:2021-10-14 08:57:36
【问题描述】:

我有一个显示分组数据的表格,并有超链接形式的“按钮”,供用户查看有关整个分组数据的更多信息。但是,该按钮也会随着每一行循环,理想情况下我只希望它为每个组显示一次。我尝试在 @foreach 循环中移动,但不断收到请求的 ID 在此集合实例中不存在的错误。

例如,我试图让这些链接在每个组中只出现一次(如图所示,在 1 和 2 中出现一次)。

代码:

        <tr>
            @foreach ($grouped as $mealplan)
            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
              Recipe Information
            </th>
            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
              Meal Plan Info
            </th>
            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
              Print
            </th>
            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
              Archive
            </th>
          <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
            Recipe Name
          </th>
          <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
            Day
          </th>
        </tr>
      </thead>
      <tbody class="bg-white divide-y divide-gray-200">
      @foreach ($mealplan as $recipe)
        <tr>
          <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
             <a href="recipeinformation/{{$recipe->Recipe_ID}}" class="text-indigo-600 hover:text-indigo-900 mb-2 mr-2">More</a>
           </td>
           <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
              <a href="/MealPlanDisplay/modal/{{$recipe->MealPlan_ID}}" class="text-indigo-600 hover:text-indigo-900 mb-2 mr-2">Meal Plan Info</a>
            </td>
           <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
              <a href="printpreviewingredients/{{$recipe->MealPlan_ID}}" class="text-indigo-600 hover:text-indigo-900 mb-2 mr-2">Print Ingredient List</a>
            </td>
            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
               <a href="/Archive/{{$recipe->MealPlan_ID}}" class="text-red-600 hover:text-indigo-900 mb-2 mr-2">Archive</a>
             </td>
          <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
            {{$recipe->recipe_name}}
      </td>
      <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
        {{$recipe->Day}}
      </td>
    </tr>
  </br>
  @endforeach
    @endforeach

任何关于将循环移动到哪里的建议,或者如何让链接在表格中的每个组中只显示一次,我们将不胜感激。

【问题讨论】:

标签: php html css laravel


【解决方案1】:

进行这样的迭代的概念如下:

注意代码中的 cmets

<table>
    {{--/*Comment: iterate through the groups to show sub-groups*/--}}
    @foreach ($group as $sub_group)
        <thead>
            {{--/*Comment: the group head row is here*/--}}
        </thead>
        <tbody>
            {{--/*Comment: iterate through the sub-group to show its items*/--}}
            @foreach ($sub_group as $item)
                {{--/*Comment: put here a condition to filter items*/--}}
                {{--/*Comment: in your case you want the "MORE" link to only 
                appear in the first row so we will check iteration index. 
                Luckily, we have $loop variable in Laravel so we don't have to count
                the index manually. The condition will be like this:
                1- if loop is in round "1", we should print the link inside the <td>.
                2- if not we should print empty <td> */--}}

                {{--/*Comment: Print the "more" link only on the first iteration */--}}
                @if($loop->iteration == 1)
                    <td>{{--/*Comment: put the link here */--}}</td>
                @else
                    <td>{{--/*Comment: empty cell*/--}}</td>
                @endif
                {{--/*Comment: then here comes the rest of the <td>s */--}}
            @endforeach
        </tbody>
    @endforeach
</table>

这是对您的代码的编辑

<table class="table" style="magin-left:200px">
    @foreach ($grouped as $mealplan)
        <thead>
            <tr>
                <th scope="col"
                    class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                    Recipe Information
                </th>
                <th scope="col"
                    class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                    Meal Plan Info
                </th>
                <th scope="col"
                    class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                    Print
                </th>
                <th scope="col"
                    class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                    Archive
                </th>
                <th scope="col"
                    class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                    Recipe Name
                </th>
                <th scope="col"
                    class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                    Day
                </th>
            </tr>
        </thead>
        <tbody class="bg-white divide-y divide-gray-200">
            @foreach ($mealplan as $recipe)
                <tr>
                    {{-- Print the "more" link only on the first iteration --}}
                    @if ($loop->iteration == 1)
                        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                            <a href="recipeinformation/{{ $recipe->Recipe_ID }}"
                                class="text-indigo-600 hover:text-indigo-900 mb-2 mr-2">More</a>
                        </td>
                    @else
                        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium"></td>
                    @endif
                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                        <a href="/MealPlanDisplay/modal/{{ $recipe->MealPlan_ID }}"
                            class="text-indigo-600 hover:text-indigo-900 mb-2 mr-2">Meal Plan Info</a>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                        <a href="printpreviewingredients/{{ $recipe->MealPlan_ID }}"
                            class="text-indigo-600 hover:text-indigo-900 mb-2 mr-2">Print Ingredient
                            List</a>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                        <a href="/Archive/{{ $recipe->MealPlan_ID }}"
                            class="text-red-600 hover:text-indigo-900 mb-2 mr-2">Archive</a>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                        {{ $recipe->recipe_name }}
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                        {{ $recipe->Day }}
                    </td>
                </tr>
            @endforeach
        </tbody>
    @endforeach
</table>

Outcome of the code

【讨论】:

  • 虽然这段代码可能会解决问题,包括解释如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。
  • 感谢您的建议。我会在回答任何问题时考虑到这一点。这只是我第一次尝试在 stackoverflow 上进行交互。
猜你喜欢
  • 2021-12-18
  • 2010-09-08
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 2018-10-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多