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

Nested Json (GeoJson) #1761

Open Simon-Miles-rbwm opened 5 years ago

Simon-Miles-rbwm commented 5 years ago

I'm looking to search against a GeoJson, example extract below but I need some pointers on how to access the returned result back into my template. I can see in the console that the Query that I'm sending is returning results.

var getSearches = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), 
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: {
            url: 'http://localhost:3001/search/%QUERY',
            wildcard: '%QUERY'
        }
    });

    getSearches.initialize();

   $('.typeahead').typeahead(
        null, {
        name: 'getSearches',
        displayKey: 'site_name',
        source: getSearches,    
        templates: {
    header: '<div><strong>{{value}}</strong></div>'
  } 
    }).on('typeahead:selected', function(event, data){            
        $('.typeahead').val(data.features.properties.site_name);        
    });
 "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -0.60140650987722599,
                    51.501188671221797
                ]
            },
            "properties": {
                "job_number": "95507",
                "job_entry_date": "12\04\2019 12:14:00",
                "locality_name": "Eton",
                "asset_number": "9700",
                "site_name": "Slough Road",
                "sor_item_name": "Driver\machine operator"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -0.60140650987722599,
                    51.501188671221797
                ]
            },
            "properties": {
                "job_number": "95507",
                "job_entry_date": "12\04\2019 12:14:00",
                "locality_name": "Eton",
                "asset_number": "9700",
                "site_name": "Slough Road",
                "sor_item_name": "Ganger"
            }
        }.....