twitter / typeahead.js

typeahead.js is a fast and fully-featured autocomplete library
http://twitter.github.io/typeahead.js/
MIT License
16.52k stars 3.21k forks source link

support for comma (or other) delineated fields #751

Closed timprepscius closed 10 years ago

timprepscius commented 10 years ago

I'm using this hack I came up with, but perhaps typeahead would like to do this correctly.

    fixTypeAheadToWorkWithCommas: function (input) {
        var z = $.data(input, 'ttTypeahead');
        z.input.getInputValue = function() {
            var s = z.input.$input.val();
            var c = s.lastIndexOf(',');
            return s.substr(c+1);
        };
        z.input.setInputValue = function(value, silent) {
            var s = z.input.$input.val();
            var c = s.lastIndexOf(',');
            var f = s.substr(0, c+1) + " " + value;
            z.input.$input.val(f);
        };

        z.input.setHintValue = function(val) {
            var s = z.input.$input.val();
            var c = s.lastIndexOf(',');
            var f = s.substr(0, c+1) + val;
            z.input.$hint.val(f);
        };

        z.input.clearHint = function() {
            z.input.setHintValue("");
        };

        z.input.resetInputValue = function () {
            z.input.setInputValue(z.input.query);
        }
timprepscius commented 10 years ago
    },
jharding commented 10 years ago

Could you give me some context? What problem does this solve?

timprepscius commented 10 years ago

Input: XXX, YYY, ZZZ, VVV works.

Example:

To:_____

And you start typing: Tim

Typeahead will correctly suggest: TimSalaBim

But then you type a tab, comma, I.

To: TimSalaBim, I

Typeahead will be confused. Whereas I want it to suggest IAmAComputer

So that when you press tab:

To: TimSalaBim, IAmAComputer

Here is some revised code:

There is a still a bug when the typed and the hint are greater length than the input box. (I think one of them has no scroll or something)

    fixTypeAheadToWorkWithCommas: function (input) {
        var z = $.data(input, 'ttTypeahead');
        z.input.getInputValue = function() {
            var s = z.input.$input.val();
            var c = s.lastIndexOf(',');
            return s.substr(c+1);
        };
        z.input.setInputValue = function(value_, silent) {
            var value = value_.trim();
            var s = z.input.$input.val();
            var c = s.lastIndexOf(',');
            var f = (c >= 0) ? (s.substr(0, c+1) + " " + value) : value;
            z.input.$input.val(f);
        };

        z.input.setHintValue = function(value_) {
            var value = value_.trim();
            var s = z.input.$input.val();
            var c = s.lastIndexOf(',');
            var f = (c >= 0) ? (s.substr(0, c+1) + " " + value) : value;
            z.input.$hint.val(f);
        };

        z.input.clearHint = function() {
            z.input.setHintValue("");
        };

        // this causes things to lose their multi-value during onBlur
        z.input.resetInputValue = function () {
        //z.input.setInputValue(z.input.query);
        }
    },
jharding commented 10 years ago

So this doesn't fall within the scope of what I want to do with the existing typeahead.js jQuery plugin. In the future, I want to create additional plugins that will support this sort of functionality, but there's no timeframe for that.

eduo commented 10 years ago

In the meantime the tokenfield project from sliptree uses bootstrap and typahead to provide most of this functionality.

serv commented 10 years ago

@timprepscius Were you able to find out a good way to hack this? I am in a situation where I would like your scenario to work.

serv commented 10 years ago

@eduo :+1: looks great! Checking out tokenfield

depiction commented 8 years ago

Please add support for setting special characters to accept, like commas. I'm not using bootstrap so the tokenfield project won't work.

timprepscius commented 8 years ago

You might try looking at my project:

https://github.com/timprepscius/mv

I believe I made it tokenize without using tokenfield.

Perhaps look in: https://github.com/timprepscius/mv/tree/master/client/web/Ext/sliptree-bootstrap-tokenfield-9e935ef

https://github.com/timprepscius/mv/blob/master/client/web/WebContent/templates/composeTemplate.html

https://github.com/timprepscius/mv/blob/master/client/web/WebContent/js/view/ComposeView.js

Not sure if this will help.

-tim

On 3/7/16, depiction notifications@github.com wrote:

Please add support for setting special characters to accept, like commas. I'm not using bootstrap so the tokenfield project won't work.


Reply to this email directly or view it on GitHub: https://github.com/twitter/typeahead.js/issues/751#issuecomment-193406156

timprepscius commented 8 years ago

Oh, I see, not bootstrap. Sorry. Brain malfunction.

-tim

On 3/7/16, Tim Prepscius timprepscius@gmail.com wrote:

You might try looking at my project:

https://github.com/timprepscius/mv

I believe I made it tokenize without using tokenfield.

Perhaps look in: https://github.com/timprepscius/mv/tree/master/client/web/Ext/sliptree-bootstrap-tokenfield-9e935ef

https://github.com/timprepscius/mv/blob/master/client/web/WebContent/templates/composeTemplate.html

https://github.com/timprepscius/mv/blob/master/client/web/WebContent/js/view/ComposeView.js

Not sure if this will help.

-tim

On 3/7/16, depiction notifications@github.com wrote:

Please add support for setting special characters to accept, like commas. I'm not using bootstrap so the tokenfield project won't work.


Reply to this email directly or view it on GitHub: https://github.com/twitter/typeahead.js/issues/751#issuecomment-193406156