使用Vue JS为模板变量设置动态初始索引(Setting a dynamic initial index for template variable using Vue JS)

编程入门 行业动态 更新时间:2024-10-23 03:28:26
使用Vue JS为模板变量设置动态初始索引(Setting a dynamic initial index for template variable using Vue JS)

我正在使用CMS创建一个前端表单,客户(剧院导演)可以定义几个放映时间。 我有一个按钮,他们根据需要点击“添加另一个”输入字段(旁注:我不知道如何使用Vue启用'删除最后一个'按钮,但这是另一天的另一个主题)。

我已经开始工作了。 但是现在,一旦导演创建了他们的放映时间并且他们回来编辑它们,我需要Vue计数器的起始索引是已经存在的字段数。 因此,如果他们已经定义了3个放映时间(0,1,2),则使用Vue动态放置的第一个字段应该以3开头。

或者,我想知道在页面加载时是否更容易在Vue中生成现有数据的数组,但我不知道我从哪个方法开始。

这是我的HTML和Vue JS:

new Vue({
  el: '#app',

  data: {
    timeslots: [
      {
        count: 0
      }
    ],
    count: 0
  },

  methods: {
    addAnother: function(){
      this.timeslots.push({
        count: ++this.count
      });
    }
  }
}); 
  
<div id="app">
  {{ performances }}
    <input name="performances[{{ zero_index }}][showtime]" value="{{ showtime }}">
  {{ /performances }}

  <template v-for="slot in timeslots">
    <input name="performances[@{{ slot.count }}][showtime]" value="{{ now }}">
  </template>
  
  <div>
    <button @click.prevent="addAnother">Add another</button>
  </div>
</div> 
  
 

任何前进的帮助表示赞赏!

I'm using a CMS to create a front-end form where customers (theatre directors) can define several showtimes. I have a button they click to "add another" input field as needed (sidenote: I don't know how to enable a 'remove last' button using Vue, but that's another topic for another day).

I've gotten that working. But now, once a director has created their showtimes and they come back to edit them, I need the starting index of the Vue counter to be the number of fields that already exist. So if they've defined 3 showtimes already (0,1,2), the first field dynamically placed with Vue should start with 3.

Alternatively, I wonder if it'd be easier to generate an array of the existing data in Vue when the page loads, but I don't know where I'd start with that method.

Here's my HTML and Vue JS:

new Vue({
  el: '#app',

  data: {
    timeslots: [
      {
        count: 0
      }
    ],
    count: 0
  },

  methods: {
    addAnother: function(){
      this.timeslots.push({
        count: ++this.count
      });
    }
  }
}); 
  
<div id="app">
  {{ performances }}
    <input name="performances[{{ zero_index }}][showtime]" value="{{ showtime }}">
  {{ /performances }}

  <template v-for="slot in timeslots">
    <input name="performances[@{{ slot.count }}][showtime]" value="{{ now }}">
  </template>
  
  <div>
    <button @click.prevent="addAnother">Add another</button>
  </div>
</div> 
  
 

Any help to move forward is appreciated!

最满意答案

我使用我在OP中推测的第二种方法,使用$index而不是像count这样的任意整数,在比预期更短的时间内解决了这个问题。 此方法需要将JS直接放在我的模板中,而不是放在单独的文件中。 但那对我没问题。

<div id="oven"> <template v-for="biscuit in oven"> <div> <input type="text" name="performances[@{{ $index }}][showtime]" value="@{{ biscuit.showtime }}"> </div> </template> <span class="add small black btn" @click="addAnother"><i class="fa fa-plus-circle"></i> Add another</span> </div> <script> new Vue({ el: '#oven', data: { oven: [ {{ performances }} { showtime: "{{ showtime }}" }, {{ /performances }} ] }, methods: { addAnother: function(){ this.oven.push({ showtime: '{{ now modify_date="+2 months" format="Y-m-d" }} 7:30 PM' }); } } }); </script>

I solved it myself in shorter time than expected using the 2nd method I speculated about in my OP, and using $index instead of an arbitrary integer like count. This method requires the JS to be put directly in my template though, not in a separate file. But that's fine by me.

<div id="oven"> <template v-for="biscuit in oven"> <div> <input type="text" name="performances[@{{ $index }}][showtime]" value="@{{ biscuit.showtime }}"> </div> </template> <span class="add small black btn" @click="addAnother"><i class="fa fa-plus-circle"></i> Add another</span> </div> <script> new Vue({ el: '#oven', data: { oven: [ {{ performances }} { showtime: "{{ showtime }}" }, {{ /performances }} ] }, methods: { addAnother: function(){ this.oven.push({ showtime: '{{ now modify_date="+2 months" format="Y-m-d" }} 7:30 PM' }); } } }); </script>

更多推荐

本文发布于:2023-07-26 15:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1277440.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   索引   模板   动态   Vue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!