一、说明
用了Layui的界面,加上vue的指令:v-model ,v-bind ,v-for 等
- 1、注意:开关按钮如果是关闭状态:status字段是不会在表单提交字段中的,打开了就是status:1,所以表单编辑时如果开关由打开变成关闭了,我们的表单数据field需加上status判断,如下:
field.status = field.status ? field.status : 0;
- 2、点开类型下拉框会发现默认选到最后一个,lay-verify="required"校验失效;所以我们需要设置type的默认值为’’
二、效果
-
1、默认页面 -
2、点击 [ 回显 ] 页面
三、代码
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>VUE+Layui 输入框、下拉选择框、复选框、开关、文本域 数据回显</title>
</head>
<link rel="stylesheet" type="text/css" href="https://www.layuicdn.com/layui/css/layui.css" v="2.5.6" e="layui"/>
<style>
.w100 {
width: 200px;
}
</style>
<body>
<div class="layui-form" id="vue-mount-element" style="padding: 30px 50px 0 0;">
<div class="layui-form-item">
<label class="layui-form-label w100">输入框:标题</label>
<div class="layui-input-inline">
<input type="text" lay-verify="required" name="title" :value="info.title" placeholder="请输入标题"
autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label w100">下拉单选框:类型</label>
<div class="layui-input-inline">
<select name="type" lay-verify="required" v-model="info.type">
<option value="">请选择类型</option>
<option v-for="item in types" :value="item.id">{{item.name}}</option>
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label w100">复选框:爱好</label>
<div class="layui-input-block">
<input type="checkbox" v-for="item in hobbyList"
:disabled="disableHobbyId.length != 0 && (disableHobbyId.indexOf(parseInt(item.id)) != -1)"
name="hobby2[]" :value="item.id" :title="item.name" v-model="info.chooseHobbyId" lay-skin="primary"/>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label w100">开关:状态(默认开启)</label>
<div class="layui-input-inline">
<input type="checkbox" name="status" value="1" v-model="info.status" lay-skin="switch" lay-text="开启|关闭">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label w100">文本域:描述</label>
<div class="layui-input-inline">
<textarea name="desc" placeholder="请输入描述" class="layui-textarea">{{info.desc}}</textarea>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label w100">
</label>
<div class="layui-input-block">
<button class="layui-btn layuiadmin-btn-admin" lay-submit>
提交
</button>
<button class="layui-btn layui-btn-warm" id="backButton">
回显
</button>
<button class="layui-btn layui-btn-normal" id="reductionButton">
还原
</button>
</div>
</div>
</div>
</body>
<script src="https://www.layuicdn.com/layui/layui.js" v="2.5.6" e="layui.all"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
let hobbyList = [
{id: 1, name: "爱好1"},
{id: 2, name: "爱好2"},
{id: 3, name: "爱好3"},
{id: 4, name: "爱好4"},
{id: 5, name: "爱好5"},
{id: 6, name: "爱好6"},
];
let disableHobbyId = [2, 4];
let types = [
{id: 1, name: "类型1"},
{id: 2, name: "类型2"},
{id: 3, name: "类型3"},
];
let info = {};
let defaultInfo = {type: '', status: 1, chooseHobbyId: []};
if (JSON.stringify(info) == "{}") {
info = defaultInfo;
}
let backInfo = {title: "这是输入的标题", type: 2, status: 0, desc: "这是输入描述的内容", chooseHobbyId: [1, 2]};
let vueObject = new Vue({
el: '#vue-mount-element',
data() {
return {
info: info,
types: types,
hobbyList: hobbyList,
disableHobbyId: disableHobbyId
};
}
})
layui.use(['laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'form'], function () {
var $ = layui.$
, laydate = layui.laydate
, laypage = layui.laypage
, layer = layui.layer
, table = layui.table
, carousel = layui.carousel
, upload = layui.upload
, form = layui.form
, element = layui.element;
form.on('submit', function (data) {
let field = data.field;
field = JSON.stringify(field);
layer.alert("表单的内容为:" + field);
});
$('#backButton').click(function () {
vueObject.info = backInfo;
setTimeout(function () {
form.render();
}, 100);
});
$('#reductionButton').click(function () {
vueObject.info = defaultInfo;
setTimeout(function () {
form.render();
}, 100);
});
});
</script>
</html>
四、如果失效了
|