How to dynamically generate div in Vue?
up vote
1
down vote
favorite
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
|
show 3 more comments
up vote
1
down vote
favorite
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
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 ifdivs: 3
?
– C2486
yesterday
@C2486 Then it will render three divs and only after thatdiv.content
should render
– Sanjay
yesterday
|
show 3 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
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
javascript html vue.js vuejs2
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 ifdivs: 3
?
– C2486
yesterday
@C2486 Then it will render three divs and only after thatdiv.content
should render
– Sanjay
yesterday
|
show 3 more comments
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 ifdivs: 3
?
– C2486
yesterday
@C2486 Then it will render three divs and only after thatdiv.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
|
show 3 more comments
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>
1
That is a great answer.
– Marc Newton
yesterday
add a comment |
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
add a comment |
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>
1
That is a great answer.
– Marc Newton
yesterday
add a comment |
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>
1
That is a great answer.
– Marc Newton
yesterday
add a comment |
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>
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>
answered yesterday
C2486
18.2k32360
18.2k32360
1
That is a great answer.
– Marc Newton
yesterday
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered yesterday
TommyF
1,0071725
1,0071725
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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