0

I am new Vue.js, I am working invoice project using Vue.js. I want out put like this, output:

{
    "invoiceFirstLevelCategory": [{
        "categoryname": "first1",
        "price": 30,
        "invoiceSecondLevelCategory": [{
            "categoryname": "first-1-2-1",
            "price": 22
        }, {
            "categoryname": "first-1-2-2",
            "price": 0,
            "invoiceThirdLevelCategory": [{
                "categoryname": "first-1-2-3-1",
                "price": 11
            }, {
                "categoryname": "first-1-2-3-2",
                "price": 11
            }]
        }]
    }]
}

Check Mycode :

<!DOCTYPE html>
<html>
<script src="https://unpkg.com/vue/dist/vue.js"></script>

<body>
    <div id="app">
        <input type="text">
        <button v-on:click="addsecond">add second</button>
        <template>
            <div v-for="pre_second in second">
                <input type="text" v-model="pre_second.categoryname">
                <input type="text" v-model="pre_second.price">
                <button v-on:click="add_thrid(second)">add thrid</button>
                <hr>
                <div v-for="thrid in second_sub">
                    <input type="text" v-model="thrid.categoryname">
                     <input type="text" v-model="thrid.price">
                     <button v-on:click="add_thrid(second)">add next</button>
                </div>
            </div>
        </template>
        <pre>{{second|json}}</pre>
    </div>
    <script>
        new Vue({
            el: '#app',
            data: {
                second: [],
                thrid: []
            },
            methods: {
                addthrid: function() {
                    this.thrid.push({
                        categoryname: '',
                        price: 0
                    });
                },
                addsecond: function() {
                    this.second.push({
                        categoryname: '',
                        price: 0
                    });
                },
                add_thrid: function(arr) {
                    if (arr.isArray) {
                        console.log("they have nested", arr);
                    }
                    var th = this.thrid.push({
                        categoryname: '',
                        price: 0
                    });
                    console.log(this.thrid);
                    this.push(th);
                }
            }
            //main
        });
    </script>
</body>
</html>

My code was wrong, but I tried it many way. I don't know where is my mistake and find the solution.

2
  • 1
    Property or method "second_sub" is not defined on the instance but referenced during render. And please add where current code is behaving wrong and how you want it to be. Commented Dec 4, 2016 at 11:46
  • I want to output like above my json Commented Dec 4, 2016 at 12:52

0

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.