Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey Guys, I am trying to use the jquery autocomplete functionality, I' trying to get the dropdown to be displayed anytime the user presses % in the text box.

HTML
<div class="form-group">
    <label>Languages</label>
    <input class="form-control autocomplete" id="Test" />
</div>


JavaScript
var availableTags = [
     "ActionScript",
     "AppleScript",
     "Asp",
     "BASIC",
     "C",
     "C++",
     "Clojure",
     "COBOL",
     "ColdFusion"
];
function split(val) {
    return val.split(/,\s*/);
}
function extractLast(term) {
    return split(term).pop();
}

$('#Test').keyup(function (e) {

    var c = String.fromCharCode(e.keyCode);

    if (e.shiftKey == true && c == "5") {
        $(".autocomplete").bind("keydown", function (event) {
            if (event.keyCode === $.ui.keyCode.TAB &&
                $(this).autocomplete("instance").menu.active) {
                event.preventDefault();
            }
        }).autocomplete({
            minLength: 0,
            source: function (request, response) {
                // delegate back to autocomplete, but extract the last term
                response($.ui.autocomplete.filter(
                  availableTags, extractLast(request.term)));
            },
            focus: function () {
                // prevent value inserted on focus
                return false;
            },
            select: function (event, ui) {
                var terms = split(this.value);
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push(ui.item.value);
                // add placeholder to get the comma-and-space at the end
                terms.push("");
                this.value = terms.join(", ");
                return false;
            }
        });
    }
});





Any ideas are very much appreciated. Thanks guys....
Posted

1 solution

Hey I found a solution to this so I am just posting the code below in case it helps anyone else.

JavaScript
var lastKey = -1;
var performSearch = true; //added variable to determine if I should search

$("#Test").autocomplete({
    minLength: 0,
    source: function (request, response) {
        if (performSearch) {
            if (lastKey >= 0) {
                response($.ui.autocomplete.filter(
                availableTags, extractLast(request.term.substring(lastKey + 1))));
            }
        }
    },
    focus: function () {
        return false;
    },
    select: function (event, ui) {
        var terms = split(this.value);
        terms.pop();
        terms.push(ui.item.value + "%");
        terms.push("");
        this.value = this.value.substr(0, lastKey + 1);
        this.value += terms.join("");
        return false;
    }
}).on("keypress", function (e) {
    var keys = [];
    keys.unshift(e.which);
    if (String.fromCharCode(keys[0]) == "%") {
        lastKey = $("#Test").val().length;
    }
}).on("keydown", function (event) {
    if (event.keyCode == 8 || event.keyCode == 32) {
        performSearch = false; //do not perform the search
    } else {
        performSearch = true; //perform the search
    }
});

function split(val) {
    return val.split(/,\s*/);
}

function extractLast(term) {
    return split(term).pop();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900