验证日期时如何在jqgrid中使用monthNames? [英] how to use monthNames in jqgrid when validating date?

查看:67
本文介绍了验证日期时如何在jqgrid中使用monthNames?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的jqgrid中,当我单击添加新记录时,我的日期字段预填充了当前日期.日期格式为yyyy-MMM-d(例如2010年1月23日). 日期为必填字段,当我单击提交"按钮时,它无法通过验证并显示错误消息,指出该日期无效,并且需要Y-m-d格式.

In my jqgrid when i am clicking on add new record i have date field prepopulated with current date. Format of the date is yyyy-MMM-d (e.g. 2010-Jan-23). Date is required field and when i click submit button it fails validation and displays error that this date is invalid, and it wants Y-m-d format.

我如何用jqgrid检查我的价值?换句话说,在验证2010年1月23日时,如何使jqgrid接受以下日期格式?

How can i check my value with jqgrid? In other words how to make jqgrid accept the following date format when validating 2010-Jan-23?

谢谢.

这是我的JSON数据:

Here is my JSON data:

{"rows":[

{"id":1,"cell":["2010-Mar-3","Pepto","2","False","not active"]},
{"id":2,"cell":["2009-May-6","Tylenol","5","False","not active"]},
{"id":3,"cell":["2008-May-6","Aspirin","9","True","active"]}

]}},

这是我的日期列定义:

{ name: 'Date', 
index: 'date', 
width: '80px', 
align: 'center', 
sortable: false,
editable: true, 
datefmt: 'Y-M-d', 
editoptions: { dataInit: function(elem) { $(elem).datepicker({ dateFormat: 'yy-M-d' });},value: getDate } ,
editrules: { required: true, date:true} },

getdate函数将当前日期插入字段.这是函数:

The getdate function inserts current date into field. here is the function:

 function getDate(){
        var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
        var now = new Date();
        return now.getFullYear() + "-" + monthNames[now.getMonth()] + "-" + now.getDate();
    }

也许是因为这个功能?我可以从日期选择器中插入当前日期吗?

Maybe this is because of this function? Can i insert current date from datepicker?

从服务器发送的数据量不太大(大约10到30行),此应用程序最多可使用50个人,因此不必担心数据量.

Amount of data sent from the server is not too big (about 10-30 rows) and this application will be used by maximum of 50 people, so there is no concerns regarding amounts of data.

任何时候当我在字段中将值设置为2010年6月23日时,都会收到错误消息:输入有效的日期值-Y-M-d

Anytime when i have value as 2010-Jun-23 in the field, i get error message:Enter valid date value - Y-M-d

推荐答案

验证您在colModel的列定义中定义了datefmt: 'Y-M-d'.在editrules选项列表中(请参阅 http://www.trirand.com/jqgridwiki/doku.php?id=wiki:common_rules#editrules )定义为,如果您将date: true用作editrules选项,则将执行与选项.

Verify that you defined datefmt: 'Y-M-d' in the column definition of colModel. In the list of editrules options (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:common_rules#editrules) is defined that if you use date: true as a editrules option, the validation will be done corresponds with the datefmt option.

如果在jqGrid中使用日期,则还有更多建议:

Some more recommendations if you use date in jqGrid:

如果尚未使用,请考虑使用jQuery.datepicker(请参见 http ://jqueryui.com/demos/datepicker/#date-formats )位于editoptionsdataInit函数(如

If you not yet use, think about of the usage of jQuery.datepicker (see http://jqueryui.com/demos/datepicker/#date-formats) inside of dataInit function of editoptions (like

editoptions: {
    dataInit : function (elem) {
        $(elem).datepicker();
    }
    // ...
}

在最简单的情况下).如果您使用搜索日期字段,则可以以相同的方式在searchoptions中将dataInitjQuery.datepicker一起使用(请参阅

in the simplest case). If you use searching for date fields, you can use dataInit with jQuery.datepicker also in searchoptions in the same way (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:singe_searching#colmodel_options). The usage of jQuery.datepicker in jqGrid is very easy, but it makes your program more comfortable.

使用标准日期格式器(formatter: 'date')有助于将发送到jqGrid的源数据格式转换为将要显示的新格式.例如,

Usage of standard date formatter (formatter: 'date') can be useful to convert source data format send to jqGrid to the new format which will be displayed. For example,

formatter: 'date', formatoptions: {srcformat:'y-m-d', newformat: 'Y-M-d' }

这不太有趣,但是可以减少从服务器发送到客户端的数据量.

It is less interesting, but it can reduce a little the size of data send from server to client.

已更新:我必须承认,我没有使用恰好是您的数据格式来测试我的建议.刚才我又测试了一次,然后仔细阅读了有关datefmt的jqGrid文档(请参阅

UPDATED: I must admit, I don't tested my suggestion with exactly your data format. Just now I tested all one more time and then read carefully jqGrid documentation about datefmt (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:colmodel_options). For datefmt are only numerical format for months is supported! So the value at the time of date verification must be in a numerical format. What can we do? For example following

我们将定义为navGrid函数添加"参数的参数(请参阅

we define as parameters of navGrid function "add" parameters (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:navigator parameter prmAdd) like following:

{ beforeCheckValues: function(posdata, formid, mode) {
      var data = posdata.Date;
      var dateParts = data.split("-");
      var mounth = dateParts[1];
      var mounths = $.jgrid.formatter.date.monthNames;
      var iMounth = -1;
      for (var i=0; i<12; i++) {
          if (mounth === mounths[i]) {
              iMounth = i;
              break;
          }
      }
      if (iMounth !== -1) {
          posdata.Date = dateParts[0]+'-'+(iMounth+1)+'-'+dateParts[2];
      }
  },
  beforeSubmit: function(postdata, formid) { 
      var data = postdata.Date;
      var dateParts = data.split("-");
      var mounths = $.jgrid.formatter.date.monthNames;
      postdata.Date = dateParts[0]+'-'+
                      $.jgrid.formatter.date.monthNames[dateParts[1]-1]+
                      '-'+dateParts[2];
      return [true,""];
  }
}

因此,我们将Date字段转换为beforeCheckValues函数内部的数字格式,然后在使用checkDate后将其全部转换回原始格式(如2010年1月23日).会起作用的.

So we convert the Date field to the numerical format inside of beforeCheckValues function and then convert all back to the original format (like 2010-Jan-23) after usage of checkDate. It will work.

现在的主要问题是:我们从这种日期检查中获得一些优势吗?我们可以不使用 editrules: { date:true },并在beforeSubmit函数中实现我们自己的日期检查.如果我们发现错误的日期,我们应该返回[false,"My error message"],并且jqGrid将显示我们的自定义错误消息我的错误消息".

The main question is now: have we some advantage from such kind of the date checking? We can just don't use editrules: { date:true } and implement our own date checking inside of beforeSubmit function. If we find out the wrong date we should return [false,"My error message"] and our custom error message "My error message" will be displayed by jqGrid.

因此,如果我们可以将日期格式更改为任何数字形式(Y-mm-d,mm/d/Y或另一种格式),那么我们将为您提供最简单的解决方案. datepicker的用法对于用户而言,格式问题不是很重要.考虑一下,然后选择您喜欢的方式.

So the easiest solution of your problem we receive if we could change the date format to any numerical form (Y-mm-d, mm/d/Y or another one). Usage of datepicker will makes for users the format problem not very important. Think about this and choose the way which your prefer.

这篇关于验证日期时如何在jqgrid中使用monthNames?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆