OutpostMM
21 Nov 2008, 9:34 AM
Here's a combobox extension where you can define 2 additional config options to have it automatically replace text when it shows the value in the field after a selection is made. I had a use for this when I wanted to use characters in the list options to indent them, but did not want them to show up in the field text. For lack of a better name I just called it CustomCombo.
Ext.ux.CustomCombo = Ext.extend(Ext.form.ComboBox, {
findText: false,
replaceText: false,
initComponent:function() {
Ext.ux.CustomCombo.superclass.initComponent.call(this);
},
setValue: function(c) {
Ext.ux.CustomCombo.superclass.setValue.call(this, c);
if (this.findText !== false && this.replaceText !== false)
{
var text = this.lastSelectionText;
if (Ext.isArray(this.findText))
{
var repl;
for (var i = 0; i < this.findText.length; i++)
{
if (Ext.isArray(this.replaceText))
repl = this.replaceText[i];
else
repl = this.replaceText;
text = text.replace(this.findText[i], repl, 'gi');
}
}
else
{
text = text.replace(this.findText, this.replaceText, 'gi');
}
Ext.form.ComboBox.superclass.setValue.call(this, text);
this.lastSelectionText = text;
}
}
});
Ext.reg('customcombo', Ext.ux.CustomCombo);
Here are a few examples:
{
xtype: "customcombo",
name: "usergroup",
findText: [" ", "™"],
replaceText: ["", "(tm)"],
...
}
That will replace " " with "", and "™" with "(tm)".
{
xtype: "customcombo",
name: "usergroup",
findText: [" ", "™"],
replaceText: "",
...
}
That will replace both " " and "™" with "".
{
xtype: "customcombo",
name: "usergroup",
findText: " ",
replaceText: " ",
...
}
That will just replace " " with " ".
So with a data set like this for the store:
[
{
"val":0,
"name":"All User Groups"
},
{
"val":"27",
"name":" Australia"
},
{
"val":"39",
"name":" Capital Territory"
},
{
"val":"40",
"name":" Canberra"
}
]
The options in the dropdown list will be properly indented with the characters, but when someone makes a selection it will only display the name.
Ext.ux.CustomCombo = Ext.extend(Ext.form.ComboBox, {
findText: false,
replaceText: false,
initComponent:function() {
Ext.ux.CustomCombo.superclass.initComponent.call(this);
},
setValue: function(c) {
Ext.ux.CustomCombo.superclass.setValue.call(this, c);
if (this.findText !== false && this.replaceText !== false)
{
var text = this.lastSelectionText;
if (Ext.isArray(this.findText))
{
var repl;
for (var i = 0; i < this.findText.length; i++)
{
if (Ext.isArray(this.replaceText))
repl = this.replaceText[i];
else
repl = this.replaceText;
text = text.replace(this.findText[i], repl, 'gi');
}
}
else
{
text = text.replace(this.findText, this.replaceText, 'gi');
}
Ext.form.ComboBox.superclass.setValue.call(this, text);
this.lastSelectionText = text;
}
}
});
Ext.reg('customcombo', Ext.ux.CustomCombo);
Here are a few examples:
{
xtype: "customcombo",
name: "usergroup",
findText: [" ", "™"],
replaceText: ["", "(tm)"],
...
}
That will replace " " with "", and "™" with "(tm)".
{
xtype: "customcombo",
name: "usergroup",
findText: [" ", "™"],
replaceText: "",
...
}
That will replace both " " and "™" with "".
{
xtype: "customcombo",
name: "usergroup",
findText: " ",
replaceText: " ",
...
}
That will just replace " " with " ".
So with a data set like this for the store:
[
{
"val":0,
"name":"All User Groups"
},
{
"val":"27",
"name":" Australia"
},
{
"val":"39",
"name":" Capital Territory"
},
{
"val":"40",
"name":" Canberra"
}
]
The options in the dropdown list will be properly indented with the characters, but when someone makes a selection it will only display the name.