简体   繁体   English

组的Vue2动态v模型

[英]Vue2 dynamic v-model for a group

I am working on a housing form and I am stuck somewhere and I am hoping someone give me a solution to my problem. 我正在制作房屋表格,并且被卡在某个地方,希望有人能给我解决我的问题的方法。

So, when you select in a form that your house have 2 floors, then you get to write each floor how many rooms, toilets, balcony etc has.. 因此,当您选择房子有两层楼的形式时,便可以写出每一层楼有多少间客房,厕所,阳台等。

My issue is to "group" these answers somehow that I could send it to my backend and "read" those answers. 我的问题是以某种方式将这些答案“分组”,以便可以将其发送到后端并“读取”这些答案。

so this is what I did so far: 所以这是我到目前为止所做的:

data() {
  return {
    flooroptions: [
      {
        name: 'Rooms',
        id: '1',
      },
      {
        name: 'Balcony',
        id: '2',
      },
      {
        name: 'Toilets',
        id: '3',
      },
    ],
    floors: '',
    floor1: [],
  },
}

and if I loop through like this: 如果我像这样循环遍历:

<div class="" v-for="(opt, index) in flooroptions">
  <input type="number" name="" value="" v-model="floor1[index]">
</div>

I can see my data using floor1: 我可以使用floor1查看数据:

{{ floor1 }}

But if in the form someone selects that 1st floor has 2 rooms and does not add any other input than I get to floor1 only 2 但是,如果有人在表单中选择1楼有2个房间,并且除了我只进入2楼,没有添加任何其他输入,

How would be better approach for this type of problem ?? 对于这种类型的问题,有什么更好的方法?

PS. PS。 Everyfloor will have this flooroptions loop and they can input number of each option for each floor, and I want to be able to read that number correctly.. 每个楼层都有这个flooroptions循环,他们可以为每个楼层输入每个选项的编号,我希望能够正确读取该编号。

if I understand correctly, I would... 如果我理解正确,我会...

data() {
  return {
    flooroptions: [
    {
      name: 'Rooms',
      id: '1',
    },
    {
      name: 'Balcony',
      id: '2',
    },
    {
      name: 'Toilets',
      id: '3',
    },
    ],
    numFloors: 2,
    floorData: [
      {'Rooms':0, 'Balcony':0, 'Toilets':2},
      {'Rooms':2, 'Balcony':1, 'Toilets':0}
    ],
  },
}

and loop through like this: 并像这样循环遍历:

<div v-for="f in numFloors">
  <div class="" v-for="opt in flooroptions">
    <input type="number" name="" value="" v-model="floorData[f][opt.name]">
  </div>
</div>

or... if you want to pass less data and the id's are know on the backen 或...如果您想传递较少的数据,并且在后台知道ID

data() {
  return {
    flooroptions: {
      1: {
        name: 'Rooms'
      },
      2: {
        name: 'Balcony'
      },
      3: {
        name: 'Toilets'
      },
    },
    numFloors: 2,
    floorData: [
      {1:0, 2:0, 3:2},
      {1:2, 2:1, 3:0}
    ],
  },
}

and loop through like this: 并像这样循环遍历:

<div v-for="f in numFloors">
  <div class="" v-for="(opt, i) in flooroptions">
    <input type="number" name="" value="" v-model="floorData[f][i]">
  </div>
</div>

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

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