HTML
JAVASCRIPT
var int_check = /^-?[0-9]*$/; var float_check = /^[+|-]?\d*\.?\d*$/; var date_check = /^(([1-9]\d{0,3}|0)\-\d{2}\-\d{2})|(([1-9]\d{0,3}|0)\.\d{2}\.\d{2})|(([1-9]\d{0,3}|0)\/\d{2}\/\d{2})$/; var email_check = /^[a-z0-9][-a-z0-9\.]*@([a-z0-9-]+\.)+[a-z]+$/; //密碼格式確認 jQuery.checkPasswordFormat = function(id_tag, message) { // input 欄位驗證 var value = $('#'+id_tag).val(); if (value.length 1 && value[value.length-1] == '0'){ value = value.slice(0, value.length-1); } if (value.length > 1 && value[value.length-1] == '.'){ value = value.slice(0, value.length-1); } } return value; } //浮點數相加 function FloatAdd(arg1, arg2){ var r1, r2, m; try { r1 = arg1.toString().split(".")[1].length; } catch (e) { r1 = 0; } try { r2 = arg2.toString().split(".")[1].length; } catch (e) { r2 = 0; } m = Math.pow(10, Math.max(r1, r2)); return (FloatMul(arg1, m) + FloatMul(arg2, m)) / m; } //浮點數相減 function FloatSubtraction(arg1, arg2){ var r1, r2, m, n; try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 } try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 } m = Math.pow(10, Math.max(r1, r2)); n = (r1 >= r2) ? r1 : r2; return ((arg1 * m - arg2 * m) / m).toFixed(n); } //浮點數相乘 function FloatMul(arg1, arg2){ var m = 0, s1 = arg1.toString(), s2 = arg2.toString(); try { m += s1.split(".")[1].length; } catch (e) { } try { m += s2.split(".")[1].length; } catch (e) { } return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m); } //浮點數相除 function FloatDiv(arg1, arg2){ var t1 = 0, t2 = 0, r1, r2; try { t1 = arg1.toString().split(".")[1].length } catch (e) { } try { t2 = arg2.toString().split(".")[1].length } catch (e) { } with (Math) { r1 = Number(arg1.toString().replace(".", "")) r2 = Number(arg2.toString().replace(".", "")) return (r1 / r2) * pow(10, t2 - t1); } } //用來轉換成為千分位表示的數字用的 function TransformThousands(num) { var date_check=/^(([1-9]\d{0,3}|0)\-\d{2}\-\d{2})|(([1-9]\d{0,3}|0)\.\d{2}\.\d{2})|(([1-9]\d{0,3}|0)\/\d{2}\/\d{2})$/; if (date_check.test(num)){ return num; } num = num + ""; var re = /(-?\d+)(\d{3})/; while (re.test(num)) { num = num.replace(re,"$1,$2") } return num; } //用來移除千分位表示的數字用的 function remove_TransformThousands(num) { var num = num.ReplaceAll(',', ''); return num; } //轉換input千分位符號使用 function inputcomma() { var $obj = $(this); var value = $obj.val(); $obj.val(TransformThousands(value)); } //移除input千分位符號使用 function remove_inputcomma() { var $obj = $(this); var value = $obj.val(); $obj.val(remove_TransformThousands(value)); } jQuery.needExist = function(id_tag, name) { // input 欄位驗證 var value = $('#'+id_tag).val(); if (value == '') { alert('未填寫 '+ name + ' 欄位'); throw ""; return false; } else { return value; } } //進行(顯示部分-輸入框)物件切換 function ClickShowInfo(){ var $obj = $(this); var field_name = $obj.attr('field_name'); var row_id = $obj.attr('row_id'); $('#show_part_'+field_name+"_"+row_id).hide(); $('#edit_part_'+field_name+"_"+row_id).show().focus(); } //送資料 function BlurUpdateInfo(){ //整理欄位資訊 var $obj = $(this); var do_nothing = $obj.attr('do_nothing'); if (do_nothing){ return false; } var row_id = $obj.attr('row_id'); var field_type = $obj.attr('field_type'); //int、str、float、date、email var field_name = $obj.attr('field_name'); var table_name = $obj.attr('table_name'); var module_name = $obj.attr('module_name'); var class_name = $obj.attr('class'); var value = $obj.val(); var old_value = $obj.attr('old_value'); if($obj.hasClass("form-control")){ $obj.parent().removeClass('has-error'); } if (value==old_value){ $obj.tooltip('destroy'); return false; } if (!value){ value = null; }; //判斷是否為選單(ForeignKey 欄位) var is_select = $obj.attr('is_select'); var listname_field_name = 'listname_'+field_name; var check_value = true; //檢查各種格式是否正確 if ((class_name.indexOf('needExist')) > -1 && !value){ $obj.tooltip({title:"不可為空值!",trigger:"manual"}).tooltip('show'); check_value = false; }; if (field_type=='int' && value){ value = value.ReplaceAll(',', ''); if(!(int_check.test(value))){ $obj.tooltip({title:"須為整數!",trigger:"manual"}).tooltip('show'); check_value = false; } }; if (field_type=='float' && value){ value = value.ReplaceAll(',', ''); if(!(float_check.test(value))){ $obj.tooltip({title:"須為數字!",trigger:"manual"}).tooltip('show'); check_value = false; } }; if (field_type=='date' && value){ if(!(date_check.test(value))){ $obj.tooltip({title:"須為日期格式(如2013-01-19)!",trigger:"manual"}).tooltip('show'); check_value = false; } }; if (field_type=='email' && value){ if(!(email_check.test(value))){ $obj.tooltip({title:"須為Email格式!",trigger:"manual"}).tooltip('show'); check_value = false; } }; if (!check_value){ $obj.val(old_value); if($obj.hasClass("form-control")){ $obj.parent().addClass("has-error"); } $obj.focus().select(); return false; } if (value == 'true'){ value = true; } else if (value == 'false'){ value = false; }; $obj.tooltip('destroy'); //移除tooltip //送資料到進行修改 var url = '/'+module_name+'/api/v1/'+table_name+'/'+row_id+'/'; if (value!=old_value){ var data = { csrfmiddlewaretoken: CSRFMIDDLEWARETOKEN } data[field_name] = value; $.ajax({ url: url, type: 'PUT', data: JSON.stringify(data), contentType: 'application/json', dataType: 'json', success: function (json, text, xhr) { //取新的資料回來修改template $.ajax({ url: url, type: 'GET', contentType: 'application/json', dataType: 'json', success: function (json, text, xhr) { if (is_select){ $obj.attr('value', value); $obj.attr('old_value', value); if (!json[listname_field_name]){ json[listname_field_name] = ''; } $('#show_part_'+field_name+"_"+row_id).html(json[listname_field_name]); } else { if (field_type=='float'||field_type=='int'){ json[field_name] = cutzero(json[field_name]); }; if (!json[field_name] || json[field_name] == 'null'){ json[field_name] = ''; } $obj.attr('value', json[field_name]); $obj.attr('old_value', json[field_name]); if (json[field_name] == 0){ var listname_value = '0'; } else if (!json[field_name]){ var listname_value = ''; } else { var listname_value = String(json[field_name]).ReplaceAll('\n', '
'); }; if (field_type=='float'||field_type=='int'){ listname_value = TransformThousands(listname_value); listname_value = listname_value.replace('.000', ''); } else if (field_type=='str') { if (listname_value=='0'){ listname_value = ''; } }; $('#show_part_'+field_name+"_"+row_id).html(listname_value); } }, error: function (data) { alert(data.responseText); } }) }, error: function (data) { $obj.val(old_value); if (data.responseText){ alert(data.responseText); } else { alert('操作失敗,可能系統忙碌中,請稍候重試一次。'); } }, }) }; //將顯示物件切換回來 if (!$obj.attr('no_change')){ $('#show_part_'+field_name+"_"+row_id).show(); $('#edit_part_'+field_name+"_"+row_id).hide(); } } function deleteRow(){ var $obj = $(this); var module_name = $obj.attr('module_name'); var table_name = $obj.attr('table_name'); var remove_target = $obj.attr('remove_target'); //刪除完畢後要移除的物件 var hide_target = $obj.attr('hide_target'); //刪除完畢後要隱藏的物件 var next_url = $obj.attr('next_url'); //刪除完畢後頁面轉移的連結 var do_change_action = $obj.attr('do_change_action'); //刪除完畢後什麼物件要執行.change()的動作 var row_id = $obj.attr('row_id'); var message = $obj.attr('message'); var row_name = $obj.attr('row_name'); if (!message){ message = '您確定要刪除『 '+row_name+' 』嗎?'; } var data = {csrfmiddlewaretoken: CSRFMIDDLEWARETOKEN}; if (confirm(message)){ $.ajax({ url: '/' + module_name + '/api/v1/' + table_name + '/' + row_id + '/', type: 'DELETE', data: JSON.stringify(data), contentType: 'application/json', dataType: 'json', success: function (json, text, xhr) { if (remove_target != '' && remove_target != undefined) { $('#' + remove_target).remove(); $('.' + remove_target).remove(); } if (next_url != '' && next_url != undefined){ window.location = next_url; } if (hide_target != '' && hide_target != undefined){ $('#' + hide_target).hide(); } if (do_change_action != '' && do_change_action != undefined){ $(do_change_action).change(); } }, error: function (data) { alert(data.responseText); } }) } else { return false; } } $(document).ready(function(){ var resizeAll = function(){ var wid = $(window).width(), // get window width headwid = $('.social-menu-top .head').width(), ulwid = wid -headwid - 30; $('.social-menu-top .details').css({ 'width': ulwid+'px' }); // apply CSS changes }; $(window).resize( function() { resizeAll(); } ); // apply function when device width changes resizeAll(); // apply function when loading the page CSRFMIDDLEWARETOKEN = $('input[name=csrfmiddlewaretoken]').val(); $('.ClickShowInfo').click(ClickShowInfo); $('.BlurUpdateInfo').blur(BlurUpdateInfo); $('.BlurUpdateInfo').keypress(function(event) { var $obj = $(this); if ($obj.attr('type')!='textarea' && event.which == 13){ $obj.blur(); } }); $('.deleteRow').click(deleteRow); $.each($('.inputcomma'), inputcomma); // 每一個input直接變換千分位 $('.inputcomma').blur(inputcomma); // input 改變後的數字變換千分位 $('.inputcomma').click(remove_inputcomma); // input 點擊時要先移除千分位符號 $("input[type='text']").focus(function () {//input會先選擇全部的值 $(this).select(); }); $('.datepicker').datepicker({ format: "yyyy-mm-dd", todayBtn: "linked", language: "zh-TW", autoclose: true, orientation: "top auto", todayHighlight: true, }).on('changeDate', BlurUpdateInfo); $(document).ajaxStart(function(){ var $img = $('#loading'); var screenTop = $(document).scrollTop(); var browserHeight = getBrowserHeight(); $img.css({ position: 'absolute', top: (screenTop+browserHeight/2) + 'px', left: '50%' }); $("body").css('opacity', 0.4); $img.css('z-index', 10000).show(); }).ajaxStop(function(){ var $img = $('#loading'); $("body").css('opacity', 1); $img.hide(); }); });
全站熱搜
留言列表