BootstrapValidator使用

$(function() {
	to_page(1);
	addFormValidator();
	updateFormValidator();
});

$('#addModal').on('hidden.bs.modal', function() {
	$("#addform").data('bootstrapValidator').destroy();
	$('#addform').data('bootstrapValidator', null);
	$('#addform input').val("");
	$("input[name='position']").attr("checked", false);
	addFormValidator();
});
$('#updateModal').on('hidden.bs.modal', function() {
	$("#updateform").data('bootstrapValidator').destroy();
	$('#updateform').data('bootstrapValidator', null);
	$("#up_image").val("");
	updateFormValidator();
});
function addFormValidator() {
	$('#addform')
			.bootstrapValidator(
					{
						message : 'This value is not valid',
						feedbackIcons : {
							valid : 'glyphicon glyphicon-ok',
							invalid : 'glyphicon glyphicon-remove',
							validating : 'glyphicon glyphicon-refresh'
						},
						fields : {
							add_seNum : {
								validators : {
									notEmpty : {
										message : '序列号不能为空'
									},
									stringLength : {
										min : 1,
										max : 10,
										message : '序列号长度必须在1~10之间'
									},
									regexp : {
										regexp : /^[0-9]*$/,
										message : '序列号只能输入数字'
									}
								}
							},
							add_image : {
								validators : {
									notEmpty : {
										message : '请选择照片'
									},
									file : {
										extension : 'png,jpg,jpeg',
										type : 'image/png,image/jpg,image/jpeg',
										message : '上传文件不符合要求'
									}
								}

							},
							add_link : {
								validators : {
									notEmpty : {
										message : '图片链接不能为空'
									},
									stringLength : {
										min : 1,
										max : 100,
										message : '图片链接长度必须在1~100之间'
									},
									regexp : {
										regexp : /^((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?$/,
										message : '图片链接格式不符合要求'
									}
								}
							},
							add_color : {
								validators : {
									notEmpty : {
										message : '背景颜色不能为空'
									},
									stringLength : {
										min : 1,
										max : 10,
										message : '背景颜色长度必须在1~10之间'
									}
								}
							},
							add_openWith : {
								validators : {
									notEmpty : {
										message : '打开宽度不能为空'
									},
									stringLength : {
										min : 1,
										max : 10,
										message : '打开宽度长度必须在1~10之间'
									},
									regexp : {
										regexp : /^[0-9]*$/,
										message : '序列号只能输入数字'
									}
								}
							},
							position : {
								validators : {
									notEmpty : {
										message : '图片位置不能为空'
									}
								}
							}
						}
					});
	$("#add").click(function() {
		$("#addform").bootstrapValidator('validate');
		if ($("#addform").data('bootstrapValidator').isValid()) {
			var add_position = $("input[name='position']:checked").val();
			$("#addModal").modal('hide');
			var pageNum = $("#pageNum").html();
			var formData = new FormData($("#addform")[0]);
			formData.append("type", "0");
			formData.append("sourceUrl", YM());
			$.ajax({
				url : "/portal/adver/insertAdever",
				data : formData,
				datatype : "json",
				type : "post",
				async : false,
				cache : false,
				contentType : false,
				processData : false,
				success : function(result) {
					$("#div1").html(result);
					$("#publicModel").modal('show');
					setTimeout("hide()", 1000);
					to_page(pageNum);
				},
				error : function() {
					$("#div1").html("添加失败");
					$("#publicModel").modal('show');
					setTimeout("hide()", 1000)
				}
			});
		}
	});
}
function updateFormValidator() {
	$('#updateform')
			.bootstrapValidator(
					{
						message : 'This value is not valid',
						feedbackIcons : {
							valid : 'glyphicon glyphicon-ok',
							invalid : 'glyphicon glyphicon-remove',
							validating : 'glyphicon glyphicon-refresh'
						},
						fields : {
							up_seNum : {
								validators : {
									notEmpty : {
										message : '序列号不能为空'
									},
									stringLength : {
										min : 1,
										max : 10,
										message : '序列号长度必须在1~10之间'
									},
									regexp : {
										regexp : /^[0-9]*$/,
										message : '序列号只能输入数字'
									}
								}
							},
							up_image : {
								validators : {
									notEmpty : {
										message : '请选择照片'
									},
									file : {
										extension : 'png,jpg,jpeg',
										type : 'image/png,image/jpg,image/jpeg',
										message : '上传文件不符合要求'
									}
								}

							},
							up_link : {
								validators : {
									notEmpty : {
										message : '图片链接不能为空'
									},
									stringLength : {
										min : 1,
										max : 100,
										message : '图片链接长度必须在1~100之间'
									},
									regexp : {
										regexp : /^((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?$/,
										message : '图片链接格式不符合要求'
									}
								}
							},
							up_color : {
								validators : {
									notEmpty : {
										message : '背景颜色不能为空'
									},
									stringLength : {
										min : 1,
										max : 10,
										message : '背景颜色长度必须在1~10之间'
									}
								}
							},
							up_openWith : {
								validators : {
									notEmpty : {
										message : '打开宽度不能为空'
									},
									stringLength : {
										min : 1,
										max : 10,
										message : '打开宽度长度必须在1~10之间'
									},
									regexp : {
										regexp : /^[0-9]*$/,
										message : '打开宽度只能输入数字'
									}
								}
							},
							upposition : {
								validators : {
									notEmpty : {
										message : '图片位置不能为空'
									}
								}
							}
						}
					});
	$("#update").click(function() {
		$("#updateform").bootstrapValidator('validate');
		if ($("#updateform").data('bootstrapValidator').isValid()) {
			$("#updateModal").modal('hide');
			var pageNum = $("#pageNum").html();
			var formData = new FormData($("#updateform")[0]);
			formData.append("uuid", updateId);
			$.ajax({
				url : "/portal/adver/updateAdver",
				data : formData,
				datatype : "json",
				type : "post",
				async : false,
				cache : false,
				contentType : false,
				processData : false,
				success : function(result) {
					$("#div1").html(result);
					$("#publicModel").modal('show');
					setTimeout("hide()", 1000)
					to_page(pageNum);
				},
				error : function() {
					$("#div1").html("修改失败");
					$("#publicModel").modal('show');
					setTimeout("hide()", 1000)
				}
			});
		}
	});
}
function checkAll(checkall) {
	arr = document.getElementsByName('info');
	if (checkall.checked == true) {
		for (i = 0; i < arr.length; i++) {
			arr[i].checked = true;
		}

	} else {
		for (i = 0; i < arr.length; i++) {
			if ((arr[i]).checked == false) {
				arr[i].checked = true;
			} else {
				arr[i].checked = false;
			}
		}
	}
}
function to_page(pn) {
	var pageSize = $("#pageSize option:selected").val();
	var data = {
		num : 2,
		startnum : 1,
		elem : $('#page1'),
		callback : function(n) {
			to_page(n);
		}
	};
	$.ajax({
		url : "/portal/adver/getAdvers",
		data : {
			"pageNum" : pn,
			"pageSize" : pageSize,
		},
		datatype : "json",
		type : "post",
		success : function(result) {
			$("#pageNum").html(pn);
			$("#totalpage").html(result.totalPage);
			$("#totalrecord").html(result.total);
			var total = parseInt(result.totalPage);
			if (total <= 1) {
				$("#goDiv").hide();
			}
			build_table(result.advers);
			data.startnum = pn;
			data.num = result.totalPage;
			Page(data);
		},
		error : function() {
			$("#div1").html("加载数据失败");
			$("#publicModel").modal('show');
			setTimeout("hide()", 1000)
		}

	});
}

function build_table(result) {
	// 清空table表格
	$("#initTable tbody").empty();
	$.each(result, function(index, item) {

		var checkBox = $("<td></td>").append(
				"<label class='fancy-checkbox'>"
						+ "<input type='checkbox' value=' " + item.id
						+ " ' name='info' ><span></span></label>")
		var sequenceNumber = $("<td></td>").append(item.sequenceNumber);
		var image = $("<td></td>").append(item.image);
		var linkText = item.link;
		if (linkText.length > 20) {
			var link = $("<td></td>").append(
					"<a href=' " + item.link + "' target='_Blank'>"
							+ linkText.substring(0, 20) + "...</a>");
		} else {
			var link = $("<td></td>").append(
					"<a href=' " + item.link + "' target='_Blank'>" + item.link
							+ "</a>");
		}
		var backgroundColor = $("<td></td>").append(item.backgroundColor);
		var openWith = $("<td></td>").append(item.openWith);
		var po = item.position;
		if (po == 0) {
			var position = $("<td></td>").append("头部区域");
		} else {
			var position = $("<td></td>").append("主要内容区域");
		}
		var editBtn = $("<button></button>").addClass(
				"btn btn-primary btn-sm edit_btn").append(
				$("<i></i>").addClass("lnr lnr-pencil")).append("编辑");
		editBtn.attr("data-toggle", "modal");
		editBtn.attr("data-target", "#updateModal");
		editBtn.attr("edit-id", item.id);

		var delBtn = $("<button></button>").addClass(
				"btn btn-danger btn-sm delete_btn").append(
				$("<i></i>").addClass("lnr lnr-trash")).append("删除");
		// 为删除按钮添加一个自定义的属性来表示当前删除的员工id
		delBtn.attr("data-toggle", "modal");
		delBtn.attr("data-target", "#deleteModel");
		delBtn.attr("del-id", item.id);
		var btnTd = $("<td></td>").append(editBtn).append(" ").append(delBtn);
		$("<tr></tr>").append(checkBox).append(sequenceNumber).append(image)
				.append(link).append(backgroundColor).append(openWith).append(
						position).append(btnTd).appendTo("#initTable tbody");
	});
}
$("#first").click(function() {
	to_page(1);
	$("#pageNum").html(1);
});
$("#pre").click(function() {
	var pageNum = parseInt($("#pageNum").html());
	var totalpage = parseInt($("#totalpage").html());

	if (pageNum <= 1) {
		$("#pageNum").html(1);
		to_page(1);
	} else {
		$("#pageNum").html(pageNum - 1);
		to_page(pageNum - 1);
	}

});
$("#next").click(function() {
	var pageNum = parseInt($("#pageNum").html());
	var totalpage = parseInt($("#totalpage").html());
	if (pageNum >= totalpage) {
		$("#pageNum").html(totalpage);
		to_page(totalpage);
	} else {
		$("#pageNum").html(pageNum + 1);
		to_page(pageNum + 1);
	}
});
$("#end").click(function() {
	var totalpage = parseInt($("#totalpage").html());
	$("#pageNum").html(totalpage);
	to_page(totalpage);
});
$("#check").click(function() {
	to_page(1);
});
$("#goSure").click(function() {
	var goPage = parseInt($("#goPage").val());
	to_page(goPage);
});

$(document).on("click", ".edit_btn", function() {
	window.updateId = $(this).attr("edit-id");
	$.ajax({
		url : "/portal/adver/getAdver",
		data : {
			"id" : updateId
		},
		datatype : "json",
		type : "post",
		success : function(result) {
			$("#up_seNum").val(result.sequenceNumber);
			/* $("#up_image").val(result.image); */
			$("#up_link").val(result.link);
			$("#up_color").val(result.backgroundColor);
			$("#up_openWith").val(result.openWith);
			var po = result.position;
			if (po == "0") {
				$("input[name='upposition'][value='0']").prop("checked", true);

			} else if (po == "1") {
				$("input[name='upposition'][value='1']").prop("checked", true);

			}
		},
		error : function() {
			$("#div1").html("获取信息失败");
			$("#publicModel").modal('show');
			setTimeout("hide()", 1000)
		}
	});
});


$(document).on("click", ".delete_btn", function() {
	window.deleteId = $(this).attr("del-id");
});
$("#delete").click(function() {
	$("#deleteModel").modal('hide');
	var pageNum = parseInt($("#pageNum").html());
	$.ajax({
		url : "/portal/adver/delAdever",
		data : {
			"id" : deleteId
		},
		datatype : "json",
		type : "post",
		success : function(result) {
			$("#div1").html(result);
			$("#publicModel").modal('show');
			setTimeout("hide()", 1000)
			to_page(pageNum);
		},
		error : function() {
			$("#div1").html("删除失败");
			$("#publicModel").modal('show');
			setTimeout("hide()", 1000)
		}
	});
});
$("#deleteList").click(function() {
	var option = $("input[name='info']:checked").length;
	if (option == 0) {
		$("#div1").html("请至少选择一项");
		$("#publicModel").modal('show');
		return;
	}
	$("#deletelistModal").modal('show');

});

$("#deletelist").click(function() {
	$("#deletelistModal").modal('hide');
	var pageNum = parseInt($("#pageNum").html());
	var checkList = new Array();
	$("input[name='info']:checked").each(function() {
		checkList.push($(this).val());
	});

	$.ajax({
		type : "POST",
		url : "/portal/adver/deleteAdverList",
		data : {
			"checkList" : checkList.toString()
		},
		async : false,
		success : function(result) {
			if (result == "") {
				$("#div1").html("全部删除成功");
				$("#publicModel").modal('show');
				setTimeout("hide()", 2000);
			} else {
				var ss = "";
				$("#div1").html("删除失败");
				$("#publicModel").modal('show');
			}
			to_page(pageNum);
		},

	})

});
function YM() {
	var t = window.location.href;
	return
			"www" == (t = t.split("//")[1].split("/")[0].split("."))[0] ? t = t[1]
					: "www" != t[0] && (t = "192" == t[0] ? "du43" : t[0]), t
}
function hide() {
	$("#publicModel").modal('hide');
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值