formatDate

//先扩展一下javascript的Date类型,增加一个函数,用于返回我们想要的 yyyy-MM-dd HH:mm:ss 这种时间格式
Date.prototype.format = function (pattern) {
    /*初始化返回值字符串*/
    var returnValue = pattern;
    /*正则式pattern类型对象定义*/
    var format = {
        "y+": this.getFullYear(),
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "H+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "S": this.getMilliseconds(),
        "h+": (this.getHours() % 12),
        "a": (this.getHours() / 12) <= 1 ? "AM" : "PM"
    };
    /*遍历正则式pattern类型对象构建returnValue对象*/
    for (var key in format) {
        var regExp = new RegExp("(" + key + ")");
        if (regExp.test(returnValue)) {
            var zero = "";
            for (var i = 0; i < RegExp.$1.length; i++) { zero += "0"; }
            var replacement = RegExp.$1.length == 1 ? format[key] : (zero + format[key]).substring((("" + format[key]).length));
            returnValue = returnValue.replace(RegExp.$1, replacement);
        }
    }
    return returnValue;
};

//翻页-------------------------------------------------------------------------------
function turnPage(pageIndex) {
    var strWhereVal = $("#strWhere").val()
    if ($("#strWhere") == undefined || $("#strWhere") == null ||
        $("#strWhere").val() == undefined || $("#strWhere").val() == null)
        getDataDrawTable("", pageIndex, UrlAction);
    else
        getDataDrawTable(strWhereVal, pageIndex, UrlAction);
}
function homePage() {
    turnPage(1);
}
function previousPage() {
    turnPage(parseInt($("#PageIndex").val()) - 1);
}
function nextPage() {
    turnPage(parseInt($("#PageIndex").val()) + 1);
}
function endPage() {
    turnPage(parseInt($("#TotalPages").val()));
}

function getDataDrawTable(strWhere, pageIndex, actionUrl) {
    $.ajax({
        url: actionUrl,
        data: { strWhere: strWhere, pageIndex: pageIndex },
        type: "POST",
        dataType: 'text',
        success: function (data) {
            var pageTable = eval('(' + data + ')');
            drawTable(pageTable.DataTable);
            MakePagination(pageTable);
        },
        error: function (xhr, textStatus, error) {
            alert(error);
        }
    });
}
function ajax(url, param, successFun) {
    $.ajax({
        url: url,
        data: JSON.stringify(param),
        contentType: "application/json;charset=UTF-8",
        type: "POST",
        dataType: "json",
        async: true,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            artDialog.alert('<br>url:' + this.url
                          + '<br>data:' + this.data
                          + '<br>textStatus:' + textStatus
                          + '<br>errorThrown:' + errorThrown);
        },
        success: successFun,
    });
}
function pagination(page) {
    var TotalCount = pageTable.TotalCount;//总记录数
    var PageSize = pageTable.PageSize;//每页记录数

    var HasPreviousPage = pageTable.HasPreviousPage;//是否有上一页
    var HasNextPage = pageTable.HasNextPage;//是否有下一页
    var PageIndex = pageTable.PageIndex;//当前页
    var TotalPages = pageTable.TotalPages;//总页数

    $("#PageIndex").val(PageIndex);
    $("#TotalPages").val(TotalPages);

    var strHomePagePreviousPage = "<a href='javascript:void(0)' onclick='homePage()'>首页</a>"
                                + "<a href='javascript:void(0)' onclick='previousPage()'>"
                                    + "<img src='../css/image/tubiao_47.png' class='list-IMG' />"
                                + "</a>";
    var strNextPageEndPage = "<a href='javascript:void(0)' onclick='nextPage()'>"
                                    + "<img src='../css/image/tubiao_50.png' class='list-IMG' />"
                                + "</a>"
                                + "<a href='javascript:void(0)' onclick='endPage()'>尾页</a>";
    strHomePagePreviousPage = HasPreviousPage ? strHomePagePreviousPage : "";
    strNextPageEndPage = HasNextPage ? strNextPageEndPage : "";

    var strPagination = "<div class='list-YeMa' id='divYeMa'>"
                            + "<span class='JILU'>共 " + TotalCount + " 条记录,每页 " + PageSize + " 条</span>"
                            + "<div class='TiaoZhuan'>"
                                + strHomePagePreviousPage
                                + "<span class='currentPageSpan'>" + PageIndex + "/" + TotalPages + "页</span>"
                                + strNextPageEndPage
                            + "</div>"
                        + "</div>";
    $("#divYeMa").remove();
    $("#divTabel").append($(strPagination));
}

function MakePagination(pageTable) {
    var TotalCount = pageTable.TotalCount;//总记录数
    var PageSize = pageTable.PageSize;//每页记录数

    var HasPreviousPage = pageTable.HasPreviousPage;//是否有上一页
    var HasNextPage = pageTable.HasNextPage;//是否有下一页
    var PageIndex = pageTable.PageIndex;//当前页
    var TotalPages = pageTable.TotalPages;//总页数

    $("#PageIndex").val(PageIndex);
    $("#TotalPages").val(TotalPages);

    var strHomePagePreviousPage = "<a href='javascript:void(0)' onclick='homePage()'>首页</a>"
                                + "<a href='javascript:void(0)' onclick='previousPage()'>"
                                    + "<img src='../css/image/tubiao_47.png' class='list-IMG' />"
                                + "</a>";
    var strNextPageEndPage = "<a href='javascript:void(0)' onclick='nextPage()'>"
                                    + "<img src='../css/image/tubiao_50.png' class='list-IMG' />"
                                + "</a>"
                                + "<a href='javascript:void(0)' onclick='endPage()'>尾页</a>";
    strHomePagePreviousPage = HasPreviousPage ? strHomePagePreviousPage : "";
    strNextPageEndPage = HasNextPage ? strNextPageEndPage : "";

    var strPagination = "<div class='list-YeMa' id='divYeMa'>"
                            + "<span class='JILU'>共 " + TotalCount + " 条记录,每页 " + PageSize + " 条</span>"
                            + "<div class='TiaoZhuan'>"
                                + strHomePagePreviousPage
                                + "<span class='currentPageSpan'>" + PageIndex + "/" + TotalPages + "页</span>"
                                + strNextPageEndPage
                            + "</div>"
                        + "</div>";
    $("#divYeMa").remove();
    $("#divTabel").append($(strPagination));
}
//utility-------------------------------------------------------------------------------
function removeAll(tableId) {//清除table已有的内容
    $("#" + tableId + " tr:not(:first)").remove();
}
function GetTable_Remove(tableId) {//清除table已有的内容
    $("#" + tableId + " tr:not(:first)").remove();
    return $("#" + tableId);
}

function format(value) {
    return value == null ? "" : value;
}
function formatDate(jsonDate) {
    if (jsonDate == undefined || jsonDate == null || jsonDate == '')
        return '';
    var strDate = new Date(jsonDate).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
    //var date = new Date(jsonDate.replace(/T/g, ' '));
    var date = new Date(strDate);
    return date.format("yyyy-MM-dd HH:mm");
}//2017-06-16T15:51:55.093
function formatDate_yMd(jsonDate) {
    if (jsonDate == undefined || jsonDate == null || jsonDate == '')
        return '';
    var date = new Date(parseInt(jsonDate.toString().replace("/Date(", "").replace(")/", ""), 10));
    return date.format("yyyy-MM-dd");
}//2017-06-16T15:51:55.093
function formatDate2(jsonDate) {
    if (jsonDate == undefined || jsonDate == null || jsonDate == '')
        return '';
    //var date = new Date(parseInt(jsonDate.toString().replace("/Date(", "").replace(")/", ""), 10));
    var date = new Date(parseInt(jsonDate, 10));
    return date.format("yyyy-MM-dd HH:mm");
}//1497542400000
function formatDate3(jsonDate) {
    if (jsonDate == undefined || jsonDate == null || jsonDate == '')
        return '';
    var date = new Date(parseInt(jsonDate.toString().replace("/Date(", "").replace(")/", ""), 10));
    return date.format("yyyy-MM-dd HH:mm");
}// /Date(1436595149269)/

//DataGrid-------------------------------------------------------------------------------
function DataGrid(name, url, param, DrawTable, ifPage) {
    this.name = name;
    this.url = url;

    this.param = param;
    if (!this.param.pageIndex)
        this.param.pageIndex = 1;
    if (!this.param.pageSize)
        this.param.pageSize = 10;

    this.DrawTable = DrawTable;
    this.ifPage = ifPage;

    this.list = {};
    this.totalPage = 0;
}
DataGrid.prototype.load = function () {
    $.ajax({
        url: this.url,
        data: JSON.stringify(this.param),
        contentType: "application/json;charset=UTF-8",
        type: "POST",
        dataType: "json",
        async: true,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            artDialog.alert('<br>url:' + this.url
                          + '<br>data:' + this.data
                          + '<br>textStatus:' + textStatus
                          + '<br>errorThrown:' + errorThrown);
        },
        success: this.produceSuccessFun(),
        //success: function (data) {
        //    this.successFun(data);
        //},
    });
}
DataGrid.prototype.produceSuccessFun = function () {
    var dataGrid = this;

    function successFun(data) {
        dataGrid.list = data.rows;
        var list = dataGrid.list;
        var table = GetTable_Remove(dataGrid.name);

        if (list == null) {
            var tr = $("<tr>");
            tr.append($('<td>没有查询到数据</td>'));
            table.append(tr);
        } else {
            dataGrid.DrawTable(table, list);

            if (dataGrid.ifPage) {
                var total = data.total;
                var pageSize = dataGrid.param.pageSize;
                var pageIndex = dataGrid.param.pageIndex;

                var HasPreviousPage = pageIndex > 1;//是否有上一页
                var TotalPage = Math.ceil(total / pageSize);//总页数
                dataGrid.totalPage = TotalPage;
                var HasNextPage = pageIndex < TotalPage;//是否有下一页

                var strHomePagePreviousPage = "<a href='javascript:void(0)' onclick='" + dataGrid.name + ".homePage()'>首页</a>"
                                            + "<a href='javascript:void(0)' onclick='" + dataGrid.name + ".previousPage()'>"
                                                + "<img src='../css/image/tubiao_47.png' class='list-IMG' />"
                                            + "</a>";
                var strNextPageEndPage = "<a href='javascript:void(0)' onclick='" + dataGrid.name + ".nextPage()'>"
                                                + "<img src='../css/image/tubiao_50.png' class='list-IMG' />"
                                            + "</a>"
                                            + "<a href='javascript:void(0)' onclick='" + dataGrid.name + ".endPage()'>尾页</a>";
                strHomePagePreviousPage = HasPreviousPage ? strHomePagePreviousPage : "";
                strNextPageEndPage = HasNextPage ? strNextPageEndPage : "";

                var strPagination = "<div class='list-YeMa' id='divYeMa'>"
                                        + "<span class='JILU'>共 " + total + " 条记录,每页 " + pageSize + " 条</span>"
                                        + "<div class='TiaoZhuan'>"
                                            + strHomePagePreviousPage
                                            + "<span class='currentPageSpan'>" + pageIndex + "/" + TotalPage + "页</span>"
                                            + strNextPageEndPage
                                        + "</div>"
                                    + "</div>";

                table.nextAll().remove();
                table.parent().append($(strPagination));
            }
        }
    }
    return successFun;
};
DataGrid.prototype.turnPage = function (pageIndex) {
    this.param.pageIndex = pageIndex;
    this.load();
}
DataGrid.prototype.homePage = function () {
    this.turnPage(1);
}
DataGrid.prototype.previousPage = function () {
    this.turnPage(this.param.pageIndex - 1);
}
DataGrid.prototype.nextPage = function () {
    this.turnPage(this.param.pageIndex + 1);
}
DataGrid.prototype.endPage = function () {
    this.turnPage(this.totalPage);
}
/*DataGrid.prototype.reload = function () {
    this.turnPage(this.param.pageIndex);
}*/
//DataGridJsonParse-------------------------------------------------------------------------------
function DataGridJsonParse_Construct(name, url, DrawTable, ifPage) {
    return new DataGridJsonParse(name, url, {}, DrawTable, ifPage);
}//DataGridJsonParse的构造方法
function DataGridJsonParsePage(name, url, DrawTable) {
    return new DataGridJsonParse(name, url, {}, DrawTable, true);
}//DataGridJsonParse的构造方法Page
function DataGridJsonParse(name, url, param, DrawTable, ifPage) {
    this.name = name;
    this.url = url;

    this.param = param;
    if (!this.param.pageIndex)
        this.param.pageIndex = 1;
    if (!this.param.pageSize)
        this.param.pageSize = 10;

    this.DrawTable = DrawTable;
    this.ifPage = ifPage;

    this.list = {};
    this.totalPage = 0;
}//DataGridJsonParse类
DataGridJsonParse.prototype.load = function () {
    $.ajax({
        url: this.url,
        data: JSON.stringify(this.param),
        contentType: "application/json;charset=UTF-8",
        type: "POST",
        dataType: "json",
        async: true,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            artDialog.alert('<br>url:' + this.url
                          + '<br>data:' + this.data
                          + '<br>textStatus:' + textStatus
                          + '<br>errorThrown:' + errorThrown);
        },
        success: this.produceSuccessFun(),
        //success: function (data) {
        //    this.successFun(data);
        //},
    });
}
DataGridJsonParse.prototype.produceSuccessFun = function () {
    var dataGrid = this;

    function successFun(data) {
        data = JSON.parse(data);
        dataGrid.list = data.rows;
        var list = dataGrid.list;
        var table = GetTable_Remove(dataGrid.name);

        if (list == null) {
            var tr = $("<tr>");
            tr.append($('<td>没有查询到数据</td>'));
            table.append(tr);
        } else {
            dataGrid.DrawTable(table, list);

            if (dataGrid.ifPage) {
                var total = data.total;
                var pageSize = dataGrid.param.pageSize;
                var pageIndex = dataGrid.param.pageIndex;

                var HasPreviousPage = pageIndex > 1;//是否有上一页
                var TotalPage = Math.ceil(total / pageSize);//总页数
                dataGrid.totalPage = TotalPage;
                var HasNextPage = pageIndex < TotalPage;//是否有下一页

                var strHomePagePreviousPage = "<a href='javascript:void(0)' onclick='" + dataGrid.name + ".homePage()'>首页</a>"
                                            + "<a href='javascript:void(0)' onclick='" + dataGrid.name + ".previousPage()'>"
                                                + "<img src='../css/image/tubiao_47.png' class='list-IMG' />"
                                            + "</a>";
                var strNextPageEndPage = "<a href='javascript:void(0)' onclick='" + dataGrid.name + ".nextPage()'>"
                                                + "<img src='../css/image/tubiao_50.png' class='list-IMG' />"
                                            + "</a>"
                                            + "<a href='javascript:void(0)' onclick='" + dataGrid.name + ".endPage()'>尾页</a>";
                strHomePagePreviousPage = HasPreviousPage ? strHomePagePreviousPage : "";
                strNextPageEndPage = HasNextPage ? strNextPageEndPage : "";

                var strPagination = "<div class='list-YeMa' id='divYeMa'>"
                                        + "<span class='JILU'>共 " + total + " 条记录,每页 " + pageSize + " 条</span>"
                                        + "<div class='TiaoZhuan'>"
                                            + strHomePagePreviousPage
                                            + "<span class='currentPageSpan'>" + pageIndex + "/" + TotalPage + "页</span>"
                                            + strNextPageEndPage
                                        + "</div>"
                                    + "</div>";

                table.nextAll().remove();
                table.parent().append($(strPagination));
            }
        }
    }
    return successFun;
};
DataGridJsonParse.prototype.turnPage = function (pageIndex) {
    this.param.pageIndex = pageIndex;
    this.load();
}
DataGridJsonParse.prototype.homePage = function () {
    this.turnPage(1);
}
DataGridJsonParse.prototype.previousPage = function () {
    this.turnPage(this.param.pageIndex - 1);
}
DataGridJsonParse.prototype.nextPage = function () {
    this.turnPage(this.param.pageIndex + 1);
}
DataGridJsonParse.prototype.endPage = function () {
    this.turnPage(this.totalPage);
}
//GetByModelId-------------------------------------------------------------------------------
function GetByModelId(ModelId, models, IdName) {
    for (var i = 0; i < models.length; i++) {
        if (models[i][IdName] == ModelId)
            return models[i];
    }
    return null;
}
//工具-------------------------------------------------------------------------------
function isNotANumber(inputData) {
    //isNaN(inputData)不能判断空串或一个空格
    //如果是一个空串或是一个空格,而isNaN是做为数字0进行处理的,而parseInt与parseFloat是返回一个错误消息,这个isNaN检查不严密而导致的。
    if (parseFloat(inputData).toString() == "NaN") {
        //alert("请输入数字……");注掉,放到调用时,由调用者弹出提示。
        return false;
    }
    else {
        //(parseFloat(inputData).toString() == "NaN"),这种方法不能判断“4e”、“3f”等形式的错误,
        //所以需加上isNaN
        if (isNaN(inputData)) {
            return false;
        }
        else {
            if (inputData == inputData.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')) {
                //验证 小数位数 是否没有超过两位
                return true;
            }
            else {
                return false;
            }
        }
    }
}

 

posted @ 2019-05-06 15:53  己为  阅读(3962)  评论(0编辑  收藏  举报