1

我有一个带有 foreach 循环的 laravel 刀片,成功构建了一个包含数据的表,每一行都有一个链接来触发模式。我现在的问题是,当我单击一个链接时,它会在每一行上触发一个模式,我希望它特定于单击的行并在其中显示该行的信息。这是我现在拥有的:

@section('content')

<div id="app">
<table class="uk-table uk-table-hover">
    <thead id="table-header">
        <tr>
            <th> Number</th>
            <th> Name</th>
            <th> Updated</th>
        </tr>
    </thead>
    <tbody>
        @foreach($results as $result)
        <tr class="" id="">
            <td>
                <a id="show-modal" @click="showModal = true">{{$result['number']}}</a>
                <modal v-if="showModal" @close="showModal = false">
            </td>
            <td>
                {{$result['name']}}
            </td>
            <td>
                {{$result['updated']}}
            </td>
        </tr>
        @endforeach
    </tbody>
</table>


<!--  Modals  -->



<!-- End container -->
</div>
@endsection


@section('loadjs')
@include('js.datatables')

<script type="text/javascript">
    Vue.component('modal',{
        template: '#modal-template'
    })

    new Vue({
        el:'#app',
        data: { 
            showModal: false
        }
    })
</script>

<!-- Modals -->
<script type="text/x-template" id="modal-template">
  <transition name="modal">
    <div class="modal-mask">
      <div class="modal-wrapper">
        <div class="modal-container">

          <div class="modal-header">
            <slot name="header">
              default header
            </slot>
          </div>

          <div class="modal-body">
            <slot name="body">
              default body
            </slot>
          </div>

          <div class="modal-footer">
            <slot name="footer">
              default footer
              <button class="modal-default-button" @click="$emit('close')">
                OK
              </button>
            </slot>
          </div>
        </div>
      </div>
    </div>
  </transition>
</script>

@endsection

为了使模态与单击的行相对应,我究竟需要更改什么?

4

2 回答 2

1

您可以轻松地做到这一点,只需$results在循环中获取数组的键foreach并按以下方式使用它 -

@section('content')

<div id="app">
<table class="uk-table uk-table-hover">
    <thead id="table-header">
        <tr>
            <th> Number</th>
            <th> Name</th>
            <th> Updated</th>
        </tr>
    </thead>
    <tbody>
        @foreach($results as $k => $result)
        <tr class="" id="">
            <td>
                <a id="show-modal" @click="showModal = {{$k}}">{{$result['number']}}</a>
                <modal v-if="showModal==={{$k}}" @close="showModal = false">
            </td>
            <td>
                {{$result['name']}}
            </td>
            <td>
                {{$result['updated']}}
            </td>
        </tr>
        @endforeach
    </tbody>
</table>


<!-- End container -->
</div>
@endsection

....

<script type="text/javascript">
    Vue.component('modal',{
        template: '#modal-template'
    })

    new Vue({
        el:'#app',
        data: { 
            showModal: false
        }
    })
</script>

@endsection
于 2019-11-12T18:48:53.103 回答
0

自从我使用 Vue 以来已经有一段时间了,但看起来你正在将行鞋绑定到所有行都将共享的变量。因此,当您将一行设置为 true 时,它​​会将所有行设置为 true 并且它们打开。

重新设计您的 Modal 组件并将打开行的逻辑移到其中可能会更好,这样您就不会尝试直接从表中管理这么多行,如果这有意义的话。

让每一行(组件)在单击时处理打开,而不是试图将所有逻辑保留在表上。

于 2019-11-12T16:45:56.273 回答