Extjs NumberField后面加单位实现思路

  UnitText.js

  =======================

  

复制代码 代码如下:

  // 输入框textfield后面放字unitText定义:

  Ext.override(Ext.form.TextField, {

  unitText : '',

  onRender : function(ct, position) {

  Ext.form.TextField.superclass.onRender.call(this,ct, position);

  if (this.unitText != ''){

  this.unitEl =ct.createChild({

  tag: 'div',

  html: this.unitText this.unitEl.addClass('x-form-unit'); this.width =this.width

  -(this.unitText.replace(/[^\x00-\xff]/g, "xx").length * 6 +2); this.alignErrorIcon= function() {

  this.errorIcon.alignTo(this.unitEl,'tl-tr', [2, 0]); }});

  将UnitText.js放入到jsp文件中,加上css样式,如下:

  

复制代码 代码如下:

  <style>

  .x-form-unit{

  height:22px;

  line-height:22px;

  padding-left:2px;

  display:inline-block;

  display:inline; </style>

  <script type="text/javascript"src="UnitText.js"></script>

  这样就可以用了,使用方法如下:

  

复制代码 代码如下:

  var jine = new Ext.form.NumberField({

  id: 'je',

  fieldLabel: '金额',

  allowDecimals: true, // 允许小数点?

  allowNegative: false, // 允许负数?

  width: 100,

  style: 'text-align:right',

  unitText: ' 元'

  });