1
    Ext.define("Datetimepicker.view.Main", {
               extend: 'Ext.form.Panel',
               requires: [
                                'Ext.TitleBar',
                                'Ext.field.DatePicker',
                                 'Ext.Spacer',
                                 'Ext.Picker'
                             ],
               config: {
                            fullscreen:'true',
                            title:'DatatimePicker',
                            items: [
                               {
                                   xtype:'fieldset',
                                        items:[
                                           {
                                              xtype:'datepickerfield',
                                              label:'Birthday',
                                              picker:{
                                                 yearFrom:1980,
                                                  yearTo:2015
                                               },
                                             name:'birthday',
                                             value:new Date()
                                         },
                                          {
                                            xtype:'textfield',
                                             label:'Time',
                                             value:''
                                           //In this textfield i want to display the time picker value
                                            }
                                        }
                                    ]
                                    },
                                 {

items:[
{
xtype:'spacer'
},
{
text: 'setValue',
handler: function() {
var datePickerField = Ext.ComponentQuery.query('datepickerfield')[0];
var randomNumber = function(from, to) {
return Math.floor(Math.random() * (to - from + 1) + from);
};
datePickerField.setValue({
month: randomNumber(0, 11),
day : randomNumber(0, 28),
year : randomNumber(1980, 2015)
});
}
},
{ xtype:'spacer'}
]
}
]
}
});

通过使用上面的代码,我得到了 datepicker 的值,它成功显示在第一个文本字段中。同样,我想在另一个文本字段中显示 datepicker 的值。任何人都可以帮我做到这一点...提前谢谢

4

1 回答 1

1

您可以通过设置textfield每次picker's更改值时的值来做到这一点,因此这是一个解决方案:

 items: [
            {
                xtype: 'datepickerfield',
                label: 'Birthday',
                name: 'birthday',
                value: new Date(),
                listeners: {
                    change: function(picker, value) {
                        // This function use to prepend 0 to the month which less than October
                        function minTwoDigits(n) {
                            return (n < 10 ? '0' : '') + n;
                        }
                        var date = value.getDate(), // Get date's value
                            month = value.getMonth(); // Get month's value
                        month += 1; // Increase the number of month by 1 since the index started with 0
                        var formatMonth = minTwoDigits(month),
                            year = value.getFullYear(), // Get year's value
                            formatDate = formatMonth.concat("/",date,"/",year); // Concatenate string
                        Ext.ComponentQuery.query('#textfield')[0].setValue(formatDate); // Set the value of the textfield with itemID equal to textfield
                    }
                }
            },
            {
                xtype:'textfield',
                label:'time',
                itemId: 'textfield',
                value:''

            }
        ]

如果您有什么不明白的,请随时提问。希望能帮助到你 :)

于 2013-01-23T12:18:23.827 回答