简体   繁体   English

Vue.js 在单击按钮时动态附加 HTML

[英]Vue.js dynamically appending HTML when clicking on a button

So basically what I am trying to achieve is allowing the user to be able to add to an existing set of information that will be passed on to Vue from JSON.所以基本上我想要实现的是允许用户添加到现有的一组信息中,这些信息将从 JSON 传递给 Vue。

So the following code is my HTML code which comprises of the div element with the id objectiveArea that vue will render to and a hard coded button for the users to add more entries.所以下面的代码是我的 HTML 代码,它由 div 元素组成,其中 id 为objectiveArea ,vue 将渲染到该元素和一个硬编码按钮,供用户添加更多条目。

<div class="form-group" id="objectiveArea"></div>
<div><input type="button" value="Add Objective" id="addButton" /></div>

And here is my Javascript where I pass the JSON into vue for it to render as well as having an onclick event when the add button is triggered.这是我的 Javascript,我在其中将 JSON 传递给 vue 以使其呈现,并在触发添加按钮时具有 onclick 事件。 The function addNewObjectiveRow serves as a template to append when the user clicks add and the objectiveElements is a template for vue to render the existing JSON to the HTML.函数addNewObjectiveRow用作用户单击添加时追加的模板, objectiveElements是 vue 将现有 JSON 呈现为 HTML 的模板。

function addNewObjectiveRow(value) {
            var $divElements = "<div class='row'>"
                + "<div class='form-inline'>"
                + "<br /><div class='form-group'><label class='control-label'>Title</label><div class=''><input type='text' class='form-control objectivetitle' placeholder='Title' /></div></div>"
                + "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"
                + "<div class='form-group input-group'><label class='control-label'>Target Completion Date</label><div class=''><input readonly type='text' class='form-control targetdateinput' placeholder='Select Date' /><span class='input-group-addon'><i class='glyphicon glyphicon-calendar'></i></span></div></div>"
                + "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"
                + "<div class='form-group'><label class='control-label'></label><div><input type='button' value='Remove Goal' class='btn-remove btn-danger deleteButton' /></div></div>"
                + "</div>"
                + "<br />"
                + "<div class='form-group'><label class='control-label'> Details</label><div><textarea class='form-control text-area-width goaldetails' rows='4' placeholder='Enter details here...'></textarea></div></div><hr />"
                + "</div>"
            return $divElements;
        }

var objectiveElements = "<div class='row'><div v-for='(objective, index) in objectivesData'>"
            + "<div class='form-inline'>"
            + "<br /><div class='form-group'><label class='control-label'>Title</label><div class=''><input type='text' class='form-control objectivetitle' placeholder='Title' v-model='decodeData(objective.Title)' /></div></div>"
            + "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"
            + "<div class='form-group input-group'><label class='control-label'>Target Completion Date</label><div class=''><input readonly type='text' class='form-control targetdateinput' placeholder='Select Date' v-formatdate='objective.TargetDate' /><span class='input-group-addon'><i class='glyphicon glyphicon-calendar'></i></span></div></div>"
            + "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"
            + "<div class='form-group'><label class='control-label'></label><div><input type='button' v-if='(index > 0)' value='Remove Goal' class='btn-remove btn-danger deleteButton' /></div></div>"
            + "</div>"
            + "<br />"
            + "<div class='form-group'><label class='control-label'> Details</label><div><textarea class='form-control text-area-width goaldetails' rows='4' placeholder='Enter details here...' v-model='decodeData(objective.Details)'></textarea></div></div><hr />"
            + "</div></div>";

var data = JSON.parse('[{"Title":"Title One","TargetDate":"21 June 2017","Details":"Details One"},{"Title":"Title Two","TargetDate":"22 June 2017","Details":"Details Two"}]');
var Objectives = Vue.extend({
       template: objectiveElements,
       data: function () {
              return {
                objectivesData: data
                     }
              }
       })

new Objectives().$mount('#objectiveArea');

$('#addButton').on('click', function () {
   $('#objectiveArea').append(addNewObjectiveRow(""));               
});//end addButton on click

However, the above code renders the existing information from the JSON just fine in the HTML but when I click on the add button, nothing happens at all.但是,上面的代码在 HTML 中很好地呈现了来自 JSON 的现有信息,但是当我单击添加按钮时,什么也没有发生。 Am I missing something or getting it all wrong?我错过了什么或全都错了吗?

https://v2.vuejs.org/v2/guide/list.html https://v2.vuejs.org/v2/guide/list.html

HTML HTML

<ul id="example-1">
  <li v-for="item in items">
    {{ item.message }}
  </li>
</ul>
<button @click="addItem()">Add new item</button>

JS JS

var example1 = new Vue({
  el: '#example-1',
  data: {
    items: [
      { message: 'Foo' },
      { message: 'Bar' }
    ]
  },
  methods: {
    addItem(){
      this.items.push({message: 'new message'})
    }
  }
})

Clicking the button will execute addItem() method which will add a new item into data items and it will automatically render new li单击该按钮将执行 addItem() 方法,该方法将向数据项中添加一个新项并自动呈现新的 li

there are many mistakes有很多错误

  1. the function addNewObjectiveRow(value) you don't use the parameter value.函数addNewObjectiveRow(value)你不使用参数值。
  2. Vue is data-driven, you should change the objectivesData , not simply append the string. Vue 是数据驱动的,您应该更改objectivesData数据,而不是简单地附加字符串。 Read the document阅读文档

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

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