// Date对象 - 自定义格式化
// ReSharper disable once NativeTypePrototypeExtending
Date.prototype.format = function (format) {
if (this.getFullYear() === 0) return undefined;
var date = {
"m+": this.getMonth() + 1,//月份
"d+": this.getDate(),//日
"h+": this.getHours(),//时(24)
"i+": this.getMinutes(),//分
"s+": this.getSeconds(),//秒
"q+": Math.floor((this.getMonth() + 3) / 3),//季度;第一季度(1月、2月、三月)...
"S+": this.getMilliseconds()//毫秒
};
if (/(y+)/i.test(format)) format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in date) {
if (date.hasOwnProperty(k))
if (new RegExp("(" + k + ")").test(format))
format = format.replace(RegExp.$1,
RegExp.$1.length === 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
return format;
};
// 通用Json处理对象
var CommonJson = {
//生成随机GUID函数
NewGuid: function ()
{
var guid = "";
for (var i = 1; i <= 32; i++)
{
var n = Math.floor(Math.random() * 16.0).toString(16);
guid += n;
if ((i === 8) || (i === 12) || (i === 16) || (i === 20)) guid += "-";
}
return guid;
},
//Json 日期转 Date对象
GetDate: function (jsonDate)
{
return new Date(jsonDate == null ? '0000-01-01' : (jsonDate.indexOf("/Date") < 0 ? jsonDate:parseInt(jsonDate.replace("/Date(", "").replace(")/", ""), 10)));
},
//获取下拉框对应的值
GetOptionValue: function (value, options)
{
if (value != null)
{
for (var i = 0; i < options.length; i++)
for (var p in options[i]) if (options[i].hasOwnProperty(p)) if (p === value.toString())
return options[i][p];
}
return undefined;
},
//获取select2对应的值
GetSelect2Value: function (value)
{
try
{
return eval('(' + value + ')').Value;
} catch (e)
{
return undefined;
}
},
//获取列表文件值
GetFileValue: function (value)
{
try
{
var fileArr = eval(value);
var regex = /\w+\.\w{3,4}$/gi;
var str = "";
for (var i = 0; i < fileArr.length; i++)
{
if (i !== 0) str = str.concat("
");
str = str.concat("" +
fileArr[i].url.match(regex)[0] +
"");
}
return str;
} catch (e)
{
return undefined;
}
},
//Json 对象给对应字段赋值
SetModel: function ($form, model)
{
if (model != undefined)
{
for (var obj in model)
{
if (model.hasOwnProperty(obj) && obj !== "0")
{
var $this = $form.find("[name='" + obj + "']");//当前 Jquery 对象
if ($this.length > 0)
{
var value = model[obj];
if ($this.hasClass("date4")) //日期控件赋值
{
var dateFormat = $this.attr("date-format") == null ? "yyyy-mm-dd hh:ii" : $this.attr("date-format");
$this.val(CommonJson.GetDate(value).format(dateFormat));
}
else if ($this.hasClass("select2"))
{ //select2赋值
try
{
var text = [];
if (model[obj + "_Text"] != undefined)
text = model[obj + "_Text"].split(";");
if (value.constructor !== Array)
{
value = value.split(";");
}
if (text.length>0)
{
for (var i = 0; i < value.length; i++)
{
$this.append("