Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #5978 (reopened enhancement)

Opened 2 years ago

Last modified 8 months ago

multiple parameters for Ajax.Autocompleter

Reported by: anonymous Assigned to: thomas@fesch.at
Priority: high Milestone:
Component: script.aculo.us Version:
Severity: major Keywords:
Cc:

Description

Allow to pass multiple fields as parameters for Ajax.Autocompleter instead of only one paramName parameter. It can be an array of field names which is passed as parameter instead of only one field name. It could be useful for forms in which the autocomplete field depends of many other fields.

Change History

03/18/07 03:57:33 changed by dokurobei

  • status changed from new to closed.
  • resolution set to fixed.

Here is my version of Ajax.Autocomplete based on release 1.6.1. Added lines are between /**(...)**/.

Ajax.Autocompleter = Class.create();
Object.extend(Object.extend(Ajax.Autocompleter.prototype, Autocompleter.Base.prototype), {
  initialize: function(element, update, url, options) {
    this.baseInitialize(element, update, options);
    this.options.asynchronous  = true;
    this.options.onComplete    = this.onComplete.bind(this);
    this.options.defaultParams = this.options.parameters || null;

/***************************************/
    this.options.autoParams    = this.options.autoParams || null;
/***************************************/

    this.url                   = url;
  },

  getUpdatedChoices: function() {
    entry = encodeURIComponent(this.options.paramName) + '=' + 
      encodeURIComponent(this.getToken());

    this.options.parameters = this.options.callback ?
      this.options.callback(this.element, entry) : entry;

    if(this.options.defaultParams) 
      this.options.parameters += '&' + this.options.defaultParams;

/***************************************/
    if(this.options.autoParams) {
      autoParams = '';
      this.options.autoParams.each(function(item) {
	if((item = $(item)) && (serialized = Form.Element.serialize(item)))
	  autoParams += '&' + serialized;
      });
      this.options.parameters += autoParams;
    }
/***************************************/

    new Ajax.Request(this.url, this.options);
  },

  onComplete: function(request) {
    this.updateChoices(request.responseText);
  }

});

New Ajax.Autocompleter takes another parameter in options - autoParams. It is an array of form elements that should be included in the query. Names of the parameters for the AJAX request are taken form the name attribute of the form element and not from the id. If the name is not set, than that element is not included in the query.

New Ajax.Autocompleter should be created like this:

<div id="autocompleter-div"></div>
<input id="autocompleted-element"/>
<input id="other-element1" name="other1"/>
<input id="other-element2" name="other2"/>

<script>
    new Ajax.Autocompleter('autocompleted-element', 'autocompleter-div', '/autocomplete.php', { paramName: 'q', autoParams: ['other-element1', 'other-element2'] });
</script>


(in reply to: ↑ description ) 02/05/08 21:16:26 changed by cziaul

  • status changed from closed to reopened.
  • resolution deleted.

Replying to anonymous:

Allow to pass multiple fields as parameters for Ajax.Autocompleter instead of only one paramName parameter. It can be an array of field names which is passed as parameter instead of only one field name. It could be useful for forms in which the autocomplete field depends of many other fields.

Hi there

Here you showed how to send mutiple fields as parameters for Ajax.Autocompleter instead of only one paramName parameter. But how to send combination of textfield and combo box/dropdown list?

The solution you given for text field not for combo or dropdown list. Please help.