Majid,

>I use Autocomplete plugin (1.0 Alpha) mostly for form inputs bound to
>foreign key columns. For instance I have a user_position table with
>two columns: user_id and position_id. On new appointment form I have
>two autocomplete text inputs with the following code:
>
>   <input type="text" id="user_id" class="ac_input"  tabindex="1" />
>   <input type="text" id="position_id" class="ac_input" tabindex="2" /
>>
>
>As you can see the inputs do not have a name attribute, and when the
>form is submitted their values are not sent, which is all right since
>they will contain strings like:
>
>   'John Doe'
>   'Sales Manager'
>
>whereas our backend expects something like:
>
>   23
>   14
>
>which are the user_id for John Doe and position_id for Sales Manager.
>To send these values I have two hidden inputs in the form like this:
>
>   <input type="hidden" name="user_id" value="">
>   <input type="hidden" name="position_id" value="">
>
>Also I have the following code in the $().ready function:
>
>   $("#user_id").result(function(event, data, formatted) {
>     $("[EMAIL PROTECTED]").val(data[1]);
>   });
>   $("#position_id").result(function(event, data, formatted) {
>     $("[EMAIL PROTECTED]").val(data[1]);
>   });

This function was introduced just for what you needed to do. It's also
pretty simple to implement.

The reason why making the developer code this simple functionality was
because only the developer knows how the backend data is being returned. It
takes some inherent knowledge of how the server's returning data.

Plus, using this method allows you do perform other functionality based upon
the results.

While you could mod the autocomplete code to do what you want, I generally
recommend against modding plug-ins, unless you want to get into supporting
your code branch. Once you mod the plug-in, it becomes more difficult to get
community assistance--not to mention when the plug-in is updated, it can
become a nightmare to update to the new code.

If writing the extra 3 lines of code for each autocomplete is just making
your life too unbearable, then I would recommend sticking with wrapper code.
That way you can write the code the way you want, but you still can update
the autocomplete plug-in code very easily.

With all that said, if you're still set on modding the plug-in to add this
functionality, you should be able to do this by adding a new option.

In the jQuery.Autocompleter.defaults add a new option called something like
"autoBindField" and make it an empty string.

Then in the jQuery.fn.extend() code at the top find the "autocomplete" key
and change the code:

return this.each(function() {
        new jQuery.Autocompleter(this, options);
});

To something like:

return this.each(function() {
        var a = new jQuery.Autocompleter(this, options);
        if( options.autoBindField.length > 0 ){
                a.result(function(e, d){
                        $(options.autoBindField).val(d[1]);
                });
        }
});

I didn't test this code, but I believe it will work as you expected.
Obviously this expects the autoBindField to be the 2nd item in the array for
that row.

-Dan

Reply via email to