Last active
December 24, 2015 10:39
-
-
Save pocky/6785790 to your computer and use it in GitHub Desktop.
Amusez toi bien avec du JSON + Typehead
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"52498eea8ead0ec0230041a7":{"id":"52498eea8ead0ec0230041a7","name":"Les appareils de chauffage \u00e9lectrique moderne","slug":"les-appareils-de-chauffage-electrique-moderne","type":"page"}, | |
"52498f0e8ead0ec5200041a8":{"id":"52498f0e8ead0ec5200041a8","name":"Les technologies utilis\u00e9es","slug":"les-technologies-utilisees","type":"page"}, | |
"52498f2c8ead0ec0230041a8":{"id":"52498f2c8ead0ec0230041a8","name":"Les normes applicables","slug":"les-normes-applicables","type":"page"}, | |
"52498f4a8ead0e7b220041a8":{"id":"52498f4a8ead0e7b220041a8","name":"Confort thermique","slug":"confort-thermique","type":"page"}, | |
"52498f8b8ead0ec5200041a9":{"id":"52498f8b8ead0ec5200041a9","name":"Propret\u00e9 de l'air","slug":"proprete-de-l-air","type":"page"}, | |
"52498fa88ead0ec0230041a9":{"id":"52498fa88ead0ec0230041a9","name":"Souplesse et adaptabilit\u00e9","slug":"souplesse-et-adaptabilite","type":"page"}, | |
"52498fc28ead0e7b220041a9":{"id":"52498fc28ead0e7b220041a9","name":"Simplicit\u00e9 d'utilisation","slug":"simplicite-d-utilisation","type":"page"}, | |
"524990b78ead0ec5200041aa":{"id":"524990b78ead0ec5200041aa","name":"Un faible investissement financier","slug":"un-faible-investissement-financier","type":"page"}, | |
"524990cf8ead0ec0230041aa":{"id":"524990cf8ead0ec0230041aa","name":"Des \u00e9conomies \u00e0 court et long termes","slug":"des-economies-a-court-et-long-termes","type":"page"}, | |
"5249912a8ead0e7b220041aa":{"id":"5249912a8ead0e7b220041aa","name":"Qu'est-ce qu'un appareil ancien ?","slug":"qu-est-ce-qu-un-appareil-ancien","type":"page"}, | |
"524991428ead0ec5200041ab":{"id":"524991428ead0ec5200041ab","name":"Les \u00e9tapes du remplacement","slug":"les-etapes-du-remplacement","type":"page"} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
$(document).ready(function() { | |
$(".typeahead").typeahead({ | |
source: function( query, process ) { | |
$.ajax({ | |
url: "{{ url('search_json', {}, true) }}", | |
dataType: "json", | |
data: { | |
text: query.term | |
}, | |
success: function( data ) { | |
var documents = new Array; | |
$.each(data, function (i, document) { | |
documents.push(document.name + ';' + document.type + ';' + document.slug); | |
}); | |
process(documents); | |
} | |
}); | |
}, | |
matcher: function (item) { | |
if (item.toLowerCase().indexOf(this.query.trim().toLowerCase()) != -1) { | |
return true; | |
} | |
}, | |
sorter: function (items) { | |
return items.sort(); | |
}, | |
highlighter: function (item) { | |
var regex = new RegExp( '(' + this.query + ')', 'gi' ); | |
return item.replace( regex, "<strong>$1</strong>" ); | |
}, | |
updater: function (item) { | |
var reg = new RegExp(";", "g"); | |
var result = item.split(reg); | |
var route = "{{ url('page_show', { 'slug':"SLUG"}, true) }}"; | |
if ('article' == result[1]) { | |
route = "{{ url('article_show', { 'slug':"SLUG"}, true) }}"; | |
} | |
window.location = route.replace('SLUG', result[2]); | |
} | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment