【问题标题】:Vue Modal in foreach loop with laravel 5.2带有laravel 5.2的foreach循环中的Vue Modal
【发布时间】:2020-03-08 07:26:45
【问题描述】:

我有一个带有 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

为了使模态框与点击的行相对应,我需要进行哪些更改?

【问题讨论】:

  • 您需要接受 props 的模态并将它们用于您的组件模板。

标签: javascript laravel vue.js


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    @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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-26
      • 2021-04-27
      • 1970-01-01
      • 1970-01-01
      • 2017-05-20
      • 2021-06-19
      • 2015-06-16
      相关资源
      最近更新 更多