123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551 |
- //全选的实现
- $(document).on('click','.check-all',function(){
- $(".ids").prop("checked", $(this).prop('checked'));
- });
- // $(document).on('click','.uncheck-all',function(){
- // $(".ids").prop("checked", false);
- // });
- //ajax get请求
- $(document).on('click','.ajax-get',function(){
- var that = this;
- var confirm_msg = $(this).attr('data-confirm')?$(this).attr('data-confirm'):'确认要执行该操作吗?';
- if ( $(this).hasClass('confirm') ) {
- layer.confirm(confirm_msg, {
- btn: ['确认', '取消'], //按钮
- shade: false //不显示遮罩
- }, function() {
- layer.closeAll();
- ajax_get_fun(that);
- }, function() {
- layer.closeAll();
- });
- }else{
- ajax_get_fun(that);
- }
- return false;
- });
- function ajax_get_fun(that) {
- var target;
- if ( (target = $(that).attr('href')) || (target = $(that).attr('url')) ) {
- var flag = $(that).attr('data-flag');
- if(flag == 1){
- return '';
- }
- $(that).attr('data-flag',1);
- $.get(target).success(function(data){
- if (data.code==1) {
- if (data.url) {
- updateAlert(data.msg,'alert-success');
- }else{
- updateAlert(data.msg,'alert-success');
- }
- setTimeout(function(){
- if($(that).hasClass('data-reload')){
- window.location.reload();
- }
- if($(that).hasClass('data-reload1')){
- parent.location.reload();
- }
- if( $(that).hasClass('no-refresh')){
- $('#top-alert').find('button').click();
- $(that).attr('data-flag',0);
- }else{
- var datatable = $(that).attr('data-table');
- if(datatable==1){
- $('#top-alert').find('button').click();
- $("#table").trigger("reloadGrid");
- }else{
- var datalayer = $(that).attr('data-layer');
- if(datalayer==1) {
- parent.layer.closeAll();
- parent.location.reload();
- }else{
- location.href=data.url;
- }
- }
- }
- },1500);
- }else{
- updateAlert(data.msg,'alert-danger');
- setTimeout(function(){
- if (data.url) {
- location.href=data.url;
- }else{
- $('#top-alert').find('button').click();
- $(that).attr('data-flag',0);
- }
- },1500);
- }
- });
- }
- }
- //ajax post submit请求
- $(document).on('click','.ajax-post',function(){
- var target,query,form;
- var target_form = $(this).attr('target-form');
- var that = this;
- var nead_confirm=false;
- var confirm_msg = $(this).attr('data-confirm')?$(this).attr('data-confirm'):'确认要执行该操作吗?';
- if( ($(this).attr('type')=='submit') || (target = $(this).attr('href')) || (target = $(this).attr('url')) ){
- form = $('.'+target_form);
- if ($(this).attr('hide-data') === 'true'){//无数据时也可以使用的功能
- form = $('.hide-data');
- query = form.serialize();
- }else if (form.get(0)==undefined){
- return false;
- }else if ( form.get(0).nodeName=='FORM' ){
- if ( $(this).hasClass('confirm') ) {
- layer.confirm(confirm_msg, {
- btn: ['确认', '取消'], //按钮
- shade: false //不显示遮罩
- }, function() {
- layer.closeAll();
- if($(this).attr('url') !== undefined){
- target = $(this).attr('url');
- }else{
- target = form.get(0).action;
- }
- query = form.serialize();
- ajax_post_fun(target,that,query);
- return false;
- }, function() {
- layer.closeAll();
- });
- return false;
- }else{
- if($(this).attr('url') !== undefined){
- target = $(this).attr('url');
- }else{
- target = form.get(0).action;
- }
- query = form.serialize();
- }
- }else if( form.get(0).nodeName=='INPUT' || form.get(0).nodeName=='SELECT' || form.get(0).nodeName=='TEXTAREA') {
- form.each(function(k,v){
- if(v.type=='checkbox' && v.checked==true){
- nead_confirm = true;
- }
- })
- if ( nead_confirm && $(this).hasClass('confirm') ) {
- layer.confirm(confirm_msg, {
- btn: ['确认', '取消'], //按钮
- shade: false //不显示遮罩
- }, function() {
- layer.closeAll();
- query = form.serialize();
- ajax_post_fun(target,that,query);
- return false;
- }, function() {
- layer.closeAll();
- });
- return false;
- }else{
- query = form.serialize();
- }
- }else{
- if ( $(this).hasClass('confirm') ) {
- layer.confirm(confirm_msg, {
- btn: ['确认', '取消'], //按钮
- shade: false //不显示遮罩
- }, function() {
- layer.closeAll();
- query = form.find('input,select,textarea').serialize();
- ajax_post_fun(target,that,query);
- return false;
- }, function() {
- layer.closeAll();
- });
- return false;
- }else{
- query = form.find('input,select,textarea').serialize();
- }
- }
- ajax_post_fun(target,that,query);
- }
- return false;
- });
- function ajax_post_fun(target,that,query) {
- $(that).addClass('disabled').attr('autocomplete','off').prop('disabled',true);
- $.post(target,query).success(function(data){
- if (data.code==1) {
- if (data.url) {
- updateAlert(data.msg,'alert-success');
- // updateAlert(data.msg + ' 页面即将自动跳转~','alert-success');
- }else{
- updateAlert(data.msg ,'alert-success');
- }
- setTimeout(function(){
- $(that).removeClass('disabled').prop('disabled',false);
- if( $(that).hasClass('no-refresh')){
- $('#top-alert').find('button').click();
- }else{
- var datatable = $(that).attr('data-table');
- if(datatable==1){
- $('#top-alert').find('button').click();
- $("#table").trigger("reloadGrid");
- }else{
- var datalayer = $(that).attr('data-layer');
- var datalayer1 = $(that).attr('data-layer_c');
- var datalayer2 = $(that).attr('data-layer_d');
- if(datalayer==1){
- parent.layer.closeAll();
- // parent.location.reload();
- }else{
- if(datalayer1==1){
- parent.layer.closeAll();
- }else {
- if(datalayer2==1){
- parent.layer.closeAll();
- parent.location.reload();
- }else {
- if (data.url && !datalayer) {
- location.href = data.url;
- }
- $('#top-alert').find('button').click();
- }
- }
- }
- }
- }
- },1500);
- }else{
- console.log(111)
- updateAlert(data.msg,'alert-danger');
- setTimeout(function(){
- $(that).removeClass('disabled').prop('disabled',false);
- if (data.url) {
- location.href=data.url;
- }else{
- $('#top-alert').find('button').click();
- }
- },1500);
- }
- });
- }
- /**顶部警告栏*/
- var top_alert = $('#top-alert');
- top_alert.find('.close').on('click', function () {
- top_alert.removeClass('block').slideUp(200);
- });
- window.updateAlert = function (text,c) {
- text = text||'default';
- c = c||false;
- if ( text!='default' ) {
- top_alert.find('.alert-content').text(text);
- if (top_alert.hasClass('block')) {
- } else {
- top_alert.addClass('block').slideDown(200);
- }
- } else {
- if (top_alert.hasClass('block')) {
- top_alert.removeClass('block').slideUp(200);
- }
- }
- if ( c!=false ) {
- top_alert.removeClass('alert-danger alert-warning alert-info alert-success').addClass(c);
- }
- };
- window.updateAlertSuccess = function (text,t) {
- updateAlert(text,'alert-success');
- setTimeout(function(){
- $(that).removeClass('disabled').prop('disabled',false);
- if (t==1) {
- location.reload();
- }else{
- $('#top-alert').find('button').click();
- }
- },1500);
- };
- window.updateAlertError = function (text,t) {
- updateAlert(text,'alert-danger');
- setTimeout(function(){
- $(that).removeClass('disabled').prop('disabled',false);
- if (t==1) {
- location.reload();
- }else{
- $('#top-alert').find('button').click();
- }
- },1500);
- };
- /* 设置表单的值 */
- function formSetValue(name, value){
- var first = name.substr(0,1), input, i = 0, val;
- if(value === "") return;
- if("#" === first || "." === first){
- input = $(name);
- } else {
- input = $("[name='" + name + "']");
- }
- if(input.eq(0).is(":radio")) { //单选按钮
- input.filter("[value='" + value + "']").each(function(){this.checked = true});
- } else if(input.eq(0).is(":checkbox")) { //复选框
- if(!$.isArray(value)){
- val = new Array();
- val[0] = value;
- } else {
- val = value;
- }
- for(i = 0, len = val.length; i < len; i++){
- input.filter("[value='" + val[i] + "']").each(function(){this.checked = true});
- }
- } else { //其他表单选项直接设置值
- input.val(value);
- }
- }
- /**
- * 列表页排序
- * @param _self
- */
- function updateSort(_self) {
- var url = $(_self).attr('data-url');;
- var id = $(_self).attr('data-id');
- var old = $(_self).attr('data-old');
- var sort = $(_self).val();
- $.post(url,{id:id,sort:sort},function(data){
- if(data.code){
- $("#table").trigger("reloadGrid");
- }else{
- $(_self).val(old);
- updateAlert(data.msg,'alert-danger');
- setTimeout(function(){
- if (data.url) {
- location.href=data.url;
- }else{
- $('#top-alert').find('button').click();
- }
- },1500);
- }
- });
- }
- $.fn.serializeObject = function()
- {
- var o = {};
- var a = this.serializeArray();
- $.each(a, function() {
- if (o[this.name]) {
- if (!o[this.name].push) {
- o[this.name] = [o[this.name]];
- }
- o[this.name].push(this.value || '');
- } else {
- o[this.name] = this.value || '';
- }
- });
- return o;
- };
- //搜索
- $('#search-btn').on('click', function() {
- var self = $("#form-search");
- var url = self.attr('action');
- console.log('search',url);
- if(url){
- var pdata = self.serializeObject();
- $("#table").jqGrid('setGridParam', {
- url: url,
- postData:pdata,
- page: 1
- }).trigger("reloadGrid");
- }
- });
- //清除搜索
- $('#search-clear').on('click',function () {
- $("#form-search")[0].reset();
- var url = $("#form-search").attr('action');
- if(url){
- var pdata = $("#form-search").serializeObject();
- $("#table").jqGrid('setGridParam',{url:url,postData:pdata,page:1}).trigger("reloadGrid");
- }
- });
- /**
- * 输入限制,只可以输入小数点和数字且小数点后只可以输入两个数字
- * 一般用于onkeyup 和 onpaste方法
- * 例:onkeyup="clearNoNum(this)"
- * @param obj
- */
- function clearNoNum(obj){
- if(obj.value.substr(0,1)=='.'){
- obj.value = '';
- }
- obj.value = obj.value.replace(/[^\d.]/g,"");
- obj.value = obj.value.replace(/\.{2,}/g,".");
- obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
- obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
- if(obj.value.indexOf(".")< 0 && obj.value !=""){
- obj.value= parseFloat(obj.value);
- }
- }
- /**
- * 输入限制,只可以输入数字
- * 一般用于onkeyup 和 onpaste方法
- * 例:onkeyup="clearNoNum(this)"
- * @param obj
- */
- function clearNoNum1(obj){
- if(obj.value.substr(0,1)=='.'){
- obj.value = '';
- }
- obj.value = obj.value.replace(/[^\d.]/g,"");
- obj.value = obj.value.replace(/\./g,"");
- }
- /**
- * 输入限制,只可以输入小数点和数字且小数点后只可以输入一个数字
- * 一般用于onkeyup 和 onpaste方法
- * 例:onkeyup="clearNoNum(this)"
- * @param obj
- */
- function clearNoNum2(obj){
- if(obj.value.substr(0,1)=='.'){
- obj.value = '';
- }
- obj.value = obj.value.replace(/[^\d.]/g,"");
- obj.value = obj.value.replace(/\.{2,}/g,".");
- obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
- obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d).*$/,'$1$2.$3');
- if(obj.value.indexOf(".")< 0 && obj.value !=""){
- obj.value= parseFloat(obj.value);
- }
- }
- /**
- * 清除“数字”以外的字符
- * 一般用于onkeyup 和 onpaste方法
- * 例:onkeyup="clearNoNum(this)"
- * @param obj
- */
- function clearNoNum3(obj){
- obj.value = obj.value.replace(/[^\d]/g,""); //清除“数字”以外的字符
- }
- //清除特殊字符
- function stripscript(obj) {
- var s = $(obj).val();
- var pattern = new RegExp("[`~!@#$%+^&*()=|{}':;'\\[\\].<>/?~!@#¥……&*()——|{}【】‘;:”“'。,、?]");
- var rs = "";
- for (var i = 0; i < s.length; i++) {
- rs = rs + s.substr(i, 1).replace(pattern, '');
- }
- $(obj).val(rs);
- }
- function layer_open(_self,flag){
- var url = $(_self).attr('url');
- var title = $(_self).attr('data-title');
- var width = $(_self).attr('data-width')?$(_self).attr('data-width'):'90%';
- var height = $(_self).attr('data-height')?$(_self).attr('data-height'):'90%';
- layer.open({
- type: 2,
- title: title,
- shadeClose: true,
- shade: 0.3,
- maxmin: true,
- area: [width, height],
- content: url,
- end: function () {
- if(flag==1){
- $("#table").trigger("reloadGrid");
- }else if(flag==2){
- window.location.reload();
- }
- }
- });
- }
- //打开查看图片
- function open_img(_self) {
- var src = $(_self).attr('src');
- var img = new Image();
- img.src = src;
- var width = $(window).width()*0.8;
- var height = $(window).height()*0.8;
- img.onload = function(){
- var imgw = img.width;
- var imgh = img.height;
- if(imgw > width&&imgh < height){
- imgh = imgh*width/imgw;
- imgw = width;
- }else if(imgw < width&&imgh > height){
- imgw = imgw*height/imgh;
- imgh = height;
- }else if(imgw > width&&imgh > height){
- var bl1 = width/height;
- var bl2 = imgw/imgh;
- if(bl1 < bl2){
- imgh = imgh*width/imgw;
- imgw = width;
- }else if(bl1 > bl2){
- imgw = imgw*height/imgh;
- imgh = height;
- }else{
- imgh = height;
- imgw = width;
- }
- }
- var imgs = '<img width="'+imgw+'" height="'+imgh+'" src="'+src+'" />';
- layer.open({
- type: 1,
- title: false,
- closeBtn: 1,
- area: [imgw+'px', imgh+'px'],
- skin: 'layui-layer-nobg', //没有背景色
- shadeClose: true,
- content: imgs
- });
- };
- }
- $('.cancel-btn').click(function () {
- parent.layer.closeAll();
- })
- $('#upload').click(function () {
- var url = $(this).attr('data-url');
- layer.open({
- type: 2,
- title: '导入',
- shadeClose: true,
- shade: 0.3,
- maxmin: true,
- area: ['90%', '90%'],
- content: url,
- });
- })
- $('#export').click(function () {
- var url = $(this).attr('data-url');
- var data = $("#form-search").serialize();
- window.location.href=url+'?'+data;
- })
- function layerOpen(_self){
- var url = $(_self).attr('url');
- var title = $(_self).attr('data-title');
- layer.open({
- type: 2,
- title: title,
- shadeClose: true,
- shade: 0.3,
- area: ['90%', '90%'],
- content: url,
- });
- }
|