当前位置:   article > 正文

vue 动态添加form表单_x-form表单组件如何动态添加

x-form表单组件如何动态添加

一、效果图

在这里插入图片描述

二、代码块

<template>
	<!-- 动态添加form表单-->
  <div class="hello">
	  <van-nav-bar
	    title="标题"
	    left-text="返回"
	    left-arrow
	    @click-left="onClickLeft"
	  />
	<!-- 动态表单增加 -->
	<div v-for="(item,index) in list" style="width: 94%;margin: 0 auto;">
		<van-row>
		  <van-col span="18" style="text-align: left;color: #006AF1;line-height: 40px;font-size: 16px;">专利申请明细-{{index+1}}</van-col>
		  <van-col span="6" style="text-align: right;line-height: 40px;">
			  <van-icon
			   v-if="list.length > 1"
			   @click="del(index)" 
			   name="delete-o" 
			  />
		  </van-col>
		</van-row>
		<van-form>
			<van-field
			v-model="item.name"
			name="用户名"
			label="用户名"
			placeholder="用户名"
			/>
			<van-field
			  v-model="item.content"
			  is-link
			  readonly
			  clickable
			  name="picker"
			  label="城市"
			  placeholder="点击选择城市"
			  @click="selectCity(index)"
			/>	
			<van-field
			  v-model="item.startDate"
			  is-link
			  readonly
			  name="datetimePicker"
			  label="开始日期"
			  placeholder="点击选择开始日期"
			  @click="dateShow(item,index,'start')"
			  />
			  <van-field
			    v-model="item.endDate"
			    is-link
			    readonly
			    name="datetimePicker"
			    label="结束日期"
			    placeholder="点击选择结束日期"
			    @click="dateShow(item,index,'end')"
			    />
			<van-field
			 v-model="item.remarks"
			 type="textarea"
			 name="备注"
			 label="备注"
			 placeholder="备注"
			 style="display: inline-block;"
			/>
		</van-form>	
	</div>
	<div style="width: 50%;margin: 0 auto;">
		<van-row v-if="list.length < 5">
			<van-col :span="11">
				<van-button round block type="info" size="small" @click="addForm" >新增</van-button>
			</van-col>
			<van-col :span="10" :offset="1">
				<van-button round block @click="save" size="small" type="primary">保存</van-button>
			</van-col>
		</van-row>
		<van-row v-else>
			<van-button round block @click="save" size="small" type="primary">保存</van-button>
		</van-row>
	</div>
<!-- 	<div style="height: 50px;"></div>
	<van-row>
	  <van-col span="24" style="position: fixed;bottom: 0;">
		  <van-button  block @click="save" size="small" type="info">保存</van-button>
	  </van-col>
	</van-row> -->
	
	<!-- 多选 -->
	<van-popup v-model="showPicker" position="bottom">
	  <van-picker
	    v-model="currentDate"
		show-toolbar
		:columns="columns"
		@confirm="onConfirm"
		@cancel="showPicker = false"
	  />
	  </van-picker>
	</van-popup>
	
	<!-- 日期 -->
	<van-popup v-model="dateShowPicker" position="bottom">
		<van-datetime-picker
		  v-model="currentDate1"
		  type="date"
		  title="选择年月日"
		  :min-date="minDate"
		  :max-date="maxDate"
		  @confirm="dateConfirm"
		  @cancel="dateShowPicker = false"
		/>
	</van-popup>
	
  </div>
</template>

<script>
import Toast from 'vant/lib/toast';
export default {
  data () {
    return {
		
	// *****日期*************************
		index:0,
		startOrEnd:'',
		dateShowPicker:false,
		minDate: new Date(2000, 0, 1),
		maxDate: new Date(2100, 11, 31),
		currentDate: new Date(),	//默认当前日期
		currentDate1: new Date(),	//默认当前日期
	// **********************************
		
		msg: 'Welcome to Your Vue.js App',
		list:[{
		  name:'Champion',
		  content:'临汾',
		  remarks:'',
		  startDate:'',
		  endDate:''
		}],
		showPicker: false,
		columns: ['临汾', '长治', '运城','晋城', '吕梁', '忻州','朔州','大同','晋中','阳泉','太原'],
		cityIndex:0,
    }
  },
	methods:{
		// 返回上一级
		onClickLeft(){
			window.history.back()
			// window.history.go(-1)
		},
// *************动态增加form表单************************
		addForm(){
			if(this.list.length <= 5){
				this.list.push({
					name:'',
					content:'',
					remarks:'',
					startDate:this.getTime(new Date()),
					endDate:this.getTime(new Date())
				})				
			}else{}
		},
		// 删除
		del(index){
			this.list.splice(index,1)
		},
		// 选择框显示
		selectCity(index){
			this.cityIndex = index;
			this.showPicker = true
		},
		// 选择框确认
		onConfirm(value) {
		  this.list[this.cityIndex].content = value;
		  this.showPicker = false;
		},
		// 保存,获取form表单的值
		save(){
			this.$message.success(JSON.stringify(this.list));
		},
// ***************日期***********************************************
		dateShow(m,n,k){
			this.index = n;
			this.startOrEnd = k;
			this.dateShowPicker = true
		},
		dateConfirm(val){
			let year = val.getFullYear()
			let month = val.getMonth() + 1
			let day = val.getDate()
			if (month >= 1 && month <= 9) {
				month = `0${month}`
			}
			if (day >= 1 && day <= 9) {
				day = `0${day}`
			}
			if(this.startOrEnd == 'start'){
				this.list[this.index].startDate = year + "-" + month + "-" + day
			}else if(this.startOrEnd == 'end'){
				this.list[this.index].endDate = year + "-" + month + "-" + day
			}
			
			this.dateShowPicker = false;
		},
		
		// 封装公共的方法,获取当前时间;
		getTime(date){
			var year = date.getFullYear();
			var month = date.getMonth() + 1;
			var day = date.getDate();
			if(month < 9) {
				month = '0' + month;
			}
			if(day < 9){
				day = '0' + day;
			}
			var time = year + "-" + month + "-" + day;
			return time;
		}
	},
	created() {
		this.list[0].startDate = this.getTime(new Date());
		this.list[0].endDate = this.getTime(new Date());
	}
}
</script>
<style scoped>
	.hello{
		height: 100vh;
		background-color: rgb(247, 247, 247);
		overflow: auto;
	}
</style>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/91433
推荐阅读
相关标签