IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

jQuery Discussion :

droppable vérif si élément existe déjà


Sujet :

jQuery

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2011
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Bâtiment

    Informations forums :
    Inscription : Décembre 2011
    Messages : 33
    Par défaut droppable vérif si élément existe déjà
    Bonjour,
    J'utilise le drag & drop jQuery qui fonctionne très bien.

    Je mets mes éléments div draggable dans des éléments div droppable.

    J'ai utilisé le shopping-cart.html de la démo jQuery.

    J'aimerais par contre vérifier si l'élément que je drop dans un div n'est pas déjà existant dans celui-ci. S'il l'est je veux empêcher le drop pour ne pas avoir deux fois le même.

    Quelq'un a-t-il une idée ?

  2. #2
    Rédacteur

    Avatar de danielhagnoul
    Homme Profil pro
    Étudiant perpétuel
    Inscrit en
    Février 2009
    Messages
    6 389
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Belgique

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

    Informations forums :
    Inscription : Février 2009
    Messages : 6 389
    Billets dans le blog
    125
    Par défaut
    Bonsoir

    Exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    <!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; }
     
    		/* -- */
    		#products { float:left; width: 50rem; margin-right: 2rem; }
    		#cart { width: 20rem; float: left; }
    		#products h1, #cart h1 { padding: .2rem; margin: 0; }
    		#cart ol { margin: 0; padding: 1rem 0 1rem 3rem; }
    	</style>
    </head>
    <body>
    	<h1>Forum jQuery</h1>
    	<section class="conteneur">
     
    		<div id="products">
    			<h1 class="ui-widget-header">Products</h1>	
    			<div id="catalog">
    				<h3><a href="#">T-Shirts</a></h3>
    				<div>
    					<ul>
    						<li>Lolcat Shirt</li>
    						<li>Cheezeburger Shirt</li>
    						<li>Buckit Shirt</li>
    					</ul>
    				</div>
    				<h3><a href="#">Bags</a></h3>
    				<div>
    					<ul>
    						<li>Zebra Striped</li>
    						<li>Black Leather</li>
    						<li>Alligator Leather</li>
    					</ul>
    				</div>
    				<h3><a href="#">Gadgets</a></h3>
    				<div>
    					<ul>
    						<li>iPhone</li>
    						<li>iPod</li>
    						<li>iPad</li>
    					</ul>
    				</div>
    			</div>
    		</div>
     
    		<div id="cart">
    			<h1 class="ui-widget-header">Shopping Cart</h1>
    			<div class="ui-widget-content">
    				<ol>
    					<li class="placeholder">Add your items here</li>
    				</ol>
    			</div>
    		</div>
     
    	</section>
    	<footer itemscope itemtype="http://data-vocabulary.org/Person">
    		<time datetime="2012-01-13T20:55:26.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());
     
    			/* -- */
    			$( "#catalog" ).accordion();
     
    			$( "#catalog li" ).draggable({
    				appendTo: "body",
    				helper: "clone"
    			});
     
    			$( "#cart ol" ).droppable({
    				activeClass: "ui-state-default",
    				hoverClass: "ui-state-hover",
    				accept: ":not(.ui-sortable-helper)",
    				drop: function( event, ui ) {
    					$( this ).find( ".placeholder" ).remove();
     
    					var bool = false;
     
    					$( "li", this ).each( function( i, item){
    						if ( $( item ).text() === ui.draggable.text() ){
    							bool = true;
    							return false;
    						}
    					});
     
    					if ( !bool ){
    						$( "<li></li>" ).text( ui.draggable.text() ).appendTo( this );
    					}
    				}
    			}).sortable({
    				items: "li:not(.placeholder)",
    				sort: function() {
    					// gets added unintentionally by droppable interacting with sortable
    					// using connectWithSortable fixes this, but doesn't allow you to customize active/hoverClass options
    					$( this ).removeClass( "ui-state-default" );
    				}
    			});
     
    			/* 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>

    Blog

    Sans l'analyse et la conception, la programmation est l'art d'ajouter des bogues à un fichier texte vide.
    (Louis Srygley : Without requirements or design, programming is the art of adding bugs to an empty text file.)

  3. #3
    Membre averti
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2011
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Bâtiment

    Informations forums :
    Inscription : Décembre 2011
    Messages : 33
    Par défaut
    super. merci.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 2
    Dernier message: 23/07/2009, 09h35
  2. Message si l'élément existe dans la table
    Par aymane19 dans le forum VB 6 et antérieur
    Réponses: 1
    Dernier message: 11/05/2008, 19h13
  3. Savoir si un élément existe dans un tableau
    Par peuf23 dans le forum Langage
    Réponses: 2
    Dernier message: 30/06/2006, 12h09
  4. Copier un élément existant
    Par Loceka dans le forum Général JavaScript
    Réponses: 6
    Dernier message: 13/11/2005, 15h28

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo