我需要能够在输入字段和标签之间切换 . 单击“添加位置”按钮(创建新div)时,输入字段必须可见 . 但是当div“Expandable”最大化时,必须隐藏它并且标签可见!
输入字段只应在单击提到的按钮后立即可见,否则标签必须取代它 . 实现这一目标的最佳方法是什么?我正在考虑使用某种切换,因为我在其他地方使用它 .
标签和输入字段放在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',
});
3 回答
你有一些错过的结束标签和
InputFieldInfo
的错误,它应该有一个小写的i
.只需按下每个按钮,您就可以基本切换
inputFieldInfo
数据 .该模板有一些编译错误:
<label>
需要一个结束标记(文本内容有用)<div class="big">
需要一个结束标记v-if
绑定到inputFieldInfo
,但该变量被声明为InputFieldInfo
(注意大写I
),但根据您的行为描述,此字段对于每个位置容器应该是唯一的,因此像这样的单个数据属性将不起作用(如果我理解你的描述正确) .每个位置容器应该有一个变量来包含位置名称(例如,
locationName
)和另一个变量以包含<input>
和<label>
(即inputFieldInfo
)的show / hide布尔值:然后,我们可以将
div.inputFieldInfo
和div.locationName
绑定到<input>
. 我们绑定到v-model
,以便用户的文本自动反映到div.locationName
变量:<label>
的内容应为div.locationName
,以便在显示时包含<input>
中的文本:要在单击展开按钮时使用
<label>
切换<input>
,我们更新expand()
以将div.inputFieldInfo
设置为false
但仅当div.locationName
不为空时(这使用户有机会重新访问/重新展开容器以填充位置以后如果需要):updated jsfiddle
你可以通过使用像这样的切换变量来做到这一点
在这个例子中,我们只是在click上输入takeinput的值,因此根据值显示标签或输入 .
这是非常基本的例外 . 但您可以根据需要扩展它