0
0

common.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. //全选的实现
  2. $(document).on('click','.check-all',function(){
  3. $(".ids").prop("checked", $(this).prop('checked'));
  4. });
  5. // $(document).on('click','.uncheck-all',function(){
  6. // $(".ids").prop("checked", false);
  7. // });
  8. //ajax get请求
  9. $(document).on('click','.ajax-get',function(){
  10. var that = this;
  11. var confirm_msg = $(this).attr('data-confirm')?$(this).attr('data-confirm'):'确认要执行该操作吗?';
  12. if ( $(this).hasClass('confirm') ) {
  13. layer.confirm(confirm_msg, {
  14. btn: ['确认', '取消'], //按钮
  15. shade: false //不显示遮罩
  16. }, function() {
  17. layer.closeAll();
  18. ajax_get_fun(that);
  19. }, function() {
  20. layer.closeAll();
  21. });
  22. }else{
  23. ajax_get_fun(that);
  24. }
  25. return false;
  26. });
  27. function ajax_get_fun(that) {
  28. var target;
  29. if ( (target = $(that).attr('href')) || (target = $(that).attr('url')) ) {
  30. var flag = $(that).attr('data-flag');
  31. if(flag == 1){
  32. return '';
  33. }
  34. $(that).attr('data-flag',1);
  35. $.get(target).success(function(data){
  36. if (data.code==1) {
  37. if (data.url) {
  38. updateAlert(data.msg,'alert-success');
  39. }else{
  40. updateAlert(data.msg,'alert-success');
  41. }
  42. setTimeout(function(){
  43. if($(that).hasClass('data-reload')){
  44. window.location.reload();
  45. }
  46. if($(that).hasClass('data-reload1')){
  47. parent.location.reload();
  48. }
  49. if( $(that).hasClass('no-refresh')){
  50. $('#top-alert').find('button').click();
  51. $(that).attr('data-flag',0);
  52. }else{
  53. var datatable = $(that).attr('data-table');
  54. if(datatable==1){
  55. $('#top-alert').find('button').click();
  56. $("#table").trigger("reloadGrid");
  57. }else{
  58. var datalayer = $(that).attr('data-layer');
  59. if(datalayer==1) {
  60. parent.layer.closeAll();
  61. parent.location.reload();
  62. }else{
  63. location.href=data.url;
  64. }
  65. }
  66. }
  67. },1500);
  68. }else{
  69. updateAlert(data.msg,'alert-danger');
  70. setTimeout(function(){
  71. if (data.url) {
  72. location.href=data.url;
  73. }else{
  74. $('#top-alert').find('button').click();
  75. $(that).attr('data-flag',0);
  76. }
  77. },1500);
  78. }
  79. });
  80. }
  81. }
  82. //ajax post submit请求
  83. $(document).on('click','.ajax-post',function(){
  84. var target,query,form;
  85. var target_form = $(this).attr('target-form');
  86. var that = this;
  87. var nead_confirm=false;
  88. var confirm_msg = $(this).attr('data-confirm')?$(this).attr('data-confirm'):'确认要执行该操作吗?';
  89. if( ($(this).attr('type')=='submit') || (target = $(this).attr('href')) || (target = $(this).attr('url')) ){
  90. form = $('.'+target_form);
  91. if ($(this).attr('hide-data') === 'true'){//无数据时也可以使用的功能
  92. form = $('.hide-data');
  93. query = form.serialize();
  94. }else if (form.get(0)==undefined){
  95. return false;
  96. }else if ( form.get(0).nodeName=='FORM' ){
  97. if ( $(this).hasClass('confirm') ) {
  98. layer.confirm(confirm_msg, {
  99. btn: ['确认', '取消'], //按钮
  100. shade: false //不显示遮罩
  101. }, function() {
  102. layer.closeAll();
  103. if($(this).attr('url') !== undefined){
  104. target = $(this).attr('url');
  105. }else{
  106. target = form.get(0).action;
  107. }
  108. query = form.serialize();
  109. ajax_post_fun(target,that,query);
  110. return false;
  111. }, function() {
  112. layer.closeAll();
  113. });
  114. return false;
  115. }else{
  116. if($(this).attr('url') !== undefined){
  117. target = $(this).attr('url');
  118. }else{
  119. target = form.get(0).action;
  120. }
  121. query = form.serialize();
  122. }
  123. }else if( form.get(0).nodeName=='INPUT' || form.get(0).nodeName=='SELECT' || form.get(0).nodeName=='TEXTAREA') {
  124. form.each(function(k,v){
  125. if(v.type=='checkbox' && v.checked==true){
  126. nead_confirm = true;
  127. }
  128. })
  129. if ( nead_confirm && $(this).hasClass('confirm') ) {
  130. layer.confirm(confirm_msg, {
  131. btn: ['确认', '取消'], //按钮
  132. shade: false //不显示遮罩
  133. }, function() {
  134. layer.closeAll();
  135. query = form.serialize();
  136. ajax_post_fun(target,that,query);
  137. return false;
  138. }, function() {
  139. layer.closeAll();
  140. });
  141. return false;
  142. }else{
  143. query = form.serialize();
  144. }
  145. }else{
  146. if ( $(this).hasClass('confirm') ) {
  147. layer.confirm(confirm_msg, {
  148. btn: ['确认', '取消'], //按钮
  149. shade: false //不显示遮罩
  150. }, function() {
  151. layer.closeAll();
  152. query = form.find('input,select,textarea').serialize();
  153. ajax_post_fun(target,that,query);
  154. return false;
  155. }, function() {
  156. layer.closeAll();
  157. });
  158. return false;
  159. }else{
  160. query = form.find('input,select,textarea').serialize();
  161. }
  162. }
  163. ajax_post_fun(target,that,query);
  164. }
  165. return false;
  166. });
  167. function ajax_post_fun(target,that,query) {
  168. $(that).addClass('disabled').attr('autocomplete','off').prop('disabled',true);
  169. $.post(target,query).success(function(data){
  170. if (data.code==1) {
  171. if (data.url) {
  172. updateAlert(data.msg,'alert-success');
  173. // updateAlert(data.msg + ' 页面即将自动跳转~','alert-success');
  174. }else{
  175. updateAlert(data.msg ,'alert-success');
  176. }
  177. setTimeout(function(){
  178. $(that).removeClass('disabled').prop('disabled',false);
  179. if( $(that).hasClass('no-refresh')){
  180. $('#top-alert').find('button').click();
  181. }else{
  182. var datatable = $(that).attr('data-table');
  183. if(datatable==1){
  184. $('#top-alert').find('button').click();
  185. $("#table").trigger("reloadGrid");
  186. }else{
  187. var datalayer = $(that).attr('data-layer');
  188. var datalayer1 = $(that).attr('data-layer_c');
  189. var datalayer2 = $(that).attr('data-layer_d');
  190. if(datalayer==1){
  191. parent.layer.closeAll();
  192. // parent.location.reload();
  193. }else{
  194. if(datalayer1==1){
  195. parent.layer.closeAll();
  196. }else {
  197. if(datalayer2==1){
  198. parent.layer.closeAll();
  199. parent.location.reload();
  200. }else {
  201. if (data.url && !datalayer) {
  202. location.href = data.url;
  203. }
  204. $('#top-alert').find('button').click();
  205. }
  206. }
  207. }
  208. }
  209. }
  210. },1500);
  211. }else{
  212. console.log(111)
  213. updateAlert(data.msg,'alert-danger');
  214. setTimeout(function(){
  215. $(that).removeClass('disabled').prop('disabled',false);
  216. if (data.url) {
  217. location.href=data.url;
  218. }else{
  219. $('#top-alert').find('button').click();
  220. }
  221. },1500);
  222. }
  223. });
  224. }
  225. /**顶部警告栏*/
  226. var top_alert = $('#top-alert');
  227. top_alert.find('.close').on('click', function () {
  228. top_alert.removeClass('block').slideUp(200);
  229. });
  230. window.updateAlert = function (text,c) {
  231. text = text||'default';
  232. c = c||false;
  233. if ( text!='default' ) {
  234. top_alert.find('.alert-content').text(text);
  235. if (top_alert.hasClass('block')) {
  236. } else {
  237. top_alert.addClass('block').slideDown(200);
  238. }
  239. } else {
  240. if (top_alert.hasClass('block')) {
  241. top_alert.removeClass('block').slideUp(200);
  242. }
  243. }
  244. if ( c!=false ) {
  245. top_alert.removeClass('alert-danger alert-warning alert-info alert-success').addClass(c);
  246. }
  247. };
  248. window.updateAlertSuccess = function (text,t) {
  249. updateAlert(text,'alert-success');
  250. setTimeout(function(){
  251. $(that).removeClass('disabled').prop('disabled',false);
  252. if (t==1) {
  253. location.reload();
  254. }else{
  255. $('#top-alert').find('button').click();
  256. }
  257. },1500);
  258. };
  259. window.updateAlertError = function (text,t) {
  260. updateAlert(text,'alert-danger');
  261. setTimeout(function(){
  262. $(that).removeClass('disabled').prop('disabled',false);
  263. if (t==1) {
  264. location.reload();
  265. }else{
  266. $('#top-alert').find('button').click();
  267. }
  268. },1500);
  269. };
  270. /* 设置表单的值 */
  271. function formSetValue(name, value){
  272. var first = name.substr(0,1), input, i = 0, val;
  273. if(value === "") return;
  274. if("#" === first || "." === first){
  275. input = $(name);
  276. } else {
  277. input = $("[name='" + name + "']");
  278. }
  279. if(input.eq(0).is(":radio")) { //单选按钮
  280. input.filter("[value='" + value + "']").each(function(){this.checked = true});
  281. } else if(input.eq(0).is(":checkbox")) { //复选框
  282. if(!$.isArray(value)){
  283. val = new Array();
  284. val[0] = value;
  285. } else {
  286. val = value;
  287. }
  288. for(i = 0, len = val.length; i < len; i++){
  289. input.filter("[value='" + val[i] + "']").each(function(){this.checked = true});
  290. }
  291. } else { //其他表单选项直接设置值
  292. input.val(value);
  293. }
  294. }
  295. /**
  296. * 列表页排序
  297. * @param _self
  298. */
  299. function updateSort(_self) {
  300. var url = $(_self).attr('data-url');;
  301. var id = $(_self).attr('data-id');
  302. var old = $(_self).attr('data-old');
  303. var sort = $(_self).val();
  304. $.post(url,{id:id,sort:sort},function(data){
  305. if(data.code){
  306. $("#table").trigger("reloadGrid");
  307. }else{
  308. $(_self).val(old);
  309. updateAlert(data.msg,'alert-danger');
  310. setTimeout(function(){
  311. if (data.url) {
  312. location.href=data.url;
  313. }else{
  314. $('#top-alert').find('button').click();
  315. }
  316. },1500);
  317. }
  318. });
  319. }
  320. $.fn.serializeObject = function()
  321. {
  322. var o = {};
  323. var a = this.serializeArray();
  324. $.each(a, function() {
  325. if (o[this.name]) {
  326. if (!o[this.name].push) {
  327. o[this.name] = [o[this.name]];
  328. }
  329. o[this.name].push(this.value || '');
  330. } else {
  331. o[this.name] = this.value || '';
  332. }
  333. });
  334. return o;
  335. };
  336. //搜索
  337. $('#search-btn').on('click', function() {
  338. var self = $("#form-search");
  339. var url = self.attr('action');
  340. console.log('search',url);
  341. if(url){
  342. var pdata = self.serializeObject();
  343. $("#table").jqGrid('setGridParam', {
  344. url: url,
  345. postData:pdata,
  346. page: 1
  347. }).trigger("reloadGrid");
  348. }
  349. });
  350. //清除搜索
  351. $('#search-clear').on('click',function () {
  352. $("#form-search")[0].reset();
  353. var url = $("#form-search").attr('action');
  354. if(url){
  355. var pdata = $("#form-search").serializeObject();
  356. $("#table").jqGrid('setGridParam',{url:url,postData:pdata,page:1}).trigger("reloadGrid");
  357. }
  358. });
  359. /**
  360. * 输入限制,只可以输入小数点和数字且小数点后只可以输入两个数字
  361. * 一般用于onkeyup 和 onpaste方法
  362. * 例:onkeyup="clearNoNum(this)"
  363. * @param obj
  364. */
  365. function clearNoNum(obj){
  366. if(obj.value.substr(0,1)=='.'){
  367. obj.value = '';
  368. }
  369. obj.value = obj.value.replace(/[^\d.]/g,"");
  370. obj.value = obj.value.replace(/\.{2,}/g,".");
  371. obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
  372. obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
  373. if(obj.value.indexOf(".")< 0 && obj.value !=""){
  374. obj.value= parseFloat(obj.value);
  375. }
  376. }
  377. /**
  378. * 输入限制,只可以输入数字
  379. * 一般用于onkeyup 和 onpaste方法
  380. * 例:onkeyup="clearNoNum(this)"
  381. * @param obj
  382. */
  383. function clearNoNum1(obj){
  384. if(obj.value.substr(0,1)=='.'){
  385. obj.value = '';
  386. }
  387. obj.value = obj.value.replace(/[^\d.]/g,"");
  388. obj.value = obj.value.replace(/\./g,"");
  389. }
  390. /**
  391. * 输入限制,只可以输入小数点和数字且小数点后只可以输入一个数字
  392. * 一般用于onkeyup 和 onpaste方法
  393. * 例:onkeyup="clearNoNum(this)"
  394. * @param obj
  395. */
  396. function clearNoNum2(obj){
  397. if(obj.value.substr(0,1)=='.'){
  398. obj.value = '';
  399. }
  400. obj.value = obj.value.replace(/[^\d.]/g,"");
  401. obj.value = obj.value.replace(/\.{2,}/g,".");
  402. obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
  403. obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d).*$/,'$1$2.$3');
  404. if(obj.value.indexOf(".")< 0 && obj.value !=""){
  405. obj.value= parseFloat(obj.value);
  406. }
  407. }
  408. /**
  409. * 清除“数字”以外的字符
  410. * 一般用于onkeyup 和 onpaste方法
  411. * 例:onkeyup="clearNoNum(this)"
  412. * @param obj
  413. */
  414. function clearNoNum3(obj){
  415. obj.value = obj.value.replace(/[^\d]/g,""); //清除“数字”以外的字符
  416. }
  417. //清除特殊字符
  418. function stripscript(obj) {
  419. var s = $(obj).val();
  420. var pattern = new RegExp("[`~!@#$%+^&*()=|{}':;'\\[\\].<>/?~!@#¥……&*()——|{}【】‘;:”“'。,、?]");
  421. var rs = "";
  422. for (var i = 0; i < s.length; i++) {
  423. rs = rs + s.substr(i, 1).replace(pattern, '');
  424. }
  425. $(obj).val(rs);
  426. }
  427. function layer_open(_self,flag){
  428. var url = $(_self).attr('url');
  429. var title = $(_self).attr('data-title');
  430. var width = $(_self).attr('data-width')?$(_self).attr('data-width'):'90%';
  431. var height = $(_self).attr('data-height')?$(_self).attr('data-height'):'90%';
  432. layer.open({
  433. type: 2,
  434. title: title,
  435. shadeClose: true,
  436. shade: 0.3,
  437. maxmin: true,
  438. area: [width, height],
  439. content: url,
  440. end: function () {
  441. if(flag==1){
  442. $("#table").trigger("reloadGrid");
  443. }else if(flag==2){
  444. window.location.reload();
  445. }
  446. }
  447. });
  448. }
  449. //打开查看图片
  450. function open_img(_self) {
  451. var src = $(_self).attr('src');
  452. var img = new Image();
  453. img.src = src;
  454. var width = $(window).width()*0.8;
  455. var height = $(window).height()*0.8;
  456. img.onload = function(){
  457. var imgw = img.width;
  458. var imgh = img.height;
  459. if(imgw > width&&imgh < height){
  460. imgh = imgh*width/imgw;
  461. imgw = width;
  462. }else if(imgw < width&&imgh > height){
  463. imgw = imgw*height/imgh;
  464. imgh = height;
  465. }else if(imgw > width&&imgh > height){
  466. var bl1 = width/height;
  467. var bl2 = imgw/imgh;
  468. if(bl1 < bl2){
  469. imgh = imgh*width/imgw;
  470. imgw = width;
  471. }else if(bl1 > bl2){
  472. imgw = imgw*height/imgh;
  473. imgh = height;
  474. }else{
  475. imgh = height;
  476. imgw = width;
  477. }
  478. }
  479. var imgs = '<img width="'+imgw+'" height="'+imgh+'" src="'+src+'" />';
  480. layer.open({
  481. type: 1,
  482. title: false,
  483. closeBtn: 1,
  484. area: [imgw+'px', imgh+'px'],
  485. skin: 'layui-layer-nobg', //没有背景色
  486. shadeClose: true,
  487. content: imgs
  488. });
  489. };
  490. }
  491. $('.cancel-btn').click(function () {
  492. parent.layer.closeAll();
  493. })
  494. $('#upload').click(function () {
  495. var url = $(this).attr('data-url');
  496. layer.open({
  497. type: 2,
  498. title: '导入',
  499. shadeClose: true,
  500. shade: 0.3,
  501. maxmin: true,
  502. area: ['90%', '90%'],
  503. content: url,
  504. });
  505. })
  506. $('#export').click(function () {
  507. var url = $(this).attr('data-url');
  508. var data = $("#form-search").serialize();
  509. window.location.href=url+'?'+data;
  510. })
  511. function layerOpen(_self){
  512. var url = $(_self).attr('url');
  513. var title = $(_self).attr('data-title');
  514. layer.open({
  515. type: 2,
  516. title: title,
  517. shadeClose: true,
  518. shade: 0.3,
  519. area: ['90%', '90%'],
  520. content: url,
  521. });
  522. }