当前位置 : 主页 > 网络编程 > JavaScript >

扩展layui中table组件增加行单选功能

来源:互联网 收集:自由互联 发布时间:2021-06-28
layui.zip layui.zip form.js /** layui-v2.2.3 MIT License By http://www.layui.com */ ;layui.define("layer",function(e){"use strict";var t=layui.$,i=layui.layer,a=layui.hint(),n=layui.device(),l="form",s=".layui-form",r="layui-this",o="layui-
layui.zip layui.zip form.js
/** layui-v2.2.3 MIT License By http://www.layui.com */
 ;layui.define("layer",function(e){"use strict";var t=layui.$,i=layui.layer,a=layui.hint(),n=layui.device(),l="form",s=".layui-form",r="layui-this",o="layui-hide",c="layui-disabled",u=function(){this.config={verify:{required:[/[\S]+/,"必填项不能为空"],phone:[/^1\d{10}$/,"请输入正确的手机号"],email:[/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,"邮箱格式不正确"],url:[/(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/,"链接格式不正确"],number:function(e){if(!e||isNaN(e))return"只能填写数字"},date:[/^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/,"日期格式不正确"],identity:[/(^\d{15}$)|(^\d{17}(x|X|\d)$)/,"请输入正确的身份证号"]}}};u.prototype.set=function(e){var i=this;return t.extend(!0,i.config,e),i},u.prototype.verify=function(e){var i=this;return t.extend(!0,i.config.verify,e),i},u.prototype.on=function(e,t){return layui.onevent.call(this,l,e,t)},u.prototype.render=function(e,i){var n=this,u=t(s+function(){return i?'[lay-filter="'+i+'"]':""}()),d={select:function(){var e,i="请选择",a="layui-form-select",n="layui-select-title",s="layui-select-none",d="",f=u.find("select"),y=function(i,l){t(i.target).parent().hasClass(n)&&!l||(t("."+a).removeClass(a+"ed "+a+"up"),e&&d&&e.val(d)),e=null},v=function(i,u,f){var v=t(this),p=i.find("."+n),m=p.find("input"),k=i.find("dl"),x=k.children("dd");if(!u){var b=function(){var e=i.offset().top+i.outerHeight()+5-h.scrollTop(),t=k.outerHeight();i.addClass(a+"ed"),x.removeClass(o),e+t>h.height()&&e>=t&&i.addClass(a+"up")},g=function(e){i.removeClass(a+"ed "+a+"up"),m.blur(),e||C(m.val(),function(e){e&&(d=k.find("."+r).html(),m&&m.val(d))})};p.on("click",function(e){i.hasClass(a+"ed")?g():(y(e,!0),b()),k.find("."+s).remove()}),p.find(".layui-edge").on("click",function(){m.focus()}),m.on("keyup",function(e){var t=e.keyCode;9===t&&b()}).on("keydown",function(e){var t=e.keyCode;9===t?g():13===t&&e.preventDefault()});var C=function(e,i,a){var n=0;layui.each(x,function(){var i=t(this),l=i.text(),s=l.indexOf(e)===-1;(""===e||"blur"===a?e!==l:s)&&n++,"keyup"===a&&i[s?"addClass":"removeClass"](o)});var l=n===x.length;return i(l),l},w=function(e){var t=this.value,i=e.keyCode;return 9!==i&&13!==i&&37!==i&&38!==i&&39!==i&&40!==i&&(C(t,function(e){e?k.find("."+s)[0]||k.append('

无匹配项

'):k.find("."+s).remove()},"keyup"),void(""===t&&k.find("."+s).remove()))};f&&m.on("keyup",w).on("blur",function(t){e=m,d=k.find("."+r).html(),setTimeout(function(){C(m.val(),function(e){d||m.val("")},"blur")},200)}),x.on("click",function(){var e=t(this),a=e.attr("lay-value"),n=v.attr("lay-filter");return!e.hasClass(c)&&(e.hasClass("layui-select-tips")?m.val(""):(m.val(e.text()),e.addClass(r)),e.siblings().removeClass(r),v.val(a).removeClass("layui-form-danger"),layui.event.call(this,l,"select("+n+")",{elem:v[0],value:a,othis:i}),g(!0),!1)}),i.find("dl>dt").on("click",function(e){return!1}),t(document).off("click",y).on("click",y)}};f.each(function(e,l){var s=t(this),o=s.next("."+a),u=this.disabled,d=l.value,f=t(l.options[l.selectedIndex]),y=l.options[0];if("string"==typeof s.attr("lay-ignore"))return s.show();var h="string"==typeof s.attr("lay-search"),p=y?y.value?i:y.innerHTML||i:i,m=t([' ',' ',' ','
'+function(e){var t=[];return layui.each(e,function(e,a){0!==e||a.value?"optgroup"===a.tagName.toLowerCase()?t.push("
"+a.label+"
"):t.push('
'+a.innerHTML+"
"):t.push('
'+(a.innerHTML||i)+"
")}),0===t.length&&t.push('
没有选项
'),t.join("")}(s.find("*"))+"
"," "].join(""));o[0]&&o.remove(),s.after(m),v.call(this,m,u,h)})},checkbox:function(){var e={checkbox:["layui-form-checkbox","layui-form-checked","checkbox"],_switch:["layui-form-switch","layui-form-onswitch","switch"]},i=u.find("input[type=checkbox]"),a=function(e,i){var a=t(this);e.on("click",function(){var t=a.attr("lay-filter"),n=(a.attr("lay-text")||"").split("|");a[0].disabled||(e.data("_class",i[1]),a[0].checked?(a[0].checked=!1,e.data("_method","removeClass").data("_text",n[0]),e.removeClass(i[1]).find("em").text(n[1])):(a[0].checked=!0,e.data("_method","addClass").data("_text",n[1]),e.addClass(i[1]).find("em").text(n[0])),layui.event.call(a[0],l,i[2]+"("+t+")",{elem:a[0],value:a[0].value,othis:e}))})};i.each(function(i,n){var l=t(this),s=l.attr("lay-skin"),r=(l.attr("lay-text")||"").split("|"),o=this.disabled;"switch"===s&&(s="_"+s);var u=e[s]||e.checkbox;if("string"==typeof l.attr("lay-ignore"))return l.show();var d=l.next("."+u[0]),f=t([' ',{_switch:" "+((n.checked?r[0]:r[1])||"")+" "}[s]||(n.title.replace(/\s/g,"")?" "+n.title+"":"")+' '+(s?"":"")+""," "].join(""));d[0]&&d.remove(),l.after(f),a.call(this,f,u)})},radio:function(){var e="layui-form-radio",i=["",""],a=u.find("input[type=radio]"),n=function(a){var n=t(this),r="layui-anim-scaleSpring";a.on("click",function(){var o=n[0].name,c=n.parents(s),u=n.attr("lay-filter"),d=c.find("input[name="+o.replace(/(\.|#|\[|\])/g,"\\$1")+"]");n[0].disabled||(layui.each(d,function(){var a=t(this).next("."+e);this.checked=!1,a.removeClass(e+"ed"),a.find(".layui-icon").removeClass(r).html(i[1])}),n[0].checked=!0,a.addClass(e+"ed"),a.find(".layui-icon").addClass(r).html(i[0]),layui.event.call(n[0],l,"radio("+u+")",{elem:n[0],value:n[0].value,othis:a}))})};a.each(function(a,l){var s=t(this),r=s.next("."+e),o=this.disabled;if("string"==typeof s.attr("lay-ignore"))return s.show();var u=t([' ',' '+i[l.checked?0:1]+"",void 0==s.attr("notitle")?" "+(l.title||"未命名")+"":""," "].join(""));r[0]&&r.remove(),s.after(u),n.call(this,u)})}};return e?d[e]?d[e]():a.error("不支持的"+e+"表单渲染"):layui.each(d,function(e,t){t()}),n},u.prototype.resetCheckbox=function(e,t){e[e.data("_method")](e.data("_class")),e.find("em").text(e.data("_text")),t.checked=!t.checked};var d=function(){var e=t(this),a=f.config.verify,r=null,o="layui-form-danger",c={},u=e.parents(s),d=u.find("*[lay-verify]"),y=e.parents("form")[0],h=u.find("input,select,textarea"),v=e.attr("lay-filter");return layui.each(d,function(e,l){var s=t(this),c=s.attr("lay-verify").split("|"),u=s.attr("lay-verType"),d=s.val();if(s.removeClass(o),layui.each(c,function(e,t){var c,f="",y="function"==typeof a[t];if(a[t]){var c=y?f=a[t](d,l):!a[t][0].test(d);if(f=f||a[t][1],c)return"tips"===u?i.tips(f,s,{tips:1}):"alert"===u?i.alert(f,{title:"提示",shadeClose:!0}):i.msg(f,{icon:5,shift:6}),n.android||n.ios||l.focus(),s.addClass(o),r=!0}}),r)return r}),!r&&(layui.each(h,function(e,t){t.name&&(/^checkbox|radio$/.test(t.type)&&!t.checked||(c[t.name]=t.value))}),layui.event.call(this,l,"submit("+v+")",{elem:this,form:y,field:c}))},f=new u,y=t(document),h=t(window);f.render(),y.on("reset",s,function(){var e=t(this).attr("lay-filter");setTimeout(function(){f.render(null,e)},50)}),y.on("submit",s,d).on("click","*[lay-submit]",d),e(l,f)});
上一篇:for与each
下一篇:js判断字符长度
网友评论