表格数据下放到控件,再对控件进行监听,把需要的值插入到自定义属性中,再通过data.elem.attributes['pid'].nodeValue;获取自己想要的值。
<script type="text/html" id="barDemo"><a class="layui-btn layui-btn-xs" lay-event="edit">更新</a><a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a><input type="checkbox" name="status" lay-skin="switch" value = {{d.status}} {{d.status=='1'?'checked':''}} pid="{{d.wuliucode}}" lay-filter="switchTest" lay-event="status" id="status" lay-text="启用|禁用">
</script>
table.render({elem: '#test',url:'/getxishutable' // 此处为静态模拟数据,实际使用时需换成真实接口,toolbar: '#toolbarDemo',defaultToolbar: ['filter', 'exports', 'print', {title: '帮助',layEvent: 'LAYTABLE_TIPS',icon: 'layui-icon-tips'}],height: 'full-25' // 最大高度减去其他容器已占有的高度差,cellMinWidth: 80// ,totalRow: true // 开启合计行,page: true,cols: [[{type: 'checkbox', fixed: 'left'},{field:'id', fixed: 'left', title: 'ID', sort: true,},{field:'wuliucode', title: '公司编码'},{field:'wuliuname', title:'公司名称',},{field:'pname', title:'公司简称',},{field:'lctparabolic', title: '轻抛物系数',edit:true, sort: true}//,{field:'status', title: '状态', sort: true,templet: "#switchTpl"},{fixed: 'right', title:'操作', minWidth: 125, toolbar: '#barDemo',templet: "#barDemo"}]],success:function (data){console.log(data)},error: function(res, msg){console.log(res, msg)}});/指定开关事件form.on('switch(switchTest)', function(data){console.log(data)var status = data.elem.checked?1:0;var pid= data.elem.attributes['pid'].nodeValue;//获取修改数据的参数条件//layer.tips(this.name + ':'+ data.elem.checked+'选中状态:'+status+'编号:'+pid, data.othis);var ajax = new $ax("/upxishustatus", function (data) {result = data.result;layer.msg('成功更新数据'+result+'条');layui.table.reloadData('test', {// 当发送后端请求成功后,可再通过 reloadData 方法完成数据重载scrollPos: 'fixed' // 保持滚动条位置不变 - v2.7.3 新增});}, function (data) {Layui.error("修改失败!" + data.responseJSON.message + "!");});ajax.set("wuliucode", pid);ajax.set("status", status);ajax.start();});//ajax封装(function () {var $ax = function (url, success, error) {this.url = url;this.type = "post";this.data = {};this.dataType = "json";this.async = false;this.success = success;this.error = error;};$ax.prototype = {start : function () {var me = this;if (this.url.indexOf("?") == -1) {this.url = this.url + "?jstime=" + new Date().getTime();} else {this.url = this.url + "&jstime=" + new Date().getTime();}$.ajax({type: this.type,url: this.url,dataType: this.dataType,async: this.async,data: this.data,beforeSend: function(data) {},success: function(data) {me.success(data);},error: function(data) {me.error(data);}});},set : function (key, value) {if (typeof key == "object") {for (var i in key) {if (typeof i == "function")continue;this.data[i] = key[i];}} else {this.data[key] = (typeof value == "undefined") ? $("#" + key).val() : value;}return this;},setData : function(data){this.data = data;return this;},clear : function () {this.data = {};return this;}};window.$ax = $ax;} ());
