在 vuejs 和 laravel 的循环中显示特定的 div `@click`
Show specific div `@click` in a loop in vuejs and laravel
我试图在点击时显示隐藏的 dive,但是我在使用循环时遇到了麻烦,其中我的所有 divs 都是动态的。当我单击按钮时,它显示所有 div,但我想在单击时显示特定的单个 div。我试过这样的东西 -
index.blade.php
<div class="col-md-12" id="questionsection">
@foreach ($data['a'] as $row)
<div class="row">
<div class="col-sm-2"></div>
<div class="col-sm-10">
<button @click='myFilter($event)'>Comment</button>
<div v-bind:class="{ noActive: isActive }">
<form action="#" method="POST">
<textarea class="textarea" name="answer"></textarea>
<button>Save</button>
</form>
</div>
</div>
</div>
@endforeach
</div>
.noActive{ display:none}
而在 vuejs 中 script
是-
<script>
var vm = new Vue({
el: '#myApp',
data: {
isActive: true,
},
methods: {
myFilter: function (event){
this.isActive = !this.isActive;
}
}
})
</script>
项目数组是在 blade 中渲染的,所以当它到达前端时,Vue 并不知道它。
此外,isActive
对应用程序组件是全局的,因此它适用于所有项目。
您需要将数组作为 prop 传递给您的 vue 组件,然后使用 v-for
.
对其进行循环
<div class="row" v-for="(item, index) in {{ data['a'] }}" :key="index">
<!-- same body of the loop as before -->
</div>
然后将index和item添加到myFilter($event, index, item)
函数中,更新数组中对应的item。
您将需要使用列出的方法之一更新项目 in the doc。
如果您不想将它移动到它自己的组件中,那么您将需要一个唯一的标识符来显示循环中的哪个 div 是活动的。您当前的设置无法知道哪个 div 已被点击,您只是切换一个标志,这意味着 div 中的全部或 none 被显示。
解决这个问题的一种方法是使用 foreach
循环的索引,例如
@foreach($data['a'] as $key => $row)
然后对于您的 vue 实例,您可以:
<script>
var vm = new Vue({
el: '#myApp',
data: {
activeKey: null,
},
methods: {
isActive(i) {
return this.activeKey === i;
},
toggleActive(i) {
this.activeKey = this.isActive(i) ? null : i;
}
}
})
</script>
由于逻辑略有变化,您需要更新 blade 文件中的几行:
<button @click='toggleActive($key)'>Comment</button>
<div v-bind:class="{ noActive: isActive($key) }">
这种方法适用于非常小的项目或 vue 使用不多的项目,但是,如果不是这种情况,我建议将其重构为一个组件。
https://laracasts.com/series/learn-vue-2-step-by-step/episodes/7
我试图在点击时显示隐藏的 dive,但是我在使用循环时遇到了麻烦,其中我的所有 divs 都是动态的。当我单击按钮时,它显示所有 div,但我想在单击时显示特定的单个 div。我试过这样的东西 -
index.blade.php
<div class="col-md-12" id="questionsection">
@foreach ($data['a'] as $row)
<div class="row">
<div class="col-sm-2"></div>
<div class="col-sm-10">
<button @click='myFilter($event)'>Comment</button>
<div v-bind:class="{ noActive: isActive }">
<form action="#" method="POST">
<textarea class="textarea" name="answer"></textarea>
<button>Save</button>
</form>
</div>
</div>
</div>
@endforeach
</div>
.noActive{ display:none}
而在 vuejs 中 script
是-
<script>
var vm = new Vue({
el: '#myApp',
data: {
isActive: true,
},
methods: {
myFilter: function (event){
this.isActive = !this.isActive;
}
}
})
</script>
项目数组是在 blade 中渲染的,所以当它到达前端时,Vue 并不知道它。
此外,isActive
对应用程序组件是全局的,因此它适用于所有项目。
您需要将数组作为 prop 传递给您的 vue 组件,然后使用 v-for
.
<div class="row" v-for="(item, index) in {{ data['a'] }}" :key="index">
<!-- same body of the loop as before -->
</div>
然后将index和item添加到myFilter($event, index, item)
函数中,更新数组中对应的item。
您将需要使用列出的方法之一更新项目 in the doc。
如果您不想将它移动到它自己的组件中,那么您将需要一个唯一的标识符来显示循环中的哪个 div 是活动的。您当前的设置无法知道哪个 div 已被点击,您只是切换一个标志,这意味着 div 中的全部或 none 被显示。
解决这个问题的一种方法是使用 foreach
循环的索引,例如
@foreach($data['a'] as $key => $row)
然后对于您的 vue 实例,您可以:
<script>
var vm = new Vue({
el: '#myApp',
data: {
activeKey: null,
},
methods: {
isActive(i) {
return this.activeKey === i;
},
toggleActive(i) {
this.activeKey = this.isActive(i) ? null : i;
}
}
})
</script>
由于逻辑略有变化,您需要更新 blade 文件中的几行:
<button @click='toggleActive($key)'>Comment</button>
<div v-bind:class="{ noActive: isActive($key) }">
这种方法适用于非常小的项目或 vue 使用不多的项目,但是,如果不是这种情况,我建议将其重构为一个组件。
https://laracasts.com/series/learn-vue-2-step-by-step/episodes/7