Js Util工具集

JSON 数据还原 防止后台JSON转换异常

var FastJson = {
    isArray: function(a) {
        return "object" == typeof a && "[object array]" == Object.prototype.toString.call(a)
            .toLowerCase();
    },
    isObject: function(a) {
        return "object" == typeof a && "[object object]" == Object.prototype.toString.call(a)
            .toLowerCase();
    },
    format: function(a) {
        if (null == a)
            return null;
        "string" == typeof a && (a = eval("(" + a + ")"));
        return this._format(a, a, null, null, null);
    },
    _randomId: function() {
        return "randomId_" + parseInt(1E9 * Math.random());
    },
    _getJsonValue: function(a, c) {
        var d = this._randomId(),
            b;
        b = "" + ("function " + d + "(root){") + ("return root." + c + ";");
        b += "}";
        b += "";
        var e = document.createElement("script");
        e.id = d;
        e.text = b;
        document.body.appendChild(e);
        d = window[d](a);
        e.parentNode.removeChild(e);
        return d;
    },
    _format: function(a, c, d, b, e) {
        d || (d = "");
        if (this.isObject(c)) {
            if (c.$ref) {
                var g = c.$ref;
                0 == g.indexOf("$.") && (b[e] = this._getJsonValue(a, g.substring(2)));
                return
            }
            for (var f in c)
                b = d, "" != b && (b += "."), g = c[f], b += f, this
                ._format(a, g, b, c, f);
        } else if (this.isArray(c))
            for (f in c)
                b = d, g = c[f], b = b + "[" + f + "]", this._format(a, g,
                    b, c, f);
        return a;
    }
};
isArray: function(a) {
        return "object" == typeof a && "[object array]" == Object.prototype.toString.call(a)
            .toLowerCase();
    },
    isObject: function(a) {
        return "object" == typeof a && "[object object]" == Object.prototype.toString.call(a)
            .toLowerCase();
    },
    format: function(a) {
        if (null == a)
            return null;
        "string" == typeof a && (a = eval("(" + a + ")"));
        return this._format(a, a, null, null, null);
    },
    _randomId: function() {
        return "randomId_" + parseInt(1E9 * Math.random());
    },
    _getJsonValue: function(a, c) {
        var d = this._randomId(),
            b;
        b = "" + ("function " + d + "(root){") + ("return root." + c + ";");
        b += "}";
        b += "";
        var e = document.createElement("script");
        e.id = d;
        e.text = b;
        document.body.appendChild(e);
        d = window[d](a);
        e.parentNode.removeChild(e);
        return d;
    },
    _format: function(a, c, d, b, e) {
        d || (d = "");
        if (this.isObject(c)) {
            if (c.$ref) {
                var g = c.$ref;
                0 == g.indexOf("$.") && (b[e] = this._getJsonValue(a, g.substring(2)));
                return
            }
            for (var f in c)
                b = d, "" != b && (b += "."), g = c[f], b += f, this
                ._format(a, g, b, c, f);
        } else if (this.isArray(c))
            for (f in c)
                b = d, g = c[f], b = b + "[" + f + "]", this._format(a, g,
                    b, c, f);
        return a;
    }
};

POST提交

function post(URL, PARAMS) {
    var temp = document.createElement("form");
    temp.action = URL;
    temp.method = "post";
    temp.style.display = "none";
    for (var x in PARAMS) {
        var opt = document.createElement("textarea");
        opt.name = x;
        opt.value = PARAMS[x];
        temp.appendChild(opt);
    }
    document.body.appendChild(temp);
    temp.submit();
    return temp;
}

时间转换JS

function getDateTime(data) {
    if (data == undefined) {
        return "";
    }
    var d = new Date(data); //根据时间戳生成的时间对象
    var date = (d.getFullYear()) + "-" + (d.getMonth() + 1) + "-" + (d.getDate());
    return date;
}

具体时间转换 精确到秒

function getDateTimeHours(data) {
    if (data == undefined) {
        return "";
    }
    var d = new Date(data); //根据时间戳生成的时间对象
    var date = (d.getFullYear()) + "-" + (d.getMonth() + 1) + "-" + (d.getDate()) + " " + (d.getHours()) + ":" + (d.getMinutes()) + ":" + (d.getSeconds());
    return date;
}

具体时间 精确到分

function getDateTimeMinute(data) {
    if (data == undefined) {
        return "";
    }
    var d = new Date(data); //根据时间戳生成的时间对象
    var date = (d.getFullYear()) + "-" + (d.getMonth() + 1) + "-" + (d.getDate()) + " " + (d.getHours()) + ":" + (d.getMinutes());
    return date;
}

时间范围筛选 这里需要引入第三方datepicker JS插件

function DatePicker(beginSelector, endSelector) {
    //仅选择日期
    $(beginSelector).datepicker({
        language: "zh-CN",
        autoclose: true,
        startView: 0,
        format: "yyyy-mm-dd",
        clearBtn: true,
        todayBtn: false,
        endDate: new Date()
    }).on('changeDate', function(ev) {
        if (ev.date) {
            $(endSelector).datepicker('setStartDate', new Date(ev.date.valueOf()));
            $(beginSelector).datepicker('hide');
            $(endSelector).datepicker('show');
        } else {
            $(endSelector).datepicker('setStartDate', null);
        }
    })
    $(endSelector).datepicker({
        language: "zh-CN",
        autoclose: true,
        startView: 0,
        format: "yyyy-mm-dd",
        clearBtn: true,
        todayBtn: false,
        endDate: new Date()
    }).on('changeDate', function(ev) {
        if (ev.date) {
            $(beginSelector).datepicker('setEndDate', new Date(ev.date.valueOf()))
        } else {
            $(beginSelector).datepicker('setEndDate', new Date());
        }
    })
}

格式化金额 保留后2位小数

function formatCurrency(num) {
    if (num == undefined || num == '') {
        return 0.00;
    }
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
        num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + num + '.' + cents);
}

时间范围筛选 可以选中未来时间

function DatePicker2(beginSelector, endSelector) {
    //仅选择日期
    $(beginSelector).datepicker({
        language: "zh-CN",
        autoclose: true,
        startView: 0,
        format: "yyyy-mm-dd",
        clearBtn: true,
        todayBtn: false
    }).on('changeDate', function(ev) {
        if (ev.date) {
            $(endSelector).datepicker('setStartDate', new Date(ev.date.valueOf()));
            $(beginSelector).datepicker('hide');
            $(endSelector).datepicker('show');
        } else {
            $(endSelector).datepicker('setStartDate', null);
        }
    })
    $(endSelector).datepicker({
        language: "zh-CN",
        autoclose: true,
        startView: 0,
        format: "yyyy-mm-dd",
        clearBtn: true,
        todayBtn: false
    }).on('changeDate', function(ev) {
        if (ev.date) {
            $(beginSelector).datepicker('setEndDate', new Date(ev.date.valueOf()))
        } else {
            $(beginSelector).datepicker('setEndDate', new Date());
        }
    })
}

时间范围筛选 今天开始 月份

function DatePicker3(endSelector) {
    $(endSelector).datepicker({
        language: "zh-CN",
        autoclose: true,
        todayHighlight: true,
        format: 'yyyy-mm',
        startView: 3,
        minViewMode: 1,
        maxViewMode: 3,
        forceParse: 0,
    }).datepicker('setStartDate', new Date());
}

时间范围筛选 今天开始 日期

function DatePicker4(endSelector) {
    //仅选择日期
    $(endSelector).datepicker({
        language: "zh-CN",
        autoclose: true,
        startView: 0
        format: "yyyy-mm-dd",
        clearBtn: true,
        todayBtn: false
    }).datepicker('setStartDate', new Date());
}
function showdatepicker(endSelector) {
    $(endSelector).datepicker({
        language: "zh-CN",
        monthNamesShort: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],  区域化月名为中文
        prevText: '上月',  //前选按钮提示
        nextText: '下月',  //后选按钮提示
        changeYear: true,  //年下拉菜单
        changeMonth: true,  //月下拉菜单
        showButtonPanel: true,  //显示按钮面板
        showMonthAfterYear: true,  //月份显示在年后面
        currentText: "本月",  //当前日期按钮提示文字
        closeText: "关闭",  //关闭按钮提示文字
        dateFormat: "yy-mm",  //日期格式
    });
}

AJAX 同步

function sync(url, PARAMS) {
    return ajax_util(url, PARAMS, false);
}
//AJAX 异步
function async(url, PARAMS) {
    return ajax_util(url, PARAMS, true);
}
//AJAX工具JS
function ajax_util(url, PARAMS, is_a_s_ync) {
    var _data;
    $.ajax({
        cache: false,
        type: "POST",
        url: contextPath + url,
        data: PARAMS,
        async: is_a_s_ync,
        error: function(request) {
            alert("Connection error");
        },
        success: function(data) {
            _data = FastJson.format(data);
        },
        error: function() {
            alert("Ajax请求数据失败!");
        }
    });
    return _data;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • AJAX 原生js操作ajax 1.创建XMLHttpRequest对象 var xhr = new XMLHtt...
    碧玉含香阅读 8,706评论 0 7
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,564评论 25 709
  • 1.几种基本数据类型?复杂数据类型?值类型和引用数据类型?堆栈数据结构? 基本数据类型:Undefined、Nul...
    极乐君阅读 11,022评论 0 106
  • 推荐10套最适合孩子阅读的少儿英语书。 英语作为全球通用语言,学习的重要性不言而喻,少儿是人类成长学习的最佳时期,...
    黑鸿升阅读 18,249评论 0 3
  • 文/爱晓贱 所有的花言巧语都得绞尽脑汁的酝酿 姑娘,听说有一种眼神叫看见你就发光 全部的放荡不羁都想恰到好处的隐藏...
    落雪有晴空阅读 4,342评论 10 6

友情链接更多精彩内容