使用dojo.formToJson时获取不同的日期模式 [英] Getting different date pattern when using dojo.formToJson

查看:140
本文介绍了使用dojo.formToJson时获取不同的日期模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过提供一个属性来更改 dijit / form / DateTextBox 的datePattern。

 code>< form dojoType =dijit.form.Formdata-dojo-id =calc_formid =calc_form> 

< input type =textdata-dojo-type =dijit / form / DateTextBoxdata-dojo-id =CONTRACT_DATE
id =CONTRACT_DATEname = CONTRACT_DATE
constraints ={datePattern:'MM-dd-yyyy',strict:true}/>
< / form>

即属性为 constraints ={datePattern:'MM-dd- yyyy',strict:true},我的日期模式在页面中正确显示为'01 -28-2016'。 p>

但是,当我尝试使用 dijit / form / DateTextBox 的表单的JSON > dojo.formToJson(formID),我得到一个不同的值,然后分配的模式:'2016-01-28'



为什么?有没有解决方案?

解决方案

问题在 dojo.formToJson 它返回默认日期格式
无论您在 dijit / form / DateTextBox 输入中指定格式。



所以,我建议格式化生成的jsonForm内的日期,



这里是一个解决方案:



首先导入所需的js

  // AMD加载
require([dojo / date / locale ,dojo / json],
函数(locale,JSON){
......
})

dojo / date / locale此处用于更改日期格式



dojo / json用于解析o json对象或反向(Object to String)



然后声明 formatJsonFormDates function
(参数在代码中解释,它返回一个格式化日期的新jsonForm)


可以帮助您转换所有的da如果表单中有很多日期,则$ a
将数组参数


中的输入日期的名称属性传递给



  //帮助函数
/ **
*该函数从dojo生成form formToJson(HTML from),
*,并将所有字符串日期替换为所需格式
*(YYYY-MM-dd至MM-dd-YYYY)例如:
*
* @param string jsonForm生成的jsonForm的值($)
* @param对象形式Dijit.form.Form的值
* @param数组dateFieldsNames字符串数组日期表单字段的值被格式化
* @param string datepattern所需的日期格式的值//mm-dd-YYYY
*
* @return string jsonFormObject新的返回的jsonForm的值具有所需的日期格式
* /

var formatJsonFormDates = function(jsonForm,form,dateFieldsNames,datepattern){
//如果没有字段传递给函数返回默认
if(!fieldsNames。乐ngth&& fieldsNames.length< 1)return jsonForm;

jsonFormObject = JSON.parse(jsonForm);
for(var i = 0; i< fieldsNames.length; i ++){
//检查字段是否为Date
的实例if(form.getValues()[fieldsNames [i] ] instanceof Date){
newDate = locale.format(form.getValues()[fieldsNames [i]],{datePattern:datepattern,selector:date});
jsonFormObject [fieldsNames [i]] = newDate;
}
}
返回JSON.stringify(jsonFormObject);
}

finnaly,得到你的jsonForm应用该功能后:

  var formData = dojo.formToJson(yourFormID); 
//我推荐使用dijit / registry而不是dijit

formData = formatJsonFormDates(formData,dijit.byId(yourFormID),[CONTRACT_DATE],MM-dd- YYYY);


I have changed the datePattern of dijit/form/DateTextBox by providing an attribute

<form dojoType="dijit.form.Form" data-dojo-id="calc_form" id="calc_form">

    <input type="text" data-dojo-type="dijit/form/DateTextBox" data-dojo-id="CONTRACT_DATE"
           id="CONTRACT_DATE" name="CONTRACT_DATE" 
           constraints="{datePattern:'MM-dd-yyyy', strict:true}" />
</form>

i.e the attribute is constraints="{datePattern:'MM-dd-yyyy', strict:true}" and I got the date pattern shown correctly in the page as '01-28-2016'.

But when I tried to get the JSON of the form containing the dijit/form/DateTextBox using dojo.formToJson("formID"), I am getting a different value then the assigned pattern: '2016-01-28'

Why? Is there any solution for that?

解决方案

The probleme is in the dojo.formToJson it return the default date format whatever you specify the format in the dijit/form/DateTextBox input.

So , I suggest to format the date inside the generated jsonForm ,

here is a solution :

First import the required js ,

//AMD loading
require(["dojo/date/locale","dojo/json"],
function(locale,JSON){
    ......
})

"dojo/date/locale" used here to change date pattern

"dojo/json" used to Parse o json Object or reverse (Object to String)

then declare the formatJsonFormDates function ( params are explained in the code it return a new jsonForm with formatted date)

helps you to convert all date at ones if there is many dates in the Form, by passing the name attribute of the input date in an Array parameter

//helper function
/**
 * The function get  generated 'form' from dojo.formToJson(HTML from),
 * and replaces all string Date to the desired format 
 * ("YYYY-MM-dd" to "MM-dd-YYYY" by exemple)
 *
 * @param string jsonForm Value of generated jsonForm(HTML from)
 * @param Object form Value of the Dijit.form.Form
 * @param Array dateFieldsNames string array Values of date form fields to be formatted
 * @param string datepattern Values of the wanted Dateformat // "mm-dd-YYYY"
 *
 * @return string jsonFormObject Value of new Returned jsonForm with desired date format
 */

var formatJsonFormDates  = function(jsonForm,form,dateFieldsNames,datepattern){
    //if no field passed to the function return default
    if(!fieldsNames.length && fieldsNames.length < 1 ) return jsonForm;

    jsonFormObject = JSON.parse(jsonForm);
    for(var i = 0; i<fieldsNames.length ;i++){
        //check if field is an instance of Date
        if(form.getValues()[fieldsNames[i]] instanceof Date) {
            newDate = locale.format(form.getValues()[fieldsNames[i]],{datePattern: datepattern, selector: "date"});
            jsonFormObject[fieldsNames[i]] = newDate;
        }
    }
    return JSON.stringify(jsonFormObject);
}

finnaly , after getting your jsonForm apply the function on it :

var formData = dojo.formToJson("yourFormID");
//I recomoand  to use dijit/registry  instead of dijit 

formData = formatJsonFormDates(formData,dijit.byId("yourFormID"),["CONTRACT_DATE"],"MM-dd-yyyy");

.

这篇关于使用dojo.formToJson时获取不同的日期模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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