如何限制ExtJS DatePicker中的两个日期?

时间:2012-02-16 20:46:05

标签: regex extjs datepicker extjs4

我在ExtJS4中有一个DatePicker。我只想让每个月有两个日期。第15天和最后一天(30/31/28/29,取决于月/年)

我如何禁用选择器中的每一天但允许这两个日期?

2 个答案:

答案 0 :(得分:2)

请参阅disabledDates

Ext.form.field.Date配置选项

来自API文档:

  

disabledDates:String []要禁用的“日期”数组,作为字符串。   这些字符串将用于构建动态正则表达式   他们非常强大。一些例子:

// disable these exact dates:
disabledDates: ["03/08/2003", "09/16/2003"]
// disable these days for every year:
disabledDates: ["03/08", "09/16"]
// only match the beginning (useful if you are using short years):
disabledDates: ["^03/08"]
// disable every day in March 2006:
disabledDates: ["03/../2006"]
// disable every day in every March:
disabledDates: ["^03"]
     

请注意,数组中包含的日期格式应完全相同   匹配格式配置。为了支持正则表达式,if   您使用的日期格式为“。”在它,你将不得不   限制日期时逃避点。例如:[“03 \ .08 \ .03”]。

答案 1 :(得分:2)

//Get the last date of the month. If in Feb 2012, lastDate is 29.
var lastDate = Ext.Date.getDaysInMonth(new Date());
//15th 
var middleDate = 15;

//Construct regular expression
var disabledArray=[];
var today = Ext.Date.format(new Date(), 'm/d/Y');
var dateReg = /(\d{2}\/)\d{2}(\/\d{4})/;
disabledArray.push(today.replace(dateReg, '$1' + middleDate + '$2'));
disabledArray.push(today.replace(dateReg, '$1' + lastDate + '$2'));

//Something like "^(?!02/15/2012|02/29/2012).*$" including the two days allowed.
var disabledReg = '^(?!' + disabledArray.join('|') + ').*$';

//Apply the regular expression to date field
var dateField = new Ext.form.field.Date({
    format: 'm/d/Y',
    disabledDates: [disabledReg]
});