简体   繁体   English

Vue-multiselect:如何将 object 转换为数组以在选项道具中使用?

[英]Vue-multiselect: How to convert object to array for use in options prop?

I am using vue-multiselect like so:我正在使用vue-multiselect像这样:

    <multiselect
      id="customer_last_name_input"
      v-model="value"
      :options="activeUserProfiles"
      label="lastname"
      placeholder="Select or search for an existing customer"
      track-by="uid"
      :close-on-select="true"
      @select="onSelect"
      @remove="onRemove"
      :loading="isLoading"
      :custom-label="customerSelectName"
      aria-describedby="searchHelpBlock"
      selectLabel=""
    >

...that grabs the list of active customers from an Array and then makes them available in a nice select menu. ...从数组中获取活跃客户列表,然后在漂亮的 select 菜单中提供它们。

This works good.这很好用。 However, I need to add another option from another resource (called customerNone ) to the options prop and but the data is returned as an Object like so:但是,我需要将另一个资源中的另一个选项(称为customerNone )添加到options道具,但数据以 Object 的形式返回,如下所示:

{"uid":1,"lastname":"None Given","firstname":"User","email":null,"phone":null...blah} {"uid":1,"lastname":"None Given","firstname":"User","email":null,"phone":null...blah}

The vue-multiselect docs state that the :option prop MUST be an Array. vue-multiselect 文档 state 说:option属性必须是一个数组。

Question: What is the best way for me to handle this in the vue-multiselect component?问题:我在 vue-multiselect 组件中处理这个问题的最佳方法是什么? Here's my attempt to help explain what I am trying to do (not sure if this is the best way to handle it).这是我试图帮助解释我正在尝试做的事情(不确定这是否是处理它的最佳方法)。 Unfortunately, my attempt causes a console error (see below):不幸的是,我的尝试导致控制台错误(见下文):

I am passing a prop down called noCustomer which, if is true , I need to use customerNone profile on :options :我正在传递一个名为noCustomer的道具,如果它是true ,我需要在:options上使用customerNone配置文件:

<multiselect
      :options="noCustomer ? customerNone : getActiveUserProfiles"
>

here's the error:这是错误:

Invalid prop: type check failed for prop "options". Expected Array, got Object 

Is there a way I can convert the customerNone object to an array of object?有没有办法可以将customerNone object 转换为 object 数组? Thanks!谢谢!

You could wrap the customerNone object in brackets at the time that you pass it to the <multiselect> like [customerNone] .您可以在将customerNone object 传递给<multiselect>时将其包裹在括号中,例如[customerNone]

This syntax creates a new array on the fly, having 1 element that is the object variable:此语法即时创建一个新数组,其中包含 1 个元素,即 object 变量:

<multiselect
  :options="noCustomer ? [customerNone] : getActiveUserProfiles"
>

Update for comments更新评论

In order to auto-select the generic option when it's available, use a watch on the noCustomer prop to set value whenever noCustomer === true :为了在通用选项可用时自动选择它,请在noCustomer === true时使用noCustomer道具上的watch来设置value

watch: {
  noCustomer(newValue, oldValue) {
    if(newValue) {        // Checking that `noCustomer === true` 
      this.value = this.customerNone;
    }
  }
}

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

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