Précédent   Forum des professionnels en informatique > Webmasters - Développement Web > JavaScript > Bibliothèques & Frameworks > jQuery
jQuery Forum d'entraide sur le framework jQuery. Avant de poster : Tutoriels jQuery, FAQ jQuery, Tous les tutoriels JavaScript, Toutes les FAQ JavaScript
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 13/01/2012, 16h29   #1
Membre du Club
 
Inscription : janvier 2010
Messages : 210
Détails du profil
Informations personnelles :
Âge : 37

Informations forums :
Inscription : janvier 2010
Messages : 210
Points : 53
Points : 53
Par défaut Autocomplete Query UI - supprimer items affichés dans une DIV

Bonjour,

J'utilise l'Autocomplete de jQuery UI en suivant le code de la demo http://jqueryui.com/demos/autocomplete/#remote :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
<script>
$(function() {
	function log( message ) {
		$( "<div/>" ).text( message ).prependTo( "#log" );
		$( "#log" ).attr( "scrollTop", 0 );
	}
 
	$( "#birds" ).autocomplete({
		source: "search.php",
		minLength: 2,
		select: function( event, ui ) {
			log( ui.item ?
				"Selected: " + ui.item.value + " aka " + ui.item.id :
				"Nothing selected, input was " + this.value );
		}
	});
});
</script>
Code :
1
2
3
4
5
6
7
8
9
10
11
 
<div class="demo">
	<div class="ui-widget">
		<label for="birds">Birds: </label>
		<input id="birds" />
	</div>
	<div class="ui-widget" style="margin-top:2em; font-family:Arial">
		Result:
		<div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div>
	</div>
</div><!-- End demo -->
Si je choisis successivement par ex. 3 items dans la liste , le code html généré est du type :
Code :
1
2
3
4
5
6
7
 
Result:
<div id="log" class="ui-widget-content" style="height: 200px; width: 300px; overflow: auto;">
   <div>Selected: Spotted Crake aka 1</div>
   <div>Selected: Corn Bunting aka 2 </div>
   <div>Selected: Avocet aka 3 </div>
</div>
Je souhaiterais compléter ce code pour supprimer de façon différenciée l'affichage d'un item rapatrié dans la DIV "log". En cliquant sur une croix en face d'un item présent dans la DIV '"log", cela ferait disparaître l'item considéré de cette DIV. Je pense que chaque <div>Selected:,,,</div> pourrait être différenciée en lui affectant la valeur unique du ui.item.id.
Code :
1
2
3
4
5
6
7
 
Result:
<div id="log" class="ui-widget-content" style="height: 200px; width: 300px; overflow: auto;">
   <div id="1">Selected: Spotted Crake aka 1 <span>X</span></div>//si je clique sur cette croix la div ayant l'ID1 disparaît
   <div id="2">Selected: Corn Bunting aka 2 <span>X</span></div>//si je clique sur cette croix la div ayant l'ID2 disparaît
   <div id="3">Selected: Avocet aka 3 <span>X</span></div>//si je clique sur cette croix la div ayant l'ID3 disparaît
</div>
Mais je suis débutant et ne vois pas comment mettre en pratique mon idée. Pouvez-vous m'aider ? Merci.
almoha est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 13/01/2012, 20h03   #2
Rédacteur
 
Avatar de danielhagnoul
 
Homme Daniel Hagnoul
Étudiant perpétuel
Inscription : février 2009
Messages : 3 221
Détails du profil
Informations personnelles :
Nom : Homme Daniel Hagnoul
Âge : 61
Localisation : Belgique

Informations professionnelles :
Activité : Étudiant perpétuel
Secteur : Enseignement

Informations forums :
Inscription : février 2009
Messages : 3 221
Points : 6 767
Points : 6 767
Bonsoir

Exemple :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!doctype html>
<html lang="fr">
<head>
	<meta charset="utf-8">
	<meta name="Author" content="Daniel Hagnoul">
	<title>Forum jQuery</title>
	<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/themes/humanity/jquery-ui.css">
	<link rel="stylesheet" href="http://danielhagnoul.developpez.com/lib/jPicker/css/jPicker.dvjh-1.1.6.min.css" />
	<style>
		/* Base */
		html {font-size:62.5%; } /* Pour 62.5% 1rem =~ 10px */
		div,p,h1,h2,h3,h4,h5,h6,ul,ol,dl,form,table,img {margin:0; padding:0; }
		body {background-color:rgb(122, 79, 79); color:#000000; font-family:sans-serif; font-size:1.4rem; font-style:normal; font-weight:normal; line-height:normal; letter-spacing:normal; }
		h1,h2,h3,h4,h5 {font-family:'Redressed', cursive; padding:0.6rem; }
		p, div, td {word-wrap:break-word; }
		pre, code {white-space:pre-wrap; word-wrap:break-word; }
		img, input, textarea, select {max-width:100%; }
        img {border:none; }
		h1 {font-size:2.4rem; text-shadow: 0.4rem 0.4rem 0.4rem #bbbbbb; text-align:center; }
		p {padding:0.6rem; }
		.conteneur {width:95%; min-width:80rem; min-height:30rem; margin:1.2rem auto; background-color:#ffffff; color:#000000; border:0.1rem solid #666666; }
		footer {margin-left:3.6rem; }
 
		/* -- */
	</style>
</head>
<body>
	<h1>Forum jQuery</h1>
	<section class="conteneur">
 
		<div class="ui-widget">
			<label for="langages">Langages: </label>
			<input id="langages" />
		</div>
 
		<div class="ui-widget" style="margin-top:2em; font-family:Arial">
			Result:
			<div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div>
			<button id="removeCheckedItem">Remove ckecked items</button>
		</div>
 
	</section>
	<footer itemscope itemtype="http://data-vocabulary.org/Person">
		<time datetime="2012-01-13T19:58:31.000+01:00" pubdate>2012-01-13</time>
		<span itemprop="name">Daniel Hagnoul</span>
		<a href="http://www.developpez.net/forums/u285162/danielhagnoul/" itemprop="url">@danielhagnoul</a>
	</footer>
	<script charset="utf-8" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
	<script charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/i18n/jquery-ui-i18n.min.js"></script>
	<script charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/jquery-ui.min.js"></script>
	<script charset="utf-8" src="http://danielhagnoul.developpez.com/lib/jPicker/jpicker-1.1.6.min.js"></script>
	<script>	
		"use strict";
 
		// Date ISO format long US
		Date.prototype.formatISO = function(){
			this._nowFormat = 'aaaa-mm-jjThh:ii:ss.000Szz:00';
			this._toLen2 = function(_nowStr){
				_nowStr = _nowStr.toString();
				return ('0'+_nowStr).substr(-2,2);
			};
			this._nowFormat = this._nowFormat.replace(/j+/, this._toLen2(this.getDate()));
			this._nowFormat = this._nowFormat.replace(/m+/, this._toLen2(this.getMonth()+1));
			this._nowFormat = this._nowFormat.replace(/a+/, this.getFullYear());
			this._nowFormat = this._nowFormat.replace(/h+/, this._toLen2(this.getHours()));
			this._nowFormat = this._nowFormat.replace(/i+/, this._toLen2(this.getMinutes()));
			this._nowFormat = this._nowFormat.replace(/s+/, this._toLen2(this.getSeconds()));
			this._nowFormat = this._nowFormat.replace(/S+/, (this.getTimezoneOffset() < 0) ? ("+") : ("-"));
			this._nowFormat = this._nowFormat.replace(/z+/, this._toLen2(Math.abs(this.getTimezoneOffset()/60)));
			return this._nowFormat;
		};
 
		$(function(){
			/* Base */
			console.log(new Date().formatISO());
 
			/* -- */
 
			var availableTags = [
				"ActionScript",
				"AppleScript",
				"Asp",
				"BASIC",
				"C",
				"C++",
				"Clojure",
				"COBOL",
				"ColdFusion",
				"Erlang",
				"Fortran",
				"Groovy",
				"Haskell",
				"Java",
				"JavaScript",
				"Lisp",
				"Perl",
				"PHP",
				"Python",
				"Ruby",
				"Scala",
				"Scheme"
			];
 
			function log( message ) {
				$( "<div/>" ).html( '<input type="checkbox"/>' + message ).prependTo( "#log" );
				$( "#log" ).scrollTop( 0 );
			}
 
			$( "#langages" ).autocomplete({
				source: availableTags,
				minLength: 1,
				select: function( event, ui ) {
					log( ui.item ?
						'Selected: ' + ui.item.value :
						"Nothing selected, input was " + this.value );
				}
			});
 
			$( "#removeCheckedItem" ).click( function(){
				$( "#log" ).find( "input:checked" ).parent().remove();
			});
 
 
			/* jPicker : http://www.digitalmagicpro.com/jPicker/, outil pour choisir rapidement une couleur */
			$.fn.jPicker.defaults.images.clientPath="http://danielhagnoul.developpez.com/lib/jPicker/images/";
			$(".conteneur").jPicker({window:{expandable:true,title:"jPicker : choissisez une couleur :",
			alphaSupport:true,position:{x:'screenCenter',y:'top'}},color:{ active:new $.jPicker.Color({ r: 210, g: 214, b: 98, a: 128 })}},
			function(color, context){var c = color.val("all");if (c){$("body").css("backgroundColor", "rgba(" +
			c.r + "," + c.g + "," + c.b + "," + (c.a/255).toFixed(2) + ")");}});
		});
 
		$(window).load(function(){
 
		});
	</script>
</body>  
</html>
__________________

FAQ jQuery

Mon cahier d’exercices sur jQuery & Co

Si un message vous a aidé ou vous semble pertinent, votez pour lui !
danielhagnoul est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 14/01/2012, 14h09   #3
Membre du Club
 
Inscription : janvier 2010
Messages : 210
Détails du profil
Informations personnelles :
Âge : 37

Informations forums :
Inscription : janvier 2010
Messages : 210
Points : 53
Points : 53
Bonjour,

Merci beaucoup pour cet exemple de code. Je vais l'adopter et l'adapter. Dans le code de la demo :
Code :
1
2
3
4
5
6
 
select: function( event, ui ) {
			log( ui.item ?
				"Selected: " + ui.item.value + " aka " + ui.item.id :
				"Nothing selected, input was " + this.value );
		}
il est affiché dans la DIV "log", outre le ui.item.value, le ui.item.id qui correspond à l'id de l'item. Dans mon propre code je récupère aussi un id. Mais au lieu d'afficher la valeur de cet id avec .text, je souhaiterais l'affecter à la value d'un champ hidden (chaque item rapatrié dans la DIV "log" aurait son propre champ hidden et donc sa propre value). J'avais pensé au code suivant :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
$( "#langages" ).autocomplete({
				source: "scripts/autoc/search_autocomplete.php",
                                minLength: 2,
				select: function( event, ui ) {
 
				$('<input type="hidden" name="contrib_post[]" id="contrib_post'+ui.item.id+'" value="" >');//création de l'input hidden avec  un id correspondant à la valeur de ui.item.id
				$(this).val(ui.item.id);// affectation de la valeur de ui.item.id à la value de l'input précédemment créé
 
			        log( ui.item ?
 
				ui.item.value :
				"Nothing selected, input was " + this.value );
 
 
				}
			});
Le code n'entraîne aucune erreur mais rien ne se passe (pas de création dynamique de l'input hidden). Pouvez-vous m'aider ? Merci.
almoha est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 14/01/2012, 18h57   #4
Rédacteur
 
Avatar de danielhagnoul
 
Homme Daniel Hagnoul
Étudiant perpétuel
Inscription : février 2009
Messages : 3 221
Détails du profil
Informations personnelles :
Nom : Homme Daniel Hagnoul
Âge : 61
Localisation : Belgique

Informations professionnelles :
Activité : Étudiant perpétuel
Secteur : Enseignement

Informations forums :
Inscription : février 2009
Messages : 3 221
Points : 6 767
Points : 6 767
Bonsoir

En passant du HTML dans le paramètre message.

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function log( message ) {
	$( "<div/>" ).html( message ).prependTo( "#log" );
	$( "#log" ).scrollTop( 0 );
}
 
$( "#langages" ).autocomplete({
	source: "scripts/autoc/search_autocomplete.php",
	minLength: 2,
	select: function( event, ui ) {
		log( ui.item ? (
			'<input type="checkbox"/> Selected: ' +
			ui.item.value +
			'<input type="hidden" name="contrib_post[]" id="contrib_post' +
			ui.item.id +
			'" value="" >'
			) :
			"Nothing selected, input was " + this.value );
	}
});
__________________

FAQ jQuery

Mon cahier d’exercices sur jQuery & Co

Si un message vous a aidé ou vous semble pertinent, votez pour lui !
danielhagnoul est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 16/01/2012, 20h45   #5
Membre du Club
 
Inscription : janvier 2010
Messages : 210
Détails du profil
Informations personnelles :
Âge : 37

Informations forums :
Inscription : janvier 2010
Messages : 210
Points : 53
Points : 53
Bonsoir,

Merci beaucoup pour votre aide. Votre code répond à ma problématique
almoha est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 19h30.


 
 
 
 
Partenaires

Hébergement Web