2017-09-27 120 views
3

我想更改dijit dateTextBox的日期显示格式。
目前我的日期显示的是这样如何使用Dijit DateTextBox显示和验证自定义格式?

enter image description here

我的声明是这样的

<input type="text" data-dojo-type="dijit/form/DateTextBox" data-dojo-attach-point="theInput" /> 

但我想的格式更改为我想例如2017年9月14日的任何格式,或09-14-2017。

此外,如果使用键盘输入格式,是否可以根据相同的格式进行验证 - 即,当用户键入数据时必须使用该格式或者出现无效消息。

编辑: 所有我能找到的,它允许你设置这两个输入约束和格式的限制提到 - 但也有文档中没有例子如何使用此。能够看到陈述式和程序式的例子会很好。

enter image description here

此外,根据这里的一些其他职位在声明标记,你可以指定一个像

data-dojo-props="constraints:{datePattern:'yyyy-MM'}" 

但是我想以编程方式做到这一点的限制,但是当我检查输入对象

this.theInput.constraints 

没有datePattern,min或max等属性。

回答

4

这是可能的两个编程和声明代码:

  1. 的声明例如:

只是把你的模式,最小,最大的制约道具,如:

<input type="text" name="shortYear" data-dojo-type="dijit/form/DateTextBox" data-dojo-props="constraints:{datePattern: 'MM-dd-yyyy',min:'2016-12', max:'2018-06'}" value="09-14-2017" required="true" /> 

查看工作小提琴

require(["dijit/form/DateTextBox","dojo/parser", "dijit/form/Button","dojo/on" , 
 
    "dojo/domReady!" 
 
], function(DateTextBox,parser,Button, On) { 
 
\t \t parser.parse(); 
 
});
<link href="https://ajax.googleapis.com/ajax/libs/dojo/1.10.0/dijit/themes/claro/claro.css" rel="stylesheet"/> 
 
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script> 
 
<body class="claro"> 
 
<input type="text" name="shortYear" data-dojo-type="dijit/form/DateTextBox" data-dojo-props="constraints:{datePattern: 'MM-dd-yyyy',min:'2017-09-03', max:'2018-01-01'}" value="09-14-2017" required="true" /> 
 
</body>

或者这FIddle

  • 为程序化的例子:
  • 你刚刚使用直接访问限制对象并在那里设置约束值Mydateinput.constraints.contName = value like

    myDateBox.constraints.datePattern = 'MM-dd-yyyy' 
    myDateBox.constraints.min = new Date(); 
    myDateBox.constraints.max = new Date("yyyy-MM-dd") 
    

    请参阅使用片断

    require(["dijit/form/DateTextBox", "dijit/form/Button","dojo/on" , 
     
        "dojo/domReady!" 
     
    ], function(DateTextBox,Button, On) { 
     
    \t \t datebox = new DateTextBox({ 
     
        }, "date"); 
     
        datebox.constraints.datePattern = 'MM-dd-yyyy'; 
     
        
     
        datebox.constraints.min = new Date("2017/09/03"); 
     
        datebox.constraints.max = new Date("2018/01/01") 
     
        
     
        datebox.startup(); 
     
    });
    <link href="https://ajax.googleapis.com/ajax/libs/dojo/1.10.0/dijit/themes/claro/claro.css" rel="stylesheet"/> 
     
    <script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script> 
     
    
     
    <body class="claro"> 
     
        <div id="date" ></div> 
     
    </body>

    还是Fiddle

    相关问题