jquey+bootstrap实现表单拖拽并设置属性生成表单json数据代码
代码语言:html
所属分类:表单美化
代码描述:jquey+bootstrap实现表单拖拽并设置属性生成表单json数据代码
代码标签: 拖拽 并 设置 属性 生成 表单 json 数据
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html>
<html lang="en">
<head>
<title>拖拽表单</title>
<!-- Required meta tags -->
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/jquery.1.11.min.js"></script>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://at.alicdn.com/t/font_2716841_5q133u7t0gp.css" />
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link type="text/css" rel="stylesheet" href="//repo.bfw.wiki/bfwrepo/css/bootstrap.3.3.4.css">
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/bootstrap-3.3.4.js"></script>
<style>
.controlRealName i {
margin-right: 3px;
}
.controlName {
width: 48%;
float: left;
padding: 5px;
border: 1px solid #ededed;
margin: 0 1% 20px 1%;
border-radius: 2px;
color: #777;
}
.interval {
width: 96%;
margin: 0 2%;
padding: 0 2%;
background: #ffffff;
float: left;
/* overflow-y: auto;
overflow-x: hidden; */
}
.controlImgName {
width: 20px;
float: left;
height: 20px;
overflow: hidden;
position: relative;
}
.controlImgName img {
width: 100%;
position: absolute;
top: 0;
left: 0;
}
.controlRealName {
float: left;
font-size: 14px;
}
.controlName:hover {
color: #0377ed;
cursor: move;
border: 1px solid #0377ed;
}
.LdgFormTitle {
text-align: center;
font-size: 18px;
}
.LdgFormSubject {
width: 100%;
padding: 0px 10px;
float: left;
position: relative;
height: 100%;
overflow: auto;
background-color: #fff;
}
.LdgFormItemHead {
padding: 7px 6px;
width: 100%;
}
.LdgFormItemContent .checkbox,
.LdgFormItemContent .checkbox-inline,
.LdgFormItemContent .radio,
.LdgFormItemContent .radio-inline {
padding-top: 7px;
margin-top: 0;
margin-bottom: 0;
}
.LdgFormItemName {
float: left;
}
.LdgFormItemGroupButton {
position: absolute;
right: 0;
bottom: 0;
}
.LdgFormItemGroupButton i {
margin-left: 2px;
background-color: #22aaff;
padding: 5px;
color: white;
cursor: pointer;
}
.LdgFormItemGroupButton i.controlMove {
cursor: move;
}
.LdgFormItemID {
background-color: #22aaff;
color: white;
padding: 2px;
margin-right: 5px;
}
.LdgFormItemContent {
width: 100%;
}
.LdgFormItemContent::before,
.LdgFormItemContent::after {
display: table;
content: '';
}
.checkMargin {
margin-top: 7px;
}
.selectInputDiv {
display: none;
border: 1px solid #ccc;
width: 92%;
padding: 4px 6px;
box-shadow: 1px 1px 0px 0px #ccc;
position: absolute;
z-index: 10000;
background: #fff;
overflow: auto;
}
.selectInputDiv p {
margin-bottom: 0px;
padding: 0.5rem 0;
}
.selectInputDiv p:hover {
background: #0377ed;
color: #fff;
}
.full {
width: 100%;
float: left;
}
.z_photo {
padding: 5px;
border: 1px dashed #e7e6e6;
}
.z_file {
width: 120px;
height: 120px;
margin: 0 auto;
position: relative;
}
.add-img {
display: block;
width: 120px;
height: 120px;
}
.imgFile {
width: 100%;
height: 100%;
opacity: 0;
position: absolute;
top: 0px;
left: 0px;
z-index: 100;
}
.imgExplain {
font-size: 12px;
color: #888;
float: left;
}
.errerClass {
display: none;
float: left;
font-size: 14px;
color: #ff0000;
}
.message {
float: left;
width: 100%;
overflow: hidden;
position: relative;
}
.message .input {
width: 70%;
float: left;
}
.message .liulan {
width: 30%;
height: 38px;
padding: 4px 6px;
background: url(images/file.png) left center no-repeat;
text-indent: 10px;
cursor: pointer;
border: 1px solid #ccc;
border-radius: 3px;
}
.message .files {
position: absolute;
left: 0px;
top: 0px;
cursor: pointer;
filter: Alpha(opacity=0);
opacity: 0;
display: none;
}
.fileTips {
float: left;
}
.fileTips p {
color: #888 !important;
font-size: 12px !important;
margin: 0px !important;
}
.LdgFormButton {
border-bottom: 1px solid #e7e6e6;
padding: 5px;
margin-bottom: 5px;
}
.btn:focus,
.btn:active:focus,
.btn.active:focus,
.btn.focus,
.btn:active.focus,
.btn.active.focus {
outline: none;
box-shadow: none;
}
.LdgFormAttribute,
.LdgFormConfigs,
.LdgFormControls {
width: 96%;
margin: 0 2%;
padding: 0 2%;
float: left;
position: relative;
overflow-y: auto;
overflow-x: hidden;
}
.textRegularCustom {
display: none;
}
.placeLocationTop {
border-top: 3px solid #22aaff;
}
.placeLocationBottom {
border-bottom: 3px solid #22aaff;
}
.LdgFormItem {
width: 100%;
}
.required {
float: left;
color: #f00;
margin: 0 2px;
}
.requiredHiddle {
display: none;
}
.previewMain {
position: absolute;
width: 800px;
height: 600px;
display: none;
margin: 0 auto;
border: 1px solid #000;
background: #fff;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.errorMain {
line-height: 40px;
height: 40px;
overflow: hidden;
color: #f00;
width: 100%;
display: none;
}
.hidMargin {
margin-bottom: 0px !important;
}
.z_fileImgDel {
position: absolute;
width: 100%;
text-align: center;
background: #000;
opacity: 0.7;
color: #fff;
bottom: 0px;
z-index: 101;
}
.messagedel {
position: absolute;
z-index: 101;
width: 120px;
height: 100%;
text-align: center;
line-height: 38px;
background: #000;
opacity: 0.5;
color: #fff;
display: none;
right: 30%;
}
.LdgFormTitle {
border-bottom: 1px solid #e7e6e6;
padding: 5px;
margin-bottom: 5px;
}
.LdgDiyForm {
border: 2px solid #e7e6e6;
}
.LdgDiyForm,
.LdgDiyForm > div {
height: 100%;
position: relative;
}
.LdgDiyForm > div:nth-child(1) {
background-color: #fff;
width: 250px;
float: left;
border-right: 1px solid #e7e6e6;
}
.LdgDiyForm > div:nth-child(2) {
width: calc(100% - 254px);
margin-left: 250px;
padding: 4px;
position: absolute;
}
.LdgDiyForm > div:nth-child(3),
.LdgDiyForm > div:nth-child(4) {
display: none;
border-left: 1px solid #e7e6e6;
}
.hasProperties.LdgDiyForm > div:nth-child(2) {
width: calc(100% - 504px);
position: absolute;
}
.hasProperties.LdgDiyForm > .showAttr {
/*div:nth-child(3)*/
background-color: #fff;
width: 250px;
float: right;
display: flex;
flex-direction: column;
}
.LdgFormSubjectForm .row {
margin: 3px auto;
}
.ldgPrew .LdgFormItem {
border: rgb(25, 178, 238) 1px dashed;
background-color: rgba(236, 245, 255, 0.3);
padding: 3px;
position: relative;
}
.ldgLeft .LdgFormItemHead,
.ldgRight .LdgFormItemHead {
width: 100px;
}
.ldgLeft .LdgFormItem,
.ldgRight .LdgFormItem {
width: 100%;
display: flex;
}
.ldgLeft .LdgFormItemContent {
float: left;
}
.ldgRight .LdgFormItemName {
float: right;
}
.ldgRight .required {
float: right;
}
</style>
</head>
<body style="height: 100%; position: fixed; width: 100%">
<div id="app"></div>
<script>
//-----------------------------------------全局变量-------------------------------------------
var mouseX //鼠标的位置
var mouseY //鼠标的位置
var controlHtml //被选中的控件
var isCanBePlaced = false //是否可以放置
var isDownControl = false //是否按下了控件
var formLeft = 0 //form表单的坐标
var formTop = 0
var formRight = 0
var formBottom = 0
var object //中部控件
var objectControl //中部控件点击后记录控件信息
var objectIndex //中部控件正在操作的id
var numberRegExp = '^[0-9]+$' //纯数字表达式
var letterRegExp = '^[a-zA-Z]+$' //纯字母
var chineseRegExp = '^[\u4e00-\u9fa5]{0,}$' //中文
var emailRegExp = '^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$' //邮箱
var mobileRegExp = '^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$' //手机号码
var idRegExp = '^((d{18})|([0-9x]{18})|([0-9X]{18}))$' //身份证
var ldgDefaultBtns = {
//默认按钮 移动和删除
move: { class: 'icon-yidong controlMove' },
delete: { class: 'icon-shanchu controlDelete' },
}
var contorlCache = []
//-----------------------------------------初始化方法及页面加载方法-------------------------------------------
function Initialization(Parameters) {
//先清除对应div中的html代码
let el = '.DiyForm'
if (Parameters['el']) {
el = Parameters['el']
}
$(el).empty()
let height = '100%'
if (Parameters['height']) {
height = Parameters['height']
}
$(el).height(height)
var main = '<div class="LdgDiyForm" style="background:#edededa1;">'
main += leftToolbar()
main += centerMain()
main += rightAttribute()
main += '</div><div class="dragControl"></div>'
main += '<div class="previewMain"></div>'
//添加基本的界面布局
$(el).append(main)
$('.interval').css('height', document.body.offsetHeight + 'px') //初始化高度,跟浏览器可视高度一致
}
function GetBtns(btns, id) {
let str = '<!--表单控件的功能按钮-->'
str += '<div class="LdgFormItemGroupButton">'
str += '<span class="LdgFormItemID">' + id + '</span>'
if (btns) {
for (let key in btns) {
str += '<i class="iconfont ' + btns[key].class + '"></i>'
}
} else {
for (let key in ldgDefaultBtns) {
str += '<i class="iconfont ' + ldgDefaultBtns[key].class + '"></i>'
}
}
str += '</div>'
return str
}
function GetRequired(required) {
if (required) {
return '<div class="required">*</div>'
}
return ''
}
function GetCheck(required, check = false) {
if (required || check) {
return '<div class="errorMain"></div>'
}
return ''
}
function getRadioByTextAreaValue(TextAreaValue, optionvalue, id) {
let spilts = TextAreaValue.split('\n')
let str = ''
spilts.forEach(function (item) {
if (item != '') {
let checked = ''
if (item == optionvalue) {
checked = " checked=''"
}
str += `<div class="radio"><label><input type="radio" name="${id}" value="${item}"${checked}>${item}</label></div>`
}
})
return str
}
function getCheckBoxByTextAreaValue(TextAreaValue, optionvalue, id) {
let spilts = TextAreaValue.split('\n')
let spiltso = optionvalue.split('\n')
let str = ''
spilts.forEach(function (item) {
if (item != '') {
let checked = ''
spiltso.forEach(function (item2) {
if (item == item2) {
checked = " checked=''"
}
})
str += `<div class="checkbox"><label><input name="${id}" type="checkbox" value="${item}"${checked}>${item}</label></div>`
}
})
return str
}
function getSelectByTextAreaValue(TextAreaValue, optionvalue, id) {
let spilts = TextAreaValue.split('\n')
if (!optionvalue || optionvalue.indexOf('请选择') == 0) {
optionvalue = ''
}
let str = '<select name="${id}" class="form-control">'
spilts.forEach(function (item) {
let itemvalue = item
if (itemvalue.indexOf('请选择') == 0) {
itemvalue = ''
}
if (item != '') {
let selected = ''
if (itemvalue == optionvalue) {
selected = " selected=''"
}
str += `<option value="${itemvalue}"${selected}>${item}</option>`
}
})
str += '</select>'
return str
}
function getTextSelectByTextAreaValue(TextAreaValue, id) {
let spilts = TextAreaValue.split('\n')
let str = '<input type="text" name="${id}" class="form-control selectInput formVal"><div class="selectInputDiv" title="下拉菜单">'
spilts.forEach(function (item) {
if (item != '') {
str += '<p class="selectInputDivP">' + item + '</p>'
}
})
str += '</div>'
return str
}
let ldgFormConfig={
width: { name: '组件标签宽度', value: '100', type: 'number' },
align: { name: '标签对齐方式', value: 0, options: { 右侧对齐: 0, 顶部对齐: 1, 左侧对齐: 2 }, type: 'radio' },
}
let controlL.........完整代码请登录后点击上方下载按钮下载查看
















网友评论0