New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing `href` property on a clicked item #383

Closed
pkissman opened this Issue Oct 16, 2017 · 6 comments

Comments

Projects
None yet
3 participants
@pkissman

pkissman commented Oct 16, 2017

I have a fairly straightforward typeahead that defines an item.href with a {{display}} placeholder.
Since moving from 2.9.0 to 2.10.3, the href can no longer be found (is undefined). Even whey I put in a dummy href, such as "/beer/" it cannot be found. It seem like the same behavior I am seeing on your country v.2 demo.

Is there a change from 2.9.x that might affect the visibility of the href option?

Here is my code:
It is the same href template for all groups, so not specified at the group level.

$.typeahead({
            input: ".js-typeahead",
            minLength: 1,
            order: "asc",
            group: true,
            maxItemPerGroup: 3,
            hint: true,
            dropdownFilter: "All Types",
            href: "/resource-sharing/databases/links.php?locid={{display}}",
            template: "{{display}}",
            emptyTemplate: "no result for {{query}}",
            source: {
                public: {
                    data: data.publics
                },
                academic: {
                    data: data.academics
                },
                school: { 
                    data: data.schools
                },
                special: {
                    data: data.specials
                }
            },
            callback: {
                onClickAfter: function (node, a, item, event) {
                    event.preventDefault();
                    var str = item.href;
                    **alert(str);**
                    var re = /(^.*=).*\[(.*)\]$/i;
                    var newhref = str.replace(re, "$1$2");
                    $(location).attr('href',newhref);
                },
                onResult: function (node, query, obj, objCount) {
                    console.log(objCount)
                    var text = "";
                    if (query !== "") {
                        text = objCount + ' elements matching "' + query + '"';
                    }
                    $('.js-result-container').text(text);
                }
            },
            debug: true
        });
@pkissman

This comment has been minimized.

pkissman commented Oct 16, 2017

I think I fixed this myself. The {{display}} in these two lines no longer seem to work.

href: "/resource-sharing/databases/links.php?locid={{display}}",
template: "{{display}}",

So I created my page redirect using an href function

href: function (item) {
                // Example of item.display: Blah Public Library [blah_id]
                var re = /\[(.*)\]$/i;
                var locid = re.exec(item.display); 
                return "/resource-sharing/databases/links.php?locid="+locid[1]
            },
callback: {
                onClickAfter: function (node, a, item, event) {
                    event.preventDefault;
                    $(location).attr('href',item.href);
                },

Not sure if the old way that I did things, the template: {{display}} method, should still work or not, so I am not closing.

Thanks

@tombertrand

This comment has been minimized.

Collaborator

tombertrand commented Oct 16, 2017

hey @pkissman I'm looking on the country_v2 demo and it's still working

image

http://www.runningcoder.org/jquerytypeahead/demo/#form-country_v2

could you provide a sample of data.academics?
either you need your data to be something like["item1", "item2", "item3"] or [{display: "item1"}] if you want to be using the {{display}} key. Else reach for any{{objectKey}} inside your data

@pkissman

This comment has been minimized.

pkissman commented Oct 17, 2017

@running-coder

This comment has been minimized.

Owner

running-coder commented Oct 17, 2017

sorry for the confusion @pkissman indeed the href was missing in the newer versions. I've re-added it and a test on top so it does not happen again on the develop branch. Let me know if it works as expected.

Thanks

@running-coder running-coder added the Bug label Oct 17, 2017

@running-coder running-coder added this to the 2.10.4 milestone Oct 17, 2017

@running-coder running-coder changed the title from item href undefined in 2.10.3 to Missing `href` property on a clicked item Oct 17, 2017

running-coder added a commit that referenced this issue Oct 17, 2017

@pkissman

This comment has been minimized.

pkissman commented Oct 17, 2017

Thanks @tombertrand. I tried your develop branch and the item.href fix worked.

running-coder added a commit that referenced this issue Oct 17, 2017

Version 2.10.4
Fix
- #383 Missing `href` property on a clicked item
@tombertrand

This comment has been minimized.

Collaborator

tombertrand commented Oct 17, 2017

Merged to master as 2.10.4 release let me know if you encounter any other issue 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment