简体   繁体   English

Vue.js使用单独的数组从对象构建视图

[英]Vue.js build a view from an object using separate array

  1. Change the html select and it updates the variable called selected . 更改html select并更新名为selected的变量。
  2. Use the selected variable to retrieve the array with the same from the hangOut object. 使用selected变量从hangOut对象检索具有相同array
  3. Change the list to include just the two names (in that order) contained in that array. 更改列表以仅包含该数组中包含的两个名称(按此顺序)。

So if selected is "often" than the list would show brown eyed jerry and green eyed jane. 因此,如果selected是“经常”,则列表将显示棕眼的杰里和绿眼的杰恩。 Here is the code and fiddle: 这是代码和小提琴:

HTML 的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 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;
    }
  }
})

Fiddle 小提琴

https://jsfiddle.net/ty3ypmn0/5/ https://jsfiddle.net/ty3ypmn0/5/

You have to pass the this.selected like this.hangOut[this.selected] as the following code 您必须将this.selectedthis.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;
    }
  }

Follow the fiddle https://jsfiddle.net/ty3ypmn0/9/ 跟随小提琴https://jsfiddle.net/ty3ypmn0/9/

You have to access the computed property in your template {{filter}} and in js you can call like this.filter 您必须在模板{{filter}}访问计算属性,在js中,您可以像这样this.filter

Let's take a look at jsFiddle : 让我们看一下jsFiddle

I've refactored your code and made a computed method to handle selection: 我已经重构了您的代码,并制作了一个计算方法来处理选择:

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's see what the code is saying: 让我们看看代码在说什么:

let keys = Object.keys(this.persons).filter(key => this.hangOut[this.selected].indexOf(key) >= 0);

This line finds the objects which you provided inside an array inside hangOut, I've used Object.keys to retrieve keys of persons (which should be people :D) then I've filtered ( Array.prototype.filter MDN reference ) them to retrieve only selected objects (take a look at this.hangOut[this.selected].indexOf(key)...) 该行在hangOut的数组中找到您提供的对象,我使用Object.keys来检索人员(应该是人员:D)的键,然后将其过滤( Array.prototype.filter MDN参考 )以仅检索选定的对象(看看this.hangOut [this.selected] .indexOf(key)...)

and finally added the items to the returned object and rendered them. 最后将项目添加到返回的对象并进行渲染。

I add two others <p> to ilustrate what selectedInfos is returning. 我添加了另外两个<p>来说明selectedInfos返回的内容。

First of all, when you are using the property computed in Vue think of variables that will change automatically if another variable inside your scope has changed (eg when this.selected changes, selectedInfos will return something new and will updates). 首先,当您使用Vue中computed的属性时,请考虑如果范围内的另一个变量已更改(例如,当this.selected更改时,selectedInfos将返回新内容并进行更新)将自动更改的变量。

I changed the "activeSelected" to this.selected to correctly keep tracking what you selected. 我将"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> 

Like this ? 像这样 ? You just need a computed filtered array, filteredPersons. 您只需要一个计算的过滤数组filteredPersons。 You can see that its inputs are persons, hangOut and 'selected', so whenever one of those changes, filteredPersons will be recalculated. 您可以看到它的输入是人员,环聊和“已选择”,因此,只要其中一项更改,都将重新计算filteredPersons。

markup 标记

<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 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);
    }
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM