[英]Vue search filter checkbox issue
I have problem with checkboxes and vue filter of array. 我有复选框和数组的vue过滤器的问题。
new Vue({ el: "#app", data: { todos: [ { text: "Learn JavaScript"}, { text: "Learn Vue"}, { text: "Play around in JSFiddle"}, { text: "Build something awesome"} ], search: '', }, methods: { }, computed: { filtered() { return this.todos.filter(todo => { return todo.text.toLowerCase().includes(this.search.toLowerCase()) }) }, } })
body { background: #20262E; padding: 20px; font-family: Helvetica; } #app { background: #fff; border-radius: 4px; padding: 20px; transition: all 0.2s; } li { margin: 8px 0; } h2 { font-weight: bold; margin-bottom: 15px; } del { color: rgba(0, 0, 0, 0.3); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <input type="text" v-model="search"> <ol> <li v-for="todo in filtered"> <label> <input type="checkbox"> <del v-if="todo.done"> {{ todo.text }} </del> <span v-else> {{ todo.text }} </span> </label> </li> </ol> </div>
If you check first from array and search for any of others: 如果您先从数组中检查并搜索其他任何数据:
Check -> Learn javascript 检查 - >学习javascript
Search -> Learn Vue 搜索 - >学习Vue
Vue appear to be checked.. Vue似乎被检查..
Is that suppose to work like that? 那假设是这样的吗?
How can I remove all from displayed list and append same time? 如何从显示的列表中删除所有内容并同时追加?
Use v-model
to bind in filtered
array 使用
v-model
绑定filtered
数组
new Vue({ el: "#app", data: { todos: [ { text: "Learn JavaScript"}, { text: "Learn Vue"}, { text: "Play around in JSFiddle",done:true}, { text: "Build something awesome"} ], search: '', }, methods: { }, computed: { filtered() { return this.todos.filter(todo => { return todo.text.toLowerCase().includes(this.search.toLowerCase()) }) }, } })
body { background: #20262E; padding: 20px; font-family: Helvetica; } #app { background: #fff; border-radius: 4px; padding: 20px; transition: all 0.2s; } li { margin: 8px 0; } h2 { font-weight: bold; margin-bottom: 15px; } del { color: rgba(0, 0, 0, 0.3); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <input type="text" v-model="search"> <ol> <li v-for="todo in filtered"> <label> <input v-model="todo.done" type="checkbox"> <del v-if="todo.done">{{ todo.text }}</del> <span v-if="!todo.done">{{ todo.text }}</span> </label> </li> </ol> </div>
Here is put checked by default for Play around in JSFiddle
默认情况下,
Play around in JSFiddle
对Play around in JSFiddle
Simply just add done: false
in todos and add v-model="todo.done"
to input in v-for
. 只需在todos中添加
done: false
并将v-model="todo.done"
到v-for
输入。
Example on JSFiddle 关于JSFiddle的例子
Differences from your code: 与您的代码的差异:
// added `done`
todos: [
{ done: false, text: "Learn JavaScript"},
{ done: false, text: "Learn Vue"},
{ done: false, text: "Play around in JSFiddle"},
{ done: false, text: "Build something awesome"}
]
<!-- added `v-model="todo.done"` -->
<input v-model="todo.done" type="checkbox">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.