Rev 6939 | Rev 7554 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
6481 | obado | 1 | |
6939 | obado | 2 | /********* Autocomplete.js ************************************************************************ |
3 | Autocompletion d'un champ texte |
||
4 | ajoutez la classe "multicomplete" ou "autocomplete" au choix sur l'input, selon que vous |
||
6942 | bpr | 5 | souhaitez autoriser un ou plusieurs mot-cles. |
6 | Puis lancez set_multicomplete, ou set_autocomplete avec la liste des mots cles en parametre |
||
6939 | obado | 7 | ****************************************************************************************************/ |
6486 | obado | 8 | |
6939 | obado | 9 | |
10 | function comma_split( val ) { |
||
11 | return val.split( /,\s*/ ); |
||
12 | } |
||
13 | |||
14 | function extractLast( term ) { |
||
15 | return comma_split( term ).pop(); |
||
16 | } |
||
17 | |||
18 | function set_multicomplete(tags){ |
||
19 | $(".multicomplete") |
||
20 | // don't navigate away from the field on tab when selecting an item |
||
21 | .bind( "keydown", function( event ) { |
||
22 | if (event.keyCode === $.ui.keyCode.TAB && $( this ).data( "ui-autocomplete" ).menu.active ) { |
||
23 | event.preventDefault(); |
||
24 | } |
||
25 | }) |
||
26 | //Adds Autocompletion |
||
27 | .autocomplete({ |
||
28 | focus: function() { |
||
29 | // prevent value inserted on focus |
||
30 | return false; |
||
31 | }, |
||
32 | source: function( request, response ) { |
||
33 | // delegate back to autocomplete, but extract the last term |
||
34 | response( $.ui.autocomplete.filter(tags, extractLast( request.term ) ) ); |
||
35 | }, |
||
36 | select: function( event, ui ) { |
||
37 | var terms = comma_split( this.value ); |
||
38 | // remove the current input |
||
39 | terms.pop(); |
||
40 | // add the selected item |
||
41 | terms.push( ui.item.value ); |
||
42 | // add placeholder to get the comma-and-space at the end |
||
43 | terms.push(""); |
||
44 | this.value = terms.join(", "); |
||
45 | return false; |
||
46 | } |
||
6486 | obado | 47 | }); |
6939 | obado | 48 | } |
6486 | obado | 49 | |
6939 | obado | 50 | function set_autocomplete(tags){ |
6486 | obado | 51 | $(".autocomplete").autocomplete({ |
6487 | obado | 52 | source: function( request, response ) { |
53 | var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" ); |
||
54 | response( $.grep( tags, function( item ){ |
||
6481 | obado | 55 | return matcher.test( item ); |
6487 | obado | 56 | }) ); |
57 | } |
||
6481 | obado | 58 | }); |
6939 | obado | 59 | } |