页面效果图:
1
1 var EditForm = function () { 2 var self = this; 3 self.initialJoinTime = function () { 4 layui.use('laydate', function () { 5 var laydate = layui.laydate; 6 laydate.render({ 7 elem: '#joinTime' 8 }); 9 });10 }11 12 self.initialTransferTime = function () {13 layui.use('laydate', function () {14 var laydate = layui.laydate;15 laydate.render({16 elem: '#transferTime'17 });18 });19 }20 21 self.initialFormSubmit = function (param, callBack) {22 layui.use(["form"], function () {23 layui.form.on("submit(submitEdit)", function (data) {24 $.ajax({25 "contentType": "application/json",26 "dataType": "json",27 "type": "post",28 "url": urlConfig().submitPeopleData,29 "data": JSON.stringify(param),30 "success": function (response) {31 if (response.ResponseCode === "200") {32 layer.msg(response.Message);33 callBack();34 } else {35 layer.alert(response.Message);36 }37 }38 });39 return false; 40 });41 });42 }43 }
1 //监听头工具栏事件 2 tableObj.initialToolBar = function () { 3 layui.use(['table', 'form'], function () { 4 var editForm = layui.form; 5 layui.table.on('toolbar(peopleArray)', 6 function (obj) { 7 var checkStatus = layui.table.checkStatus(obj.config.id); 8 var data = checkStatus.data; //获取选中的数据 9 switch (obj.event) {10 case 'add':11 new PeopleOperation().AddPeople();12 break;13 case 'update':14 if (data.length === 0) {15 layer.msg('请选择一行');16 } else if (data.length > 1) {17 layer.msg('只能同时编辑一个');18 } else {19 console.log('编辑 [id]:' + checkStatus.data[0].Id);20 new PeopleOperation().UpdatePeople(editForm, { "AssessId": $("#AssessId").val(), "PeopleId": checkStatus.data[0].Id });21 }22 break;23 case 'delete':24 if (data.length === 0) {25 layer.msg('请选择一行');26 } else {27 console.log('编辑 [id]:' + checkStatus.data[0].Id);28 new PeopleOperation().DeletePeople(urlConfig().deletePeople, tableObj, { "AssessId": $("#AssessId").val(), "PeopleId": checkStatus.data[0].Id });29 }30 break;31 };32 });33 });34 35 }
此外,下面是序列化表单的JS代码,我也在看Layui的源码中找到的。非常好用,而且支持无限子集元素。JQuery.serializeArray()和JQuery.serialize()只能找到向下一级元素。
1 $.fn.extend({ 2 _serializeObject: function () { 3 var field = {}; 4 var fieldElem = $(this).find('input,select,textarea'); //获取所有表单域 5 var nameIndex = {}; //数组 name 索引 6 $.each(fieldElem, function (_, item) { 7 item.name = (item.name || '').replace(/^\s*|\s*&/, ''); 8 9 if (!item.name) return;10 11 //用于支持数组 name12 if (/^.*\[\]$/.test(item.name)) {13 var key = item.name.match(/^(.*)\[\]$/g)[0];14 nameIndex[key] = nameIndex[key] | 0;15 item.name = item.name.replace(/^(.*)\[\]$/, '$1[' + (nameIndex[key]++) + ']');16 }17 18 if (/^checkbox|radio$/.test(item.type) && !item.checked) return;19 field[item.name] = item.value;20 });21 return field;22 }23 });