jQuery-Autocomplete icon indicating copy to clipboard operation
jQuery-Autocomplete copied to clipboard

How to use custom lookup function to match the beginning of one object value and anywhere in another?

Open oshihirii opened this issue 8 years ago • 0 comments

I am trying to emulate this jQueryUI autocomplete behaviour (jsFiddle) where the lookup match is performed at the beginning of one object value and anywhere is another object value.

I have managed to emulate this using the pixabay autocomplete plugin (codepen) where the source option value is a function.

Edit: I think I have figured it out! See demo here.

$("#autocomplete-complex").autocomplete({
lookup: function(query, done) {
    // query is what you type
    // done is what is returned to the user

    // BEGIN hasMatch() function
    function hasMatch(key_value, match_type) {
      // BEGIN if match_type === "start"
      if (match_type === "start") {
        // check if query is at the 0 index of object's 'value' string
        var is_at_start_of_string =
          key_value.toLowerCase().indexOf(query.toLowerCase()) === 0;
        if (is_at_start_of_string === true) {
          console.log("query is at start of object's 'value' string");
        }
        return is_at_start_of_string;
      } else if (match_type === "anywhere") {
        // END if match_type === "start"

        // BEGIN if match_type === "anywhere"
        // check if query is at any index of object's 'product_desc' string
        var exists_in_string =
          key_value.toLowerCase().indexOf(query.toLowerCase()) !== -1;
        if (exists_in_string === true) {
          console.log("query exists in product_desc string");
        }
        return exists_in_string;
      }
      // END if match_type === "anywhere"
    }
    // END hasMatch() function

    // declare variables
    // the iterator
    var i;
    // a result object
    var obj;
    // the array containing result objects
    var matches = [];

    // BEGIN if query is empty string
    // return an empty array
    if (query === "") {
      console.log("this thing is happening");
      var result = {}; 
      result["suggestions"] = [];
      done(result);
      return; 
    }
    // END if query is empty string
    // return an empty array

    // get length of array_of_objects
    var products_array_length = products_array.length;

    // for each object in the array, call the hasMatch() function and pass it the object's 'value' and 'product_desc' strings
    for (i = 0; i < products_array_length; i++) {
      obj = products_array[i];
      // if either of the below conditions return true,
      // push the object to the matches array
      if (
        hasMatch(obj.value, "start") ||
        hasMatch(obj.data.product_desc, "anywhere")
      ) {
        matches.push(obj);
      }
    }
    var result = {}; 
    result["suggestions"] = matches;
    done(result);
  }

oshihirii avatar Sep 02 '17 07:09 oshihirii