views:

100

answers:

2

I have an jQuery.autocomplete field presenting a limited range of decimal numbers. Ex:

var the_list = ['+2.23', '+1.10', '-1.10', '-2.00', '-3.00',]

But I allow the user to enter data like '2.3' and '-5'. At least I can get partial matches to help me when i start typing for these values.

But what if I (or someone else with clumsy fingers) enter '2,3' (with a comma instead of a decimal point). As soon as I hit the comma I will get no matches. Can this be fixed in some way?

My thought was to create a handler on keydown-event witch replaces the comma with a decimal point before the autocomplet-handler gets started. I have tried to add an eventhandler to the field, but then the autocompleter stops working all together, and there seems to be no way to specify the order of execution of the handlers either.

Please inform me how this could be done, if at all possible. Any suggestions welcome.

+2  A: 

Assuming that you're using the autocomplete plugin for jquery found here: http://www.pengoworks.com/workshop/jquery/autocomplete.htm

Then modify the plugin file to change this:

    function onChange() {
 // ignore if the following keys are pressed: [del] [shift] [capslock]
 if( lastKeyPressCode == 46 || (lastKeyPressCode > 8 && lastKeyPressCode < 32) ) return $results.hide();
 var v = $input.val();
 if (v == prev) return;
 prev = v;
 if (v.length >= options.minChars) {
  $input.addClass(options.loadingClass);
  requestData(v);
 } else {
  $input.removeClass(options.loadingClass);
  $results.hide();
 }
};

to this:

    function onChange() {
 // ignore if the following keys are pressed: [del] [shift] [capslock]
 if( lastKeyPressCode == 46 || (lastKeyPressCode > 8 && lastKeyPressCode < 32) ) return $results.hide();
 var v = $input.val();
            v = fixBadChars(v);
 if (v == prev) return;
 prev = v;
 if (v.length >= options.minChars) {
  $input.addClass(options.loadingClass);
  requestData(v);
 } else {
  $input.removeClass(options.loadingClass);
  $results.hide();
 }
};
    function fixBadChars(v) {
         // Customize this to your tastes
         return v.replace(',','.');
    };

I just took a quick look at the plugin, but I'm guessing/hoping this might work...

// Start Edit The differences between the two are:

    v = fixBadChars(v);

    function fixBadChars(v) {
         // Customize this to your tastes
         return v.replace(',','.');
    };

// End Edit

Here is a link to the plugin directly: Ref: http://www.pengoworks.com/workshop/jquery/lib/jquery.autocomplete.js

DismissedAsDrone
Exellent suggestion. But I'm using the a rewrite of the module you suggests: http://docs.jquery.com/Plugins/Autocomplete .I think it would be possible to patch it with a new option so it could be used like this:$("#theThing").autocomplete(the_list, { minChars: 0, selectFirst: false, matchContains: true, // And the new option fixBadChars: function(searchTerm) { return searchTerm.replace(',','.'); },});Anyone that could help me with that?
UlfR
You should be able to add something like filter : function(raw) { return raw.replace(',','.'); }, to the $Autocompleter.defaults object and then in the onChange function, after the "var currentValue = $input.val();", add the line "currentValue = options.filter(currentValue);"This should run the comma replacement by default, but will give you the ability to change the function by passing an anonymous function to the filter variable in your options object.
DismissedAsDrone
A: 

Many thanks to DismissedAsDrone by which help I managed to get my stuff working. I made a slight modification to the suggested solution though since I wanted to update the actual value of the inputbox, not just the value used in the search.

Here is what I have done

I started out with this autocomplete-plugin (v1.0.2) of jQuery.

I added a preFilter option (1 line of code) to the $.Autocompleter.defaults object code as:

$.Autocompleter.defaults = {
    inputClass: "ac_input",
    ...,
    preFilter: function(value) { return value; },
    ...

In the onChange function I added the usage of preFilter (4 lines of code) after the var currentValue ... row:

var currentValue = $input.val();

if ( options.preFilter(currentValue) != currentValue ) {
    $input.val(options.preFilter(currentValue));
    return;
}

HowTo use it

To make use of the new option to solve my specific problem I used it like this:

$(function() {
    $("#the_inputfield").autocomplete(the_data, {
        minChars: 0,
        max: 100,
        selectFirst: false,
        matchContains: true,
        preFilter: function(value) { return value.replace(',','.'); },
    });
});
UlfR
I will be so bold as to accept my own answer here, hope no one disagree...
UlfR