【问题标题】:Populate a drop-down in Vue.js from an ajax call从 ajax 调用填充 Vue.js 中的下拉列表
【发布时间】:2017-12-18 15:32:48
【问题描述】:

我希望能够进行 ajax 调用并使用返回的结果来生成使用 的下拉选项。 我可以这样做:

<select v-model="selected">
  <option v-for="option in options" v-bind:value="option.value">
    {{ option.text }}
  </option>
</select>
<span>Selected: {{ selected }}</span>

.js 文件

new Vue({
  el: '...',
  data: {
    selected: 'A',
    options: [
      { text: 'One', value: 'A' },
      { text: 'Two', value: 'B' },
      { text: 'Three', value: 'C' }
    ]
  }
})

但我不想让我的选项硬编码,而是来自 ajax 调用。

Ajax 调用看起来像这样:

function pullEmployees(){
        var eventOwnerId = $('#eventOwner').val();
        var eventOwnerName = $('#eventOwner :selected').text();
        $.ajax({
            url: "employees.cfm",
            data: {
                eventOwnerId: eventOwnerId,
                eventOwnerName: eventOwnerName,
                method : "updateOwners"
            },

            success: function(results) {
              // results will have a list of objects where each objects has     two properties (ID and Value)
            }
    });
}

我是 的新手,如果有人能提供帮助,我将不胜感激。

【问题讨论】:

  • 那么到目前为止有什么问题?目前我在您的代码中看到的唯一问题是 this 在您的 success 回调中不会是 Vue。
  • 我的问题是我不知道如何将ajax调用的结果附加到vue对象的options属性上。如果您在上面的示例中看到,我已经对选项进行了硬编码(文本:'One',值:'A',文本:'Two',值:'B' 等)
  • 您说对象以 {id, value} 的形式出现。他们需要是{text, value}吗?
  • 实际上这是一个错字,它们以 {ID, text} 的形式出现。顺便说一句,我必须以某种方式告诉 vue 实例哪个是我的 id,哪个是我的文本或“this.vue.data.options = results;”行会知道如何渲染下拉菜单本身吗?
  • pullEmployees 在哪里?是vue方法还是vue之外的函数? results 到底是什么?只是一个选项对象数组?

标签: vue.js vue.js javascript vue.js vue-component


【解决方案1】:

在下面的示例中,我使用setTimeout 模拟您的ajax 调用。基本上,您想在变量中捕获new Vue() 的结果,然后使用您的ajax 调用结果设置该Vue 的options 属性。

我还更新了您的模板以反映您返回的选项具有{ID, text} 的结构。

console.clear()

let app = new Vue({
  el: '#app',
  data: {
    selected: 'A',
    options: []
  }
})


function pullEmployees(){
  let options = [
    { text: 'One', ID: 'A' },
    { text: 'Two', ID: 'B' },
    { text: 'Three', ID: 'C' }
  ];
  setTimeout(() => app.options = options, 1000)
}
pullEmployees()
<script src="https://unpkg.com/vue@2.2.6/dist/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div id="app">
  <select v-model="selected">
    <option v-for="option in options" v-bind:value="option.ID">
      {{ option.text }}
    </option>
  </select>
  <span>Selected: {{ selected }}</span>
</div>

然而,所有这些都可以在 Vue 中完成。不需要在外面做。

let app = new Vue({
  el: '#app',
  data: {
    selected: 'A',
    options: []
  },
  methods:{
    pullEmployees(){
      var eventOwnerId = $('#eventOwner').val();
      var eventOwnerName = $('#eventOwner :selected').text();
      $.ajax({
        url: "employees.cfm",
        data: {
          eventOwnerId: eventOwnerId,
          eventOwnerName: eventOwnerName,
          method : "updateOwners"
        },
        success: results => this.options = results
      });
    }
  },
  mounted(){
    this.pullEmployees()
  }
})

如果 eventOwner 也是 Vue 的一部分,那么您可以从 Vue 获取该值作为数据属性。

【讨论】:

  • 你能给我举个例子,如何将 eventOwner 放入 Vue.js 中。您可以从我的代码中看到我现在是如何从 DOM 中读取这些值的。还有 "el:#app" 这是否意味着选择的 ID 是 "app"?
  • @DoArNa 我在玩你的代码时构建了这个codepen。它显示了你将如何做到这一点。进行 ajax 调用时会引发错误,因为显然我无权访问您的服务器。此外,您可能希望在所有者更改时而不是在mounted 中获取选项。
  • 非常感谢您花时间帮助我解决这个问题。据我了解,当页面加载时,它将使用 Mary 和 Bob 加载下拉列表,但是我想要的是当页面加载时下拉列表具有来自 ajax 调用的选项。我知道您希望能够拥有一个工作模型,因为您无法访问服务器,但至少代码将如何编写?页面上没有条件可以在页面加载后更改列表选项,因此我无需再担心更新选择元素。
【解决方案2】:

将你的 vue 实例存储为变量:

var vue = new Vue(// stuff...);

然后对于Ajax要注意this的范围:

function pullEmployees(){
        var _this = this; // bind "this" to local var
        var eventOwnerId = $('#eventOwner').val();
        var eventOwnerName = $('#eventOwner :selected').text();
        $.ajax({
            url: "employees.cfm",
            data: {
                eventOwnerId: eventOwnerId,
                eventOwnerName: eventOwnerName,
                method : "updateOwners"
            },

            success: function(results) {
              _this.vue.data.options = results; // set data
            }
    });
}

【讨论】:

  • 在我的 vue 实例上,我是否将选项属性作为空字符串启动? (数据:{选项:=“”}
  • 你应该把它初始化为一个空数组。
  • 谢谢,我马上试试。
猜你喜欢
  • 1970-01-01
  • 2013-05-07
  • 1970-01-01
  • 2012-11-08
  • 1970-01-01
  • 1970-01-01
  • 2011-07-17
  • 2013-01-10
  • 2017-08-26
相关资源
最近更新 更多