1

I have a data binding which contains the following string:

bc-men,bc-men-fashion,bc-men-underwear

I would like to have an input field in which I could add "bc-some-category" and afterwards click "Add" and it would be added to the end of the list with a ,(comma) in front.

My markup looks like the following:

<div class="js-referencing-categories">{{ product.refrencing_category_ids }}</div>
<input class="" required type="text" name="fname">
<button class="" v-on:click="product.refrencing_category_ids.add.value">
    Add
</button>

What would I need to do in order to make that happen, I dont seem to find any documentation on exactly that in the vueJS guide.

1

3 Answers 3

2

https://jsfiddle.net/30hyzqsp/

I'm not sure to quite understand your question but this should do the trick. You'll have to adapt with your data structure tho:

<template>
    <div>
        <div class="js-referencing-categories">{{ categories.join(', ') }}</div>
        <input class="" required type="text" name="fname" ref="input">
        <button class="" @click="addCategory($refs.input)">
            Add
        </button>
    </div>

</template>

<script>
    export default {
        data () {
            return {
                categories: []
            }
        },
        methods: {
            addCategory (e) {
                this.categories.push(e.value)
                e.value = ''
            }
        }
    }
</script>
Sign up to request clarification or add additional context in comments.

Comments

2

You can use v-model directive to control the user input and push the model value to other categories on the button click:

new Vue({
    el: '#demo',
    data () {
        return {
            categories: [
                'bc-men',
                'bc-men-fashion',
                'bc-men-underwear'
            ],
            newCategory: ''
        }
    },
    methods: {
        addCategory(category) {
            this.categories.push(category);
        }
    }
});

Vue.config.devtools = false;
Vue.config.productionTip = false
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
<ul>
    <li v-for="category in categories">{{ category }}</li>
</ul>
<input v-model="newCategory" required type="text" name="fname">
<button @click="addCategory(newCategory)">
    Add
</button>
</div>

Comments

0

You need to store what the user typed in your input, for example by using v-model:

data () {
  return {
    userInput: ''
  }
}
<input class="" v-model="userInput" required type="text" name="fname">

Then whenever the user clicks on the Add button, you add that value:

<button class="" v-on:click="addProduct">
    Add
</button>
methods: {
  addProduct () {
    this.product.refrencing_category_ids += `,${this.userInput}`;
    this.userInput = '';
  }
}

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.