select
,然后更新名为selected
的变量。selected
变量从array
对象中检索hangOut
相同的内容。所以如果selected
是"经常"比名单显示棕色眼睛的杰里和绿眼睛的简。这是代码和小提琴:
HTML
<div id="app">
<select v-model="selected">
<option v-for="(frequency,key) in selectOptions" :value="key">{{frequency}}</option>
</select>
<ul>
<li v-for="(person,key) in persons" :key="key">
{{ person.eyes + " eyed " + key }}
</li>
<p>These two like to hangout {{selected}}.</p>
</ul>
</div>
JS
var vm = new Vue({
el: '#app',
data: {
selected: "sometimes",
selectOptions: {
sometimes: "Sometimes",
often: "Often",
rarely: "Rarely"
},
persons: {
joe: {
height: 'Tall',
eyes: 'blue',
age: 30
},
jane: {
height: 'Medium',
eyes: 'green',
age: 22
},
jerry: {
height: 'short',
eyes: 'brown',
age: 33
}
},
hangOut: {
sometimes: ["joe", "jane"],
often: ["jerry", "jane"],
rarely: ["jerry", "joe"]
}
},
computed: {
filter() {
var result = {}; //create new emptly object to return
arr = this.hangOut["activeSelect"];
for (var i = 0, len = arr.length; i < len; i++) {
result[i]=arr[i];
}
return result;
}
}
})
小提琴
答案 0 :(得分:0)
您必须像this.selected
一样传递this.hangOut[this.selected]
以下代码
computed: {
filter() {
var result = {}; //create new emptly object to return
var arr = this.hangOut[this.selected];
for (var i = 0, len = arr.length; i < len; i++) {
result[i]=arr[i];
}
return result;
}
}
关注小提琴https://jsfiddle.net/ty3ypmn0/9/
您必须访问模板{{filter}}
中的计算属性,而js中您可以调用this.filter
答案 1 :(得分:0)
让我们来看看jsFiddle:
我重构了你的代码并制作了一个计算方法来处理选择:
listObjects() {
let keys = Object.keys(this.persons).filter(key => this.hangOut[this.selected].indexOf(key) >= 0);
let finalPeople = {};
for(let i = 0; i < keys.length; i++) {
let key = keys[i];
finalPeople[key] = this.persons[key];
}
return finalPeople;
},
让我们看看代码在说什么:
let keys = Object.keys(this.persons).filter(key => this.hangOut[this.selected].indexOf(key) >= 0);
这一行找到你在hangOut里面的数组中提供的对象,我使用了Object.keys来检索人员的密钥(应该是人:D)然后我已经过滤了({{3它们只检索选定的对象(看看this.hangOut [this.selected] .indexOf(key)...)
最后将项目添加到返回的对象并进行渲染。
答案 2 :(得分:0)
我添加了另外两个<p>
来说明selectedInfos
正在返回的内容。
首先,当您在Vue中使用属性computed
时,如果您的范围内的另一个变量发生了变化(例如,当此选择更改时,selectedInfos将返回一些新内容并且将更新)。
我将"activeSelected"
更改为this.selected
以正确跟踪您选择的内容。
var vm = new Vue({
el: '#app',
data: {
selected: "sometimes",
selectOptions: {
sometimes: "Sometimes",
often: "Often",
rarely: "Rarely"
},
persons: {
joe: {
height: 'Tall',
eyes: 'blue',
age: 30
},
jane: {
height: 'Medium',
eyes: 'green',
age: 22
},
jerry: {
height: 'short',
eyes: 'brown',
age: 33
}
},
hangOut: {
sometimes: ["joe", "jane"],
often: ["jerry", "jane"],
rarely: ["jerry", "joe"]
}
},
computed: {
selectedInfos() {
var result = []; //create new emptly object to return
arr = this.hangOut[this.selected];
for (var i = 0; i < arr.length; i++) {
result.push(this.persons[arr[i]])
}
return result;
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.4/vue.min.js"></script>
<div id="app">
<select v-model="selected">
<option v-for="(frequency,key) in selectOptions" :value="key">{{frequency}}</option>
</select>
<ul>
<li v-for="(person,key) in persons" :key="key">
{{ person.eyes + " eyed " + key }}
</li>
<p>These two like to hangout: {{hangOut[selected][0]}} and {{hangOut[selected][1]}}
</p>
<p>Infos: {{selectedInfos}}
</p>
<p>OneInfo: {{selectedInfos[0]}}
</p>
</ul>
</div>
答案 3 :(得分:0)
<强> Like this ? 强> 您只需要一个计算过滤的数组,filteredPersons。您可以看到它的输入是人,hangOut和&#39;选择&#39;,因此,只要其中一个更改,就会重新计算filteredPersons。
<强>标记强>
<div id="app">
<select v-model="selected">
<option v-for="(frequency,key) in selectOptions" :value="key">{{frequency}}</option>
</select>
<ul>
<li v-for="(person,key) in persons">
{{ person.eyes + " eyed " + person.name }}
</li>
<p>These two like to hangout {{selected}}.</p>
{{filteredPersons}}
</ul>
</div>
<强> JS 强>
var vm = new Vue({
el: '#app',
data: {
selected: "sometimes",
selectOptions: {
sometimes: "Sometimes",
often: "Often",
rarely: "Rarely"
},
persons: [
{name: 'joe',
height: 'Tall',
eyes: 'blue',
age: 30
},
{name: 'jane',
height: 'Medium',
eyes: 'green',
age: 22
},
{name: 'jerry',
height: 'short',
eyes: 'brown',
age: 33
}
],
hangOut: {
sometimes: ["joe", "jane"],
often: ["jerry", "jane"],
rarely: ["jerry", "joe"]
}
},
computed: {
filteredPersons(){
return this.persons.filter( pers => this.hangOut[this.selected].indexOf(pers.name) + 1);
}
}
})