Home > Web Front-end > JS Tutorial > body text

Date or number formatting extension for DisplayField in Extjs_extjs

WBOY
Release: 2016-05-16 18:20:10
Original
1040 people have browsed it
When using Ext.form.FormPanel to process data, some fields need to be read-only. Of course we can use Ext.form.TextField and then set it to ReadOnly, but the display effect is not very good because there will always be an input box. So we must use Ext.form.DisplayField, but Ext.form.DisplayField does not have a format attribute, nor does it have the renderer event, such as the date field
var form = new Ext.form.FormPanel({
frame: true,
renderTo: 'form-div',
items: [{
xtype: 'displayfield',
fieldLabel: 'Date',
value: new Date()
}]
});
Then it displays a bit incorrectly

Then we can rewrite Ext.form.DisplayField to support the format attribute

Ext.override(Ext.form.DisplayField, {
  getValue: function () {
    return this.value;
  },
  setValue: function (v) {
    this.value = v;
    this.setRawValue(this.formatValue(v));
    return this;
  },
  formatValue: function (v) {
    if (this.dateFormat && Ext.isDate(v)) {
      return v.dateFormat(this.dateFormat);
    }
    if (this.numberFormat && typeof v == 'number') {
      return Ext.util.Format.number(v, this.numberFormat);
    }
    return v;
  }
});
Copy after login
We added two attributes to Ext.form.DisplayField: dateFormat and numberFormat, and then we changed the above FormPanel

var form = new Ext.form.FormPanel({

frame: true,

renderTo: 'form-div',

items: [{

xtype: 'displayfield',

fieldLabel: 'Date',

Value: new Date(),

dateFormat: 'm/d Y'

 }]

});

It should be quite perfect, hahaha

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!