common.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. if(url){
  341. var pdata = self.serializeObject();
  342. $("#table").jqGrid('setGridParam', {
  343. url: url,
  344. postData:pdata,
  345. page: 1
  346. }).trigger("reloadGrid");
  347. }
  348. });
  349. //清除搜索
  350. $('#search-clear').on('click',function () {
  351. $("#form-search")[0].reset();
  352. var url = $("#form-search").attr('action');
  353. if(url){
  354. var pdata = $("#form-search").serializeObject();
  355. $("#table").jqGrid('setGridParam',{url:url,postData:pdata,page:1}).trigger("reloadGrid");
  356. }
  357. });
  358. /**
  359. * 输入限制,只可以输入小数点和数字且小数点后只可以输入两个数字
  360. * 一般用于onkeyup 和 onpaste方法
  361. * 例:onkeyup="clearNoNum(this)"
  362. * @param obj
  363. */
  364. function clearNoNum(obj){
  365. if(obj.value.substr(0,1)=='.'){
  366. obj.value = '';
  367. }
  368. obj.value = obj.value.replace(/[^\d.]/g,"");
  369. obj.value = obj.value.replace(/\.{2,}/g,".");
  370. obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
  371. obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
  372. if(obj.value.indexOf(".")< 0 && obj.value !=""){
  373. obj.value= parseFloat(obj.value);
  374. }
  375. }
  376. /**
  377. * 输入限制,只可以输入数字
  378. * 一般用于onkeyup 和 onpaste方法
  379. * 例:onkeyup="clearNoNum(this)"
  380. * @param obj
  381. */
  382. function clearNoNum1(obj){
  383. if(obj.value.substr(0,1)=='.'){
  384. obj.value = '';
  385. }
  386. obj.value = obj.value.replace(/[^\d.]/g,"");
  387. obj.value = obj.value.replace(/\./g,"");
  388. }
  389. /**
  390. * 输入限制,只可以输入小数点和数字且小数点后只可以输入一个数字
  391. * 一般用于onkeyup 和 onpaste方法
  392. * 例:onkeyup="clearNoNum(this)"
  393. * @param obj
  394. */
  395. function clearNoNum2(obj){
  396. if(obj.value.substr(0,1)=='.'){
  397. obj.value = '';
  398. }
  399. obj.value = obj.value.replace(/[^\d.]/g,"");
  400. obj.value = obj.value.replace(/\.{2,}/g,".");
  401. obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
  402. obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d).*$/,'$1$2.$3');
  403. if(obj.value.indexOf(".")< 0 && obj.value !=""){
  404. obj.value= parseFloat(obj.value);
  405. }
  406. }
  407. /**
  408. * 清除“数字”以外的字符
  409. * 一般用于onkeyup 和 onpaste方法
  410. * 例:onkeyup="clearNoNum(this)"
  411. * @param obj
  412. */
  413. function clearNoNum3(obj){
  414. obj.value = obj.value.replace(/[^\d]/g,""); //清除“数字”以外的字符
  415. }
  416. //清除特殊字符
  417. function stripscript(obj) {
  418. var s = $(obj).val();
  419. var pattern = new RegExp("[`~!@#$%+^&*()=|{}':;'\\[\\].<>/?~!@#¥……&*()——|{}【】‘;:”“'。,、?]");
  420. var rs = "";
  421. for (var i = 0; i < s.length; i++) {
  422. rs = rs + s.substr(i, 1).replace(pattern, '');
  423. }
  424. $(obj).val(rs);
  425. }
  426. function layer_open(_self,flag){
  427. var url = $(_self).attr('url');
  428. var title = $(_self).attr('data-title');
  429. var width = $(_self).attr('data-width')?$(_self).attr('data-width'):'90%';
  430. var height = $(_self).attr('data-height')?$(_self).attr('data-height'):'90%';
  431. layer.open({
  432. type: 2,
  433. title: title,
  434. shadeClose: true,
  435. shade: 0.3,
  436. maxmin: true,
  437. area: [width, height],
  438. content: url,
  439. end: function () {
  440. if(flag==1){
  441. $("#table").trigger("reloadGrid");
  442. }else if(flag==2){
  443. window.location.reload();
  444. }
  445. }
  446. });
  447. }
  448. //打开查看图片
  449. function open_img(_self) {
  450. var src = $(_self).attr('src');
  451. var img = new Image();
  452. img.src = src;
  453. var width = $(window).width()*0.8;
  454. var height = $(window).height()*0.8;
  455. img.onload = function(){
  456. var imgw = img.width;
  457. var imgh = img.height;
  458. if(imgw > width&&imgh < height){
  459. imgh = imgh*width/imgw;
  460. imgw = width;
  461. }else if(imgw < width&&imgh > height){
  462. imgw = imgw*height/imgh;
  463. imgh = height;
  464. }else if(imgw > width&&imgh > height){
  465. var bl1 = width/height;
  466. var bl2 = imgw/imgh;
  467. if(bl1 < bl2){
  468. imgh = imgh*width/imgw;
  469. imgw = width;
  470. }else if(bl1 > bl2){
  471. imgw = imgw*height/imgh;
  472. imgh = height;
  473. }else{
  474. imgh = height;
  475. imgw = width;
  476. }
  477. }
  478. var imgs = '<img width="'+imgw+'" height="'+imgh+'" src="'+src+'" />';
  479. layer.open({
  480. type: 1,
  481. title: false,
  482. closeBtn: 1,
  483. area: [imgw+'px', imgh+'px'],
  484. skin: 'layui-layer-nobg', //没有背景色
  485. shadeClose: true,
  486. content: imgs
  487. });
  488. };
  489. }
  490. $('.cancel-btn').click(function () {
  491. parent.layer.closeAll();
  492. })
  493. $('#upload').click(function () {
  494. var url = $(this).attr('data-url');
  495. layer.open({
  496. type: 2,
  497. title: '导入',
  498. shadeClose: true,
  499. shade: 0.3,
  500. maxmin: true,
  501. area: ['90%', '90%'],
  502. content: url,
  503. });
  504. })
  505. $('#export').click(function () {
  506. var url = $(this).attr('data-url');
  507. var data = $("#form-search").serialize();
  508. window.location.href=url+'?'+data;
  509. })
  510. function layerOpen(_self){
  511. var url = $(_self).attr('url');
  512. var title = $(_self).attr('data-title');
  513. layer.open({
  514. type: 2,
  515. title: title,
  516. shadeClose: true,
  517. shade: 0.3,
  518. area: ['90%', '90%'],
  519. content: url,
  520. });
  521. }