【发布时间】:2018-11-09 00:23:15
【问题描述】:
我需要能够在输入字段和标签之间切换。单击“添加位置”按钮(创建一个新的 div)时,输入字段必须可见。但是当 div“可扩展”被最大化时,它必须被隐藏并且标签可见!
输入字段应仅在单击提到的按钮后立即可见,否则标签必须代替它。实现这一目标的最佳方法是什么?我正在考虑使用某种切换,因为我在其他地方使用它。
标签和输入字段放在div类“switch”中。
你也可以在这个jsFiddle看到代码!
HTML
<div id="lotsOfDivs">
<addingdivs></addingdivs>
</div>
Vue
var gate = 0;
Vue.component('addingdivs', {
template: `
<div>
<div id="header">
<button class="addDiv" type="button" @click="createDiv">ADD LOCATION</button>
</div>
<div class="parent" v-for="div in divs" :style=" div.height ? { 'height': div.height }: null">
<div class="big" v-if="div.expanded" :key="'expanded' + div.id">
<div class="switch">
<input type="text" v-if="inputFieldInfo">
<label class="propertyLabel" v-else>
<div class="firstChild">
<button class="done" @click="increaseLimit">INCREASE</button>
</div>
<div class="secondChild">
<button class="done" @click="expand(div)">EXPAND</button>
</div>
</div>
<div class="small" v-else :key="'collapsed' + div.id">
<button class="done" @click="expand(div)">EXPAND</button>
</div>
</div>
</div>
`,
data: function() {
return {
gate: gate,
height: "",
count: 0,
locationsArr: ["one", "two", "three"],
divs: [],
InputFieldInfo: false
}
},
methods: {
expand: function(div) {
if (div.expanded) {
div.expanded = false
this.height = ''
} else {
div.expanded = true
this.height = '7vh'
}
},
createDiv: function() {
if (this.count <= gate) { // Here you can decide how many divs that will be generated
// this.count++;
this.divs.push({
id: this.count,
expanded: true,
inputFieldInfo: true,
height: '',
});
this.count++
}},
increaseLimit: function() {
// Here you can increase the number of divs that it's possible to generate
gate++;
}
}
});
new Vue({
el: '#lotsOfDivs',
});
【问题讨论】: