问题描述
int
我在我的PC的jqgrid中使用了这种格式设置选项,我的格式正确。
但是在另一台服务器上,我的格式为 NaN-undefined-NaN 12:NaN:NaN
如何对具有不同日期时间格式的所有服务器进行一般处理
解决方法
下面是执行日期格式自动检测的代码。为此有一些假设。
- 我们使用moment.js库
- 您应该定义服务器附带的预期日期格式。
自动检测的最大问题是格式,例如10/09/2000或更糟糕的10/09/09。为了克服这个问题,需要定义一组期望的格式。
进行演示查看代码中的注释。 如果不清楚,请问。
享受
$(document).ready(function () {
// define your expected formats from the server
var dateFormats = {
"iso_int" : "Y-m-d","short_date" : "d/m/Y","iso_date_time": "Y-m-dTH:m:s","iso_date_time_utc": "Y-m-dTH:m:sZ","my_format1" : "Y/m/d"
//define other well known formats if you want
};
// function uses moment.js to find the format
function getFormat(d){
for (var prop in dateFormats) {
if(moment(d,dateFormats[prop],true).isValid()){
return dateFormats[prop];
}
}
return null;
}
// gloabal array to store the columns with date fields
var datearr =[];
// jqgrid configuration
$("#jqGrid").jqGrid({
url: 'data.json',mtype: "GET",datatype: "json",page: 1,colModel: [
{ label: 'Order ID',name: 'OrderID',key: true,width: 75 },{
label: 'Order Date',name: 'OrderDate',width: 150,editable: true,edittype:"text",formatter : 'date',formatoptions : {
newformat: "d/m/Y"
}
},{
label: 'Customer ID',name: 'CustomerID',edittype: "select",editoptions: {
value: "ALFKI:ALFKI;ANATR:ANATR;ANTON:ANTON;AROUT:AROUT;BERGS:BERGS;BLAUS:BLAUS;BLONP:BLONP;BOLID:BOLID;BONAP:BONAP;BOTTM:BOTTM;BSBEV:BSBEV;CACTU:CACTU;CENTC:CENTC;CHOPS:CHOPS;COMMI:COMMI;CONSH:CONSH;DRACD:DRACD;DUMON:DUMON;EASTC:EASTC;ERNSH:ERNSH;FAMIA:FAMIA;FISSA:FISSA;FOLIG:FOLIG;FOLKO:FOLKO;FRANK:FRANK;FRANR:FRANR;FRANS:FRANS;FURIB:FURIB;GALED:GALED;GODOS:GODOS;GOURL:GOURL;GREAL:GREAL;GROSR:GROSR;HANAR:HANAR;HILAA:HILAA;HUNGC:HUNGC;HUNGO:HUNGO;ISLAT:ISLAT;KOENE:KOENE;LACOR:LACOR;LAMAI:LAMAI;LAUGB:LAUGB;LAZYK:LAZYK;LEHMS:LEHMS;LETSS:LETSS;LILAS:LILAS;LINOD:LINOD;LONEP:LONEP;MAGAA:MAGAA;MAISD:MAISD;MEREP:MEREP;MORGK:MORGK;NORTS:NORTS;OCEAN:OCEAN;OLDWO:OLDWO;OTTIK:OTTIK;PARIS:PARIS;PERIC:PERIC;PICCO:PICCO;PRINI:PRINI;QUEDE:QUEDE;QUEEN:QUEEN;QUICK:QUICK;RANCH:RANCH;RATTC:RATTC;REGGC:REGGC;RICAR:RICAR;RICSU:RICSU;ROMEY:ROMEY;SANTG:SANTG;SAVEA:SAVEA;SEVES:SEVES;SIMOB:SIMOB;SPECD:SPECD;SPLIR:SPLIR;SUPRD:SUPRD;THEBI:THEBI;THECR:THECR;TOMSP:TOMSP;TORTU:TORTU;TRADH:TRADH;TRAIH:TRAIH;VAFFE:VAFFE;VICTE:VICTE;VINET:VINET;WANDK:WANDK;WARTH:WARTH;WELLI:WELLI;WHITC:WHITC;WILMK:WILMK;WOLZA:WOLZA"
}
},{
label: 'Freigh',name: 'Freight',formatter: 'number',formatoptions :{decimalSeparator: ',',decimalPlaces:2,thousandsSeparator :""}
},{
label: 'Ship Name',name: 'ShipName',width: 150
//editable: true,}
],beforeProcessing : function( data,status,xhr) {
// get the columns that are dates
// and store it in global datearr
var i,cm = this.p.colModel;
if(datearr.length === 0 ) {
for( i=0; i<cm.length; i++) {
if(cm[i].formatter && cm[i].formatter === 'date') {
datearr.push({index:i,name: cm[i].name});
}
}
}
//get the first row to test the date formats
var testdata = data.rows[0],formatfound;
for(i=0; i<datearr.length;i++) {
var crd = datearr[i];
formatfound = getFormat( testdata[crd.name] );
// format found
if(formatfound !== null) {
if(cm[crd.index].formatoptions === undefined) {
cm[crd.index].formatoptions = {};
}
// set it in formatoptions
cm[crd.index].formatoptions.srcformat = formatfound;
}
}
return true;
},loadonce : true,viewrecords: true,width: 780,height: 250,rowNum: 10,pager: "#jqGridPager"
});
$("#jqGrid").navGrid("#jqGridPager",{ edit: false,add: false,del: false,search: true,refresh: true,view: true,align: "left" },{ closeAfterEdit: true,reloadAfterSubmit : false}
);
$("#chng").on('click',function(){
$("#jqGrid").jqGrid('setGridParam',{url:'data1.json',datatype:'json'}).trigger("reloadGrid");
});
});