1

Basically what I want to achieve is GET data from server and display the data in the nested json to display in Multiselect, Vue-Multiselect

After displaying, i'm able to add new tags to it if i want (Being able to update it).

I'm able to get the objects from the nested json to display in multiselect, but I'm not sure how to customize it to only show the name.

Current behaviour: enter image description here


So expected behaviour would be, only Sofa, Table and Chair should be shown in the multiselect: enter image description here

Is there a way for me to only display like the picture above?


After implementing @Ikbel's way of getting the json object and showing only the required name. Now I have another problem which is I get duplicates of the options whenever I add a new tag to it.

This is my Vue Code:

   <template>
       <multiselect :multiple="true"
       v-model="data.subCategoryNames"
       :hide-selected="true"
       :selected="data.subCategoryNames"
       :options="computedSubCategoryNames"
       :taggable="true"
       @tag="addTag"
        >
        </multiselect>
  </template>


  methods: {
    addTag (newTag) {
      // this.options.push(newTag)
      this.data.subCategoryNames.push(newTag)
    }
  }



computed: {
    computedSubCategoryNames () {
      return this.allSubCategoryNames.map((item) => {
        this.options.push(item.subCategoryName)
        this.data.subCategoryNames.push(item.subCategoryName)
        return item.subCategoryName
      })
    }
  }

Which shows this: enter image description here

Thank you for your help!

1
  • Ok, I answered your question below, take a look. Commented Jul 24, 2017 at 10:32

3 Answers 3

3

Ok @mary. Here is a better solution. Simply add label="subCategoryName" to your multiselect component to make it show subCategoryName only instead of the whole object. So no need for a computed property.

track-by should be used to avoid duplicate values.

Here is a working example.

let Multiselect = VueMultiselect.Multiselect

let vm = new Vue({
  el: '#app',
  data: {
    data: {
      subCategoryNames: [],
      options: [
        {subCategoryName: 'Chair', count: 2},
        {subCategoryName: 'Table', count: 5},
      ],
    },
  },   
 
  components: { 
    Multiselect 
  },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>

<script src="https://unpkg.com/[email protected]"></script>

<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/vue-multiselect.min.css">


<div id="app">        
  <multiselect :multiple="true"
       v-model="data.subCategoryNames"
       :hide-selected="true"
       :selected="data.subCategoryNames"
       :options="data.options"
       :taggable="true"
       label="subCategoryName"
       track-by="subCategoryName"
        >
        </multiselect>
        
   {{data.subCategoryNames}}
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

Hi @Ikbel , using label="subCategoryName" and track-by="subCategoryName" worked for my case! Thanks for your help!
1

Use a computed property to extract the subcategory names. Here is how you can do it.

Bind your multiselect options to a computed subCategoryNames instead. Let's call it computedSubCategoryNames, and use the array method map() to extract subCategoryName from subCategoryNames. Here is an example:

<multiselect :options="computedSubCategoryNames">
</multiselect>

And in define computedSubCategoryNames:

computed: {
  // Returns ['Chair', 'Sofa', 'Table']
  computedSubCategoryNames() {
    return this.subCategoryNames.map(function(item) {
      return item.subCategoryName
    }) 
  }
}

3 Comments

Hi @Ikbel , I used your method and it works! I have also added additional code to it so it works 100%. this.options.push(item.subCategoryName) this.data.subCategoryNames.push(item.subCategoryName)
Hi @Ikbel , after adding this line this.options.push(item.subCategoryName) i realized i have another problem which is, whenever i want to add a new tag, my options will then be "sofa,table,chair,TEST1,sofa,table,chair". Is there a way to stop the re-pushing of options from the computed method?
Hi @mary. Can you post your Vue methods so I can look into that. I think that there's a better way to do that.
0

Please refer below URL, onether approach to show multiselect drop down in android https://asnehal.wordpress.com/2012/04/03/multi-select-drop-down-list-in-android/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.