检查 vuejs 中是否存在值 [英] Check if value exists in vuejs
本文介绍了检查 vuejs 中是否存在值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有数据:1,2,3,4,4,5 &我的代码是这样的:
{{display.test_id}}
我的代码如果在 php 中像这样
$temp_id = array();foreach($data 作为 $data){if(in_array($data ->test_id,$temp_id)){回声1:没有";回声2:没有";回声3:没有";回声4:是";//因为他有相同的价值回声5:没有";$temp_id[] = $data ->test_id;}}
我如何在循环 vueJs 中做到这一点...??
解决方案
据我所知,你想检查数组中是否有值,然后进行相应的渲染?
如果是这样,您需要一个 Vue 自定义过滤器.像这样的事情可以解决问题:
var vm = new Vue({el: '身体',数据: {编辑列表评估:[1,2,3,4,4,5]},过滤器:{ifInArray:函数(值){返回 this.editlistAssesments.indexOf(value) >-1 ?'是' : '否';}},});
然后像这样使用它:
<span v-text="display.test_id | ifInArray"></span><!-- 将 Vue 值绑定到 html 元素是更好的做法 -->
<块引用>
查看文档了解更多信息:http://vuejs.org/guide/custom-filter.html
I have data : 1,2,3,4,4,5 & my code like this:
<div id="looping" v-for="display in editlistAssesments">
{{display.test_id}}
</div>
My code if in php such as like this
$temp_id = array();
foreach($data as $data){
if(in_array($data ->test_id,$temp_id)){
echo" 1: no";
echo" 2: no";
echo" 3: no";
echo" 4: yes"; //because he have same value
echo" 5: no";
$temp_id[] = $data ->test_id;
}
}
how I can do that in loop vueJs..??
解决方案
As far as I understand, you want to check if value is in array and then render it accordingly?
If so, you need a Vue custom filter. Something like this will do the trick:
var vm = new Vue({
el: 'body',
data: {
editlistAssesments: [1,2,3,4,4,5]
},
filters: {
ifInArray: function (value) {
return this.editlistAssesments.indexOf(value) > -1 ? 'Yes' : 'No';
}
},
});
And then use it like this:
<div id="looping" v-for="display in editlistAssesments">
<span v-text="display.test_id | ifInArray"></span>
<!-- bind Vue value to html element is better practice -->
</div>
Check docs for more information: http://vuejs.org/guide/custom-filter.html
这篇关于检查 vuejs 中是否存在值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文