Skip to content
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

correlativeTemplate: true not working as expected #497

Closed
chris-shorter opened this issue Jan 23, 2020 · 6 comments
Closed

correlativeTemplate: true not working as expected #497

chris-shorter opened this issue Jan 23, 2020 · 6 comments
Labels

Comments

@chris-shorter
Copy link

chris-shorter commented Jan 23, 2020

Hello, I hope you can help, I'm having issues with correlativeTemplate: true with ajax and a template function.

If I type a word from tag1 and a word from tag2 (on the same result) I get no results. - eg for below, search "lorem dolor"

Also is it possible to have tags just be an array of any number of tags? (at first I had it this way, and I thought that was why it didn't work, so I split it into tag1, tag2 etc)

Here is my code.

$.typeahead({
        input: '#keyword-search',
        hint: true,
        highlight: false,
        cache: true,
        emptyTemplate: function (query) {
            if (query.length > 0) {
                return 'No results found for "' + query + '"';
            }
        },
        correlativeTemplate: true,
        maxItem: 0,
        source: {
            Products: {
                display: ["search_tags","subtitle","tag0","tag1","tag2"],
                template: function (query, item) {
                    var template =
                        '<span class="title">';
                        if(item.is_rental) {
                            template += '<span>Rental: </span>';
                        }
                        template += '{{title}}</span>' +
                        '<span class="subtitle">{{subtitle}}</span>';
                    return template;
                },
                href: '{{url}}',
                ajax: {
                    url: "/site/lib/return-products-json.php",
                }
            }
        }
    });

Here's some sample data from return-products-json.php

[
{
"title": "PRODUCT1",
"url": "/product1/",
"sku": "1003",
"subtitle": "Subtitle Example 1",
"is_rental": 0,
"tag0": ""
},
{
"title": "PRODUCT2",
"url": "/product2/",
"sku": "1002",
"subtitle": "Subtitle Example 2",
"is_rental": 0,
"tag0": ""
},
{
"title": "PRODUCT3",
"url": "/product3/",
"sku": "1001",
"subtitle": "Subtitle Example 3",
"is_rental": 0,
"tag0": "TEST1",
"tag1": "lorem ipsum",
"tag2": "dolor"
},
{
"title": "PRODUCT3",
"url": "/rentals/product3/",
"sku": "1001R",
"subtitle": "Subtitle Example 3",
"is_rental": 1,
"tag0": "TEST2"
}
]

If I look at the "compiled" result by examining item, its only using tag0, not tag1 or tag2. So for Product 3, "compiled" is:

compiled: "TEST1 Subtitle Example 3"

@tombertrand
Copy link
Collaborator

hey @chris-shorter , I'm able to get the desired result
image

could it be that you enabled cache: true? This is a very evil option when still being in dev

@chris-shorter
Copy link
Author

chris-shorter commented Jan 23, 2020 via email

@chris-shorter
Copy link
Author

chris-shorter commented Jan 28, 2020

https://codepen.io/shorty2240/pen/VwYNNzN

image

So I definitely don't get results when I search for "lorem dolor" or "lorem subtitle" in this example - am I supposed to? Even "lorem ipsum dolor" which doesn't break up one term, does not match.

@tombertrand
Copy link
Collaborator

should definitly match, I'll have a look shortly

@chris-shorter chris-shorter changed the title correlativeTemplate: true not working with ajax correlativeTemplate: true not working as expected Jan 28, 2020
@chris-shorter
Copy link
Author

I have noticed something that might help. If I use the parameter in the template (eg {{tag1}} {{tag2}} etc) then they are correctly added to "compiled" and it works as expected. As a temp fix I can just include these and hide them with CSS, but I think that might be the nature of the issue?

@tombertrand
Copy link
Collaborator

tombertrand commented Jan 30, 2020

So, I've completly forgot that an alternative for your problem was implemented in #109, you are able to manually set the compiled keys in the correlativeTemplate option. Using your example I'm getting the desired result with:

correlativeTemplate: ["search_tags","subtitle","tag0","tag1","tag2"]

Here is the source: https://github.com/running-coder/jquery-typeahead/blob/master/src/jquery.typeahead.js#L1404

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants