dongshi9719 2017-12-19 14:07
浏览 43
已采纳

在动态选项卡中检索数据

here i'm trying to click on any tabs to retrieve it's own data i did it well and it really retrieves the data of one raw only ?! any help?

<div class="col-lg-12">
      <div class="tabs-container">
        <ul class="nav nav-tabs">
          @foreach($departments as $department)
          <li class=""><a data-toggle="tab" href="#tab-{{ $department->id }}" aria-expanded="false">{{ $department->name }}</a></li>
          @endforeach


        </ul>
        <div class="tab-content">
          @foreach($works as $work)
          <div id="tab-{{ $work->department_id }}" class="tab-pane">
            <div class="panel-body">
              <div class="row employees">

                <div class="col-md-3">
                  <div class="thumbnail">
                    <div class="image view view-first"> <img class="img-responsive" src="" name="imaged" alt="image"> </div>
                    <div class="caption">
                      <h4>{{ $work->address }}  </h4>
                      <p>{{ $work->body }}</p>
                    </div>
                  </div>
                  <a class="btn btn-primary" href="edit-portfolio.html"> تعديل</a>
                  <button class="btn btn-danger demo4"> حذف
                  </button>
                </div>


              </div>
            </div>
          </div>
          @endforeach
        </div>
      </div>
    </div>
    <!--  -->
  </div>

i don't need to make it ajax if anyone have a good reason for why it give me only one record for each department?

  • 写回答

1条回答 默认 最新

  • doushaiyu5065 2017-12-19 16:07
    关注

    The reason why you get one record for department is that your loop create one tab panel per work, for example if you have 2 works (1,2) belongs to one department (1) the result would be 2 tabs with the same id set to 1:

    departments:

    id     
    -----
    1
    

    works:

    id      department_id
    ---------------------
    1       1
    2       1
    

    Html result:

    <div id="tab-1" class="tab-pane">
        ...
    </div>
    <div id="tab-1" class="tab-pane">
        ...
    </div>
    

    note that both tabs have the same id tab-1.

    Solution

    group works by department_id, something like this:

     $works = Work::all()->groupBy('department_id');
    

    and then in your view:

    <div class="tab-content">
        @foreach($works as $department_id => $department_works)
            <div id="tab-{{ $department_id }}" class="tab-pane">
                <div class="panel-body">
                    <div class="row employees">
                        @foreach($department_works as $work)
                        <div class="col-md-3">
                            <div class="thumbnail">
                                <div class="image view view-first"> <img class="img-responsive" src="" name="imaged" alt="image"> </div>
                                <div class="caption">
                                    <h4>{{ $work->address }}  </h4>
                                    <p>{{ $work->body }}</p>
                                </div>
                            </div>
                            <a class="btn btn-primary" href="edit-portfolio.html"> تعديل</a>
                            <button class="btn btn-danger demo4"> حذف </button>
                        </div>
                        @endforeach
                    </div>
                </div>
            </div>
        @endforeach
    </div>
    

    Hope this helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件