Hopefully I can shed some light on these questions.
The original AutoComplete would only return the values displayed to the
user to your controller. So if you were searching on names, it would
return names. The "hidden" field is used to return another value, say a
contact id instead of a name.
Here is an example that sets up the AutoComplete. (My project name is
fatboy if you are wondering what that is)
I like to wrap it in my own widget:
class MySpecificACF(widgets.CompoundFormField):
template = """
<div xmlns:py="http://purl.org/kid/ns#" >
<script language="javascript" type="text/javascript">
// This is where you can setup extra AJAX stuff around
the ACF (not
typically needed)
addLoadEvent(new
MySpecificACFManager('${name}').initialize);
</script>
${theACF.display(value_for(theACF), **params_for(theACF))}
</div>
"""
# Your extra Javascript, in this case my extra JS manager
javascript = [fatboy.widgets.JSLocal("myspecificacfmanager.js")]
params = ['name', 'sourceList']
theACF= widgets.AutoCompleteField(name="theACF",
search_controller="mySpecifcControllerFunc")
# Important to bring in the needed JavaScript and CSS for the ACF
widget
member_widgets = ['theACF']
Here is my two controller method:
@turbogears.expose(format="json")
def mySpecifcControllerFunc(self, obj=None, searchString=None):
searchString = searchString.lower()
listOfItemsForTheACF = someModelGetterFunc(search=searchString)
textItems = []
for i in listOfItemsForTheACF:
# Notice the tuple (value to be displayed, value for
controller)
# This is what will show up when the ACF is used
textItems.append((i.forUserToSee, i.forController))
return dict(textItems=textItems)
This controller is where your form gets sent to update the data from
the submitted form
@turbogears.expose()
def update(self, obj, **data):
# This is where the 'hidden' part comes in
if data['MyACFWidgetName']['theACF']['hidden']:
theID =
Contact.get(int(data['MyACFWidgetName']['theACF']['hidden']))
obj.theIDToUpdate = theID
else:
obj.referrer = None
Now, Ian you wanted to do some extra AJAX magic when they used the ACF,
here is how I've done it and it works well across browsers, and doesn't
break the widget. If you notice in the widget code, I had a
MySpecificACFManager defined in myspecificacfmanager.js. That code
looks like this (the basics to build off of at least):
AutoCompleteListManager = function(name) {
this.name = name;
// Allows quick access to the main fields you want
this.autoCompHiddenField;
this.autoCompTextField;
this.oldKeyPress;
bindMethods(this);
};
AutoCompleteListManager.prototype.initialize = function() {
this.autoCompHiddenField = getElement("form_"+ this.name +
"_innerAutoComp_hidden");
this.autoCompTextField = getElement("form_"+ this.name +
"_innerAutoComp_text");
// Hijack the keypress function of the interal autoComplete field
// The keypress is where the autocomplete activation happens
this.oldKeyPress = eval("AutoCompleteManagerform_" + this.name +
"_innerAutoComp.theKeyPress");
eval("AutoCompleteManagerform_" + this.name +
"_innerAutoComp.theKeyPress = this.newKeyPress");
updateNodeAttributes(this.autoCompTextField, {"onkeydown":
this.newKeyPress });
}
AutoCompleteListManager.prototype.newKeyPress = function(event) {
// Deal with crappy browser implementations
event=event||window.event;
var key=event.keyCode||event.which;
var origReturn = this.oldKeyPress(event);
// ADD IN YOUR OWN CODE HERE
// Never sumbit on an enter key press
if (key == 13) return false;
else return origReturn;
}
Hope that helps
-Owen <><
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---