:: Forum >>
Override getItemText() in Subclassed AW.UI.Combo
I really need to know how to override getItemText() when I subclass AW.UI.Combo. Can someone please help?
I am trying:
AW.UI.HTMLStripCombo2 = AW.UI.List.subclass();
AW.UI.HTMLStripCombo2.create = function(){
AW.UI.Combo.create.call(this);
var obj = this.prototype;
obj.getItemText = function( a, b, c ) {
var text = document.getElementByID( this.id + "-box-text" );
return text.value.replace(/<\/?[a-z][a-z0-9]*[^<>]*>/ig, "");
}
But it doesn't work. The combo box does not appear. Help!
Thanks.
Paul Tiseo
Wednesday, November 29, 2006
Paul, I guess you need to strip html tags when the item text goes into the edit box (not in getItemText method which is also used internally by the popup list to draw list items). Here is the example -
AW.UI.HTMLStripCombo = AW.UI.Combo.subclass();
AW.UI.HTMLStripCombo.create = function(){
var obj = this.prototype;
obj.onCurrentItemChanged = function(i){
var text = this.getItemText(i);
text = AW.textToValue(text);
this.setControlText(text);
this.hidePopup();
var e = this.getContent("box/text").element();
e.value = text;
e.select();
e = null;
};
};
Usage -
var data = ["Email","<b>Phone</b>","FedEX","USPS"];
var obj = new AW.UI.HTMLStripCombo;
obj.setItemText(data);
obj.setItemCount(4);
document.write(obj);
Alex (ActiveWidgets)
Thursday, November 30, 2006
Alex,
I already have something like it, but not quite in the sense that I added a regexp directly. See:
http://www.activewidgets.com/javascript.forum.17307.5/intercept-selection-in-aw-ui.html
However, what happens is that when I use the HTMLStripCombo I created, the source data array has HTML in all elements and one element is selected as default:
var versionCombo = new AW.UI.HTMLStripCombo2();
versionCombo.setId("versionCombo");
versionCombo.setItemCount(vers_val.length);
versionCombo.setItemText(vers_val);
versionCombo.setItemValue(vers_key);
versionCombo.setSelectedItems(vers_sel);
As you can see, setSelectedItems() is called and sets the combo's default value to a value in the item array. However, it obviously bypasses the HTML-stripping code in the function tied to onCurrentItemChanged. So in effect, at first the text displayed contains HTML, but after selection every item looks fine because it then goes through an overriden function that effectively strips HTML.
I need to override the core function, assuming there is one, that reads the list item and returns it. I'm assuming that there is one key getter I could override such that the array can contain HTML but the combo never displays it or returns it? And, if so, which one and how?
- PT
Paul Tiseo
Thursday, November 30, 2006
Paul,
you cannot override getItemText because the list template uses this method to get text for the list items (or your list items will not have HTML tags if you strip them inside getItemText). You need another API method, like getItemValue, which would return the text only (without tags).
Another thing - the text in the input box is updated by the onControlTextChanged event handler, and the default one (defined in AW.UI.Input) just assigns control text to the input.value property - maybe this one should be corrected.
Alex (ActiveWidgets)
Thursday, November 30, 2006
Alex,
As you suggested, I thought of creating a new API function like getItemStrippedText(). However, it does not solve the issue of non-stripping of HTML during creation of the Combo when setting it via setSelectedItems().
The latter method, or some downstream method, needs to be adjusted to remove HTML tags.
I tried subclassing AW.UI.Input and adjusting the _text and text.setAttribute("value", ...) lines, but I guess there is a comparison of between the <input>'s value and (maybe?) the source data array, such that it will always trigger the combo's onCurrentItemChanged event? That's a problem, as I use that event to trigger a page redirect, although it "works".
I guess this seems to be unworkable, huh?
- PT
Paul Tiseo
Thursday, November 30, 2006
Alex helped me out in private mail. What's missing from the previous snippet I posted is the last line now added in correct form here for future reference:
var versionCombo = new AW.UI.HTMLStripCombo2();
versionCombo.setId("versionCombo");
versionCombo.setItemCount(vers_val.length);
versionCombo.setItemText(vers_val);
versionCombo.setItemValue(vers_key);
versionCombo.setSelectedItems(vers_sel);
versionCombo.setControlText(versionCombo.getItemText(versionCombo.getSelecte
dItems()).replace(/<\/?[a-z][a-z0-9]*[^<>]*>/ig, "") );
Paul Tiseo
Tuesday, December 5, 2006
This topic is archived.
Back to support forum
Forum search