How to dynamically generate div in Vue?











up vote
1
down vote

favorite
1












I have the following code:



<div class="content">
<p>Hello World</p>
</div>


And I have the following div count:



data() {
return {
divs: 2
}
}


So, if divs is 2 then it should generate two divs and wrap the div.content inside it so it becomes something like this:



<div>
<div>
<div class="content">
<p>Hello World</p>
</div>
</div>
</div>


As you can see there's two divs and then only there's this div.content! I tried using v-for but it generates 2 separate divs! Please help!










share|improve this question
























  • What exactly you are trying to do ? and why ? Can you share more details with multiple data ?
    – C2486
    yesterday












  • @C2486 Well, my requirement is do to that way
    – Sanjay
    yesterday










  • @C2486 There's a sample data there!
    – Sanjay
    yesterday










  • what if divs: 3 ?
    – C2486
    yesterday










  • @C2486 Then it will render three divs and only after that div.content should render
    – Sanjay
    yesterday















up vote
1
down vote

favorite
1












I have the following code:



<div class="content">
<p>Hello World</p>
</div>


And I have the following div count:



data() {
return {
divs: 2
}
}


So, if divs is 2 then it should generate two divs and wrap the div.content inside it so it becomes something like this:



<div>
<div>
<div class="content">
<p>Hello World</p>
</div>
</div>
</div>


As you can see there's two divs and then only there's this div.content! I tried using v-for but it generates 2 separate divs! Please help!










share|improve this question
























  • What exactly you are trying to do ? and why ? Can you share more details with multiple data ?
    – C2486
    yesterday












  • @C2486 Well, my requirement is do to that way
    – Sanjay
    yesterday










  • @C2486 There's a sample data there!
    – Sanjay
    yesterday










  • what if divs: 3 ?
    – C2486
    yesterday










  • @C2486 Then it will render three divs and only after that div.content should render
    – Sanjay
    yesterday













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I have the following code:



<div class="content">
<p>Hello World</p>
</div>


And I have the following div count:



data() {
return {
divs: 2
}
}


So, if divs is 2 then it should generate two divs and wrap the div.content inside it so it becomes something like this:



<div>
<div>
<div class="content">
<p>Hello World</p>
</div>
</div>
</div>


As you can see there's two divs and then only there's this div.content! I tried using v-for but it generates 2 separate divs! Please help!










share|improve this question















I have the following code:



<div class="content">
<p>Hello World</p>
</div>


And I have the following div count:



data() {
return {
divs: 2
}
}


So, if divs is 2 then it should generate two divs and wrap the div.content inside it so it becomes something like this:



<div>
<div>
<div class="content">
<p>Hello World</p>
</div>
</div>
</div>


As you can see there's two divs and then only there's this div.content! I tried using v-for but it generates 2 separate divs! Please help!







javascript html vue.js vuejs2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday

























asked yesterday









Sanjay

510113




510113












  • What exactly you are trying to do ? and why ? Can you share more details with multiple data ?
    – C2486
    yesterday












  • @C2486 Well, my requirement is do to that way
    – Sanjay
    yesterday










  • @C2486 There's a sample data there!
    – Sanjay
    yesterday










  • what if divs: 3 ?
    – C2486
    yesterday










  • @C2486 Then it will render three divs and only after that div.content should render
    – Sanjay
    yesterday


















  • What exactly you are trying to do ? and why ? Can you share more details with multiple data ?
    – C2486
    yesterday












  • @C2486 Well, my requirement is do to that way
    – Sanjay
    yesterday










  • @C2486 There's a sample data there!
    – Sanjay
    yesterday










  • what if divs: 3 ?
    – C2486
    yesterday










  • @C2486 Then it will render three divs and only after that div.content should render
    – Sanjay
    yesterday
















What exactly you are trying to do ? and why ? Can you share more details with multiple data ?
– C2486
yesterday






What exactly you are trying to do ? and why ? Can you share more details with multiple data ?
– C2486
yesterday














@C2486 Well, my requirement is do to that way
– Sanjay
yesterday




@C2486 Well, my requirement is do to that way
– Sanjay
yesterday












@C2486 There's a sample data there!
– Sanjay
yesterday




@C2486 There's a sample data there!
– Sanjay
yesterday












what if divs: 3 ?
– C2486
yesterday




what if divs: 3 ?
– C2486
yesterday












@C2486 Then it will render three divs and only after that div.content should render
– Sanjay
yesterday




@C2486 Then it will render three divs and only after that div.content should render
– Sanjay
yesterday












2 Answers
2






active

oldest

votes

















up vote
1
down vote













Here is the example to make multiple Div provided in vue data. with vue components.






Vue.config.devtools = false;
Vue.config.productionTip = false;

Vue.component('my-div', {
props:['mydata','divs'],
template: '<div class="test"><div v-if="divs==1" class="text-center">{{mydata}}</div><div v-if="divs>1"><my-div :mydata="mydata" :divs="divs-1"></my-div></div></div>'
})

var v = new Vue({
el: '#app',
data:{
name:'Niklesh Raut',
divs:4
}
});

#app div.test{
border: 2px solid blue;
padding:5px;
}
.text-center{
text-align:center;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<my-div :mydata="name" :divs="divs"></my-div>
</div>








share|improve this answer

















  • 1




    That is a great answer.
    – Marc Newton
    yesterday


















up vote
0
down vote













First off: this sounds like a pretty unnecessary requirement and most probably re-framing the problem will lead to a much better solution!



That being said, there is a way to achieve what you're trying to do with some dark Vue magic ;-)



What you need is a recursive render function:



render: function (createElement) {
return this.level >= 1 ?
createElement('div', [createElement(DynamicDiv, {
props: {
level: this.level-1
}
}, this.$slots.default)]) :
createElement('div', this.$slots.default)
},
props: {
level: {
type: Number,
required: true
}
}


https://vuejs.org/v2/guide/render-function.html



You can find a working example over here: https://codesandbox.io/s/k9p16wzmyo






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238113%2fhow-to-dynamically-generate-div-in-vue%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Here is the example to make multiple Div provided in vue data. with vue components.






    Vue.config.devtools = false;
    Vue.config.productionTip = false;

    Vue.component('my-div', {
    props:['mydata','divs'],
    template: '<div class="test"><div v-if="divs==1" class="text-center">{{mydata}}</div><div v-if="divs>1"><my-div :mydata="mydata" :divs="divs-1"></my-div></div></div>'
    })

    var v = new Vue({
    el: '#app',
    data:{
    name:'Niklesh Raut',
    divs:4
    }
    });

    #app div.test{
    border: 2px solid blue;
    padding:5px;
    }
    .text-center{
    text-align:center;
    }

    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
    <div id="app">
    <my-div :mydata="name" :divs="divs"></my-div>
    </div>








    share|improve this answer

















    • 1




      That is a great answer.
      – Marc Newton
      yesterday















    up vote
    1
    down vote













    Here is the example to make multiple Div provided in vue data. with vue components.






    Vue.config.devtools = false;
    Vue.config.productionTip = false;

    Vue.component('my-div', {
    props:['mydata','divs'],
    template: '<div class="test"><div v-if="divs==1" class="text-center">{{mydata}}</div><div v-if="divs>1"><my-div :mydata="mydata" :divs="divs-1"></my-div></div></div>'
    })

    var v = new Vue({
    el: '#app',
    data:{
    name:'Niklesh Raut',
    divs:4
    }
    });

    #app div.test{
    border: 2px solid blue;
    padding:5px;
    }
    .text-center{
    text-align:center;
    }

    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
    <div id="app">
    <my-div :mydata="name" :divs="divs"></my-div>
    </div>








    share|improve this answer

















    • 1




      That is a great answer.
      – Marc Newton
      yesterday













    up vote
    1
    down vote










    up vote
    1
    down vote









    Here is the example to make multiple Div provided in vue data. with vue components.






    Vue.config.devtools = false;
    Vue.config.productionTip = false;

    Vue.component('my-div', {
    props:['mydata','divs'],
    template: '<div class="test"><div v-if="divs==1" class="text-center">{{mydata}}</div><div v-if="divs>1"><my-div :mydata="mydata" :divs="divs-1"></my-div></div></div>'
    })

    var v = new Vue({
    el: '#app',
    data:{
    name:'Niklesh Raut',
    divs:4
    }
    });

    #app div.test{
    border: 2px solid blue;
    padding:5px;
    }
    .text-center{
    text-align:center;
    }

    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
    <div id="app">
    <my-div :mydata="name" :divs="divs"></my-div>
    </div>








    share|improve this answer












    Here is the example to make multiple Div provided in vue data. with vue components.






    Vue.config.devtools = false;
    Vue.config.productionTip = false;

    Vue.component('my-div', {
    props:['mydata','divs'],
    template: '<div class="test"><div v-if="divs==1" class="text-center">{{mydata}}</div><div v-if="divs>1"><my-div :mydata="mydata" :divs="divs-1"></my-div></div></div>'
    })

    var v = new Vue({
    el: '#app',
    data:{
    name:'Niklesh Raut',
    divs:4
    }
    });

    #app div.test{
    border: 2px solid blue;
    padding:5px;
    }
    .text-center{
    text-align:center;
    }

    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
    <div id="app">
    <my-div :mydata="name" :divs="divs"></my-div>
    </div>








    Vue.config.devtools = false;
    Vue.config.productionTip = false;

    Vue.component('my-div', {
    props:['mydata','divs'],
    template: '<div class="test"><div v-if="divs==1" class="text-center">{{mydata}}</div><div v-if="divs>1"><my-div :mydata="mydata" :divs="divs-1"></my-div></div></div>'
    })

    var v = new Vue({
    el: '#app',
    data:{
    name:'Niklesh Raut',
    divs:4
    }
    });

    #app div.test{
    border: 2px solid blue;
    padding:5px;
    }
    .text-center{
    text-align:center;
    }

    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
    <div id="app">
    <my-div :mydata="name" :divs="divs"></my-div>
    </div>





    Vue.config.devtools = false;
    Vue.config.productionTip = false;

    Vue.component('my-div', {
    props:['mydata','divs'],
    template: '<div class="test"><div v-if="divs==1" class="text-center">{{mydata}}</div><div v-if="divs>1"><my-div :mydata="mydata" :divs="divs-1"></my-div></div></div>'
    })

    var v = new Vue({
    el: '#app',
    data:{
    name:'Niklesh Raut',
    divs:4
    }
    });

    #app div.test{
    border: 2px solid blue;
    padding:5px;
    }
    .text-center{
    text-align:center;
    }

    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
    <div id="app">
    <my-div :mydata="name" :divs="divs"></my-div>
    </div>






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered yesterday









    C2486

    18.2k32360




    18.2k32360








    • 1




      That is a great answer.
      – Marc Newton
      yesterday














    • 1




      That is a great answer.
      – Marc Newton
      yesterday








    1




    1




    That is a great answer.
    – Marc Newton
    yesterday




    That is a great answer.
    – Marc Newton
    yesterday












    up vote
    0
    down vote













    First off: this sounds like a pretty unnecessary requirement and most probably re-framing the problem will lead to a much better solution!



    That being said, there is a way to achieve what you're trying to do with some dark Vue magic ;-)



    What you need is a recursive render function:



    render: function (createElement) {
    return this.level >= 1 ?
    createElement('div', [createElement(DynamicDiv, {
    props: {
    level: this.level-1
    }
    }, this.$slots.default)]) :
    createElement('div', this.$slots.default)
    },
    props: {
    level: {
    type: Number,
    required: true
    }
    }


    https://vuejs.org/v2/guide/render-function.html



    You can find a working example over here: https://codesandbox.io/s/k9p16wzmyo






    share|improve this answer

























      up vote
      0
      down vote













      First off: this sounds like a pretty unnecessary requirement and most probably re-framing the problem will lead to a much better solution!



      That being said, there is a way to achieve what you're trying to do with some dark Vue magic ;-)



      What you need is a recursive render function:



      render: function (createElement) {
      return this.level >= 1 ?
      createElement('div', [createElement(DynamicDiv, {
      props: {
      level: this.level-1
      }
      }, this.$slots.default)]) :
      createElement('div', this.$slots.default)
      },
      props: {
      level: {
      type: Number,
      required: true
      }
      }


      https://vuejs.org/v2/guide/render-function.html



      You can find a working example over here: https://codesandbox.io/s/k9p16wzmyo






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        First off: this sounds like a pretty unnecessary requirement and most probably re-framing the problem will lead to a much better solution!



        That being said, there is a way to achieve what you're trying to do with some dark Vue magic ;-)



        What you need is a recursive render function:



        render: function (createElement) {
        return this.level >= 1 ?
        createElement('div', [createElement(DynamicDiv, {
        props: {
        level: this.level-1
        }
        }, this.$slots.default)]) :
        createElement('div', this.$slots.default)
        },
        props: {
        level: {
        type: Number,
        required: true
        }
        }


        https://vuejs.org/v2/guide/render-function.html



        You can find a working example over here: https://codesandbox.io/s/k9p16wzmyo






        share|improve this answer












        First off: this sounds like a pretty unnecessary requirement and most probably re-framing the problem will lead to a much better solution!



        That being said, there is a way to achieve what you're trying to do with some dark Vue magic ;-)



        What you need is a recursive render function:



        render: function (createElement) {
        return this.level >= 1 ?
        createElement('div', [createElement(DynamicDiv, {
        props: {
        level: this.level-1
        }
        }, this.$slots.default)]) :
        createElement('div', this.$slots.default)
        },
        props: {
        level: {
        type: Number,
        required: true
        }
        }


        https://vuejs.org/v2/guide/render-function.html



        You can find a working example over here: https://codesandbox.io/s/k9p16wzmyo







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        TommyF

        1,0071725




        1,0071725






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238113%2fhow-to-dynamically-generate-div-in-vue%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Full-time equivalent

            Bicuculline

            さくらももこ