Vue.js创建选择器,显示选择的数据集中的数据

时间:2019-12-01 14:16:37

标签: arrays for-loop vue.js html-select v-for

大家好,我尝试创建一个选择输入字段,在其中用我创建的数据集中的标签填充选项,如下所示:

visitsList: [
                    {
                    label: '2017',
                    values: [
                        { id: 1, title: "January", value: 20000 },
                        { id: 2, title: "February", value: 30000 },
                        {id: 3,title: "March", value: 40000},
                        { id: 4, title: "April", value: 40000},
                        {id: 5,title: "May",value: 50000},
                        { id: 6,title: "June",value: 60000},
                        {id: 7, title: "July",value: 20000},
                        { id: 8,title: "August", value: 70000},
                        { id: 9,title: "September",value: 70000},
                        {id: 10, title: "October",value: 80000},
                        {id: 11,title: "November",value: 90000},
                        {id: 12,title: "December",value: 100000}
                    ]},
                    {
                    label: '2018',
                    values: [
                        { id: 1, title: "January", value: 20000 },
                        { id: 2, title: "February", value: 30000 },
                        {id: 3,title: "March", value: 40000},
                        { id: 4, title: "April", value: 40000},
                        {id: 5,title: "May",value: 50000},
                        { id: 6,title: "June",value: 60000},
                        {id: 7, title: "July",value: 20000},
                        { id: 8,title: "August", value: 70000},
                        { id: 9,title: "September",value: 70000},
                        {id: 10, title: "October",value: 80000},
                        {id: 11,title: "November",value: 90000},
                        {id: 12,title: "December",value: 100000}
                    ]},
                    {
                    label: '2019',
                    values: [
                        { id: 1, title: "January", value: 20000 },
                        { id: 2, title: "February", value: 30000 },
                        {id: 3,title: "March", value: 40000},
                        { id: 4, title: "April", value: 40000},
                        {id: 5,title: "May",value: 50000},
                        { id: 6,title: "June",value: 60000},
                        {id: 7, title: "July",value: 20000},
                        { id: 8,title: "August", value: 70000},
                        { id: 9,title: "September",value: 70000},
                        {id: 10, title: "October",value: 80000},
                        {id: 11,title: "November",value: 90000},
                        {id: 12,title: "December",value: 100000}
                    ]
                    }   
              ],
              selectedYear: [],

目标是如果我选择带有年份的选项,则它应该显示值。 模板如下所示

 <select v-model="widget.selectedYear">
                    <option v-for="year in widget.visitsList" v-bind:key="year.values">
                        {{year.label}}
                    </option>
                </select>
                <!--v-select :option="widget.visitsList.label" ></v-select-->
              <table class="table table-bordered table-striped mb-0">
                <thead>
                  <tr>
                    <th scope="col">Month</th>
                    <th scope="col">Views</th>
                  </tr>
                </thead>
                <tbody>
                  <tr v-for="year in widget.visitsList" v-bind:key="year.label" ><!--v-if="year.label == selectedYear"-->
                        <th scope="row">{{visit.title}}</th>
                        <td>{{visit.value}}</td>
                    </div>
                  </tr>
                </tbody>
              </table>

Here is also a picture of how its rendered

我尝试了很多事情,但是以某种方式我找不到正确的解决方案。也许没有办法。 谢谢您的帮助 关于马克西姆

1 个答案:

答案 0 :(得分:1)

我认为您应该为此使用计算属性,将第一个选择器绑定到数据属性,然后让计算属性监视该数据属性并根据数据属性的更改返回第二个选择的值。 / p>