12 Replies Latest reply on Jun 6, 2010 12:36 AM by David_F57

    Making a list a definite length with no repeated data

    iamcootis Level 1

      I have a list tag that currently accepts repeats and as many objects as I will drop into it. Is there a simple way to make this where it will only accept four objects and no repeats?

        • 1. Re: Making a list a definite length with no repeated data
          David_F57 Level 5

          hi,

           

          There are a few ways to do this, your drag object can have a flag that is set after a successful drag that can be used to test whether to accept the drag operation from that object so it can only happen once, If the list length equals the maximum allowable drop objects you can disallow the drop.

           

          something like this

           

           

          protected function dropCanvas_dragEnterHandler(event:DragEvent):void
                      {
          
                         if (event.target.used = false && itemlist.length < 4)
                          {
                              if (event.target.used == false && mylist.length < 4)
                              {
                                   event.target.used = true;  
                                   DragManager.acceptDragDrop(Canvas(event.target));
                              }

           

          • 2. Re: Making a list a definite length with no repeated data
            iamcootis Level 1

            I can kinda see what you're doing but I can't get it to work. Plus, you can't get access to the lists length property.

            • 3. Re: Making a list a definite length with no repeated data
              David_F57 Level 5

              hi,

               

              I'll do up a quick running example in the morning, are you using flex 3 or flex 4 sdk.

               

              David.

              • 5. Re: Making a list a definite length with no repeated data
                David_F57 Level 5

                hi,

                 

                the code isn't the cleanest in the world(plus I haven't really used flex 3 seriously for over a year) but it does what you want,

                 

                http://gumbo.flashhub.net/norepeat    source included

                 

                 

                David

                • 6. Re: Making a list a definite length with no repeated data
                  iamcootis Level 1

                  Thanks a lot..it works great and I learned a couple things too.

                  • 7. Re: Making a list a definite length with no repeated data
                    iamcootis Level 1

                    My client found a problem in this and was able to add more than 4 items and also duplicates. During debugging it appears if you move the items fast enough, the events won't get fired and you can drop the items without them being checked. Any idea how I can work around this?

                    • 8. Re: Making a list a definite length with no repeated data
                      David_F57 Level 5

                      hi,

                       

                       

                      You must have changed the drag/drop methodology somewhere, the original code only enables drop once criteria for drop has been meet so you can move as fast as you want it can't drop if the criteria test fails, if the criteria event isn't fired then the acceptdrop is never fired.  If you want I can look at you code to see how it differes.

                       

                      David

                      • 9. Re: Making a list a definite length with no repeated data
                        iamcootis Level 1

                        Here is the code in my item renderer:

                         

                        protected function hbox1_mouseDownHandler(event:MouseEvent):void{
                                        var target:Canvas = Canvas(event.currentTarget);
                                        var sourceObject:Lesson = new Lesson(data.lesson, data.lessonNumber);
                                        var dragSource:DragSource = new DragSource();
                                        dragSource.addData( sourceObject, "listCopy" );
                                        mx.managers.DragManager.doDrag(target, dragSource, event, null , -0, -0, 0.40, true );
                                    }

                         

                        Here are the two drag//drop handlers in my class:

                         

                            /**
                                         * allows for 4 items on the list and no repeats
                                         */ 
                                        private function dragDropHandler(event:DragEvent):void{
                                            if (event.dragSource.hasFormat('listCopy'))
                                            {
                                                var newObj:Lesson;
                                                newObj = event.dragSource.dataForFormat("listCopy") as Lesson;
                                                model.selectedLessonList.addItem(newObj as Lesson);
                                            }   
                                            }
                                           
                                            /**
                                         * allows for 4 items on the list and no repeats
                                         */
                                           private function dragEnterHandler(event:DragEvent):void
                                        {
                                            tmpObject = event.dragSource.dataForFormat("listCopy");
                                            if(tmpObject == null){return;}//something messed up!
                                            trace(tmpObject.lesson);
                                            repeatFlag = false;
                                            for (var i:int=0; i < model.selectedLessonList.length;i++){
                                                if (model.selectedLessonList[i].lesson == tmpObject.lesson){
                                                    repeatFlag=true;
                                                    break;
                                                }   
                                            }   
                                            if (model.selectedLessonList.length < maxLength && repeatFlag==false){
                                                DragManager.acceptDragDrop(List(event.target));       
                                                DragManager.showFeedback(mx.managers.DragManager.COPY);
                                            }
                                        }  

                         

                        Here is the list containing the original item:

                         

                                   <mx:List id="lstAllCategories" width="245" height="95%" itemRenderer="com.components.renderers.CategoryListItemRenderer"
                                                        dataProvider="{ApplicationModel.instance.categoryList}" styleName="BoxBackground"
                                                        rollOver="createToolTip('Mouse_Over_AllCategories')" rollOut="createToolTip('Mouse_Over_BottomOfScreen')">

                         

                        Here is the list that the item is being dropped into:

                         

                        <mx:List id="lstCurrentSelectedLessons" dataProvider="{model.selectedLessonList}" width="245" height="140"  dropEnabled="true" dragDrop="dragDropHandler(event);checkSelectedLessonList()" dragEnter="dragEnterHandler(event)"
                                                        doubleClickEnabled="true" doubleClick="beginLesson()" itemRenderer="com.components.renderers.ChosenListItemRenderer" styleName="BoxBackground"
                                                        rollOver="createToolTip('Mouse_Over_CurrentLessons')" rollOut="createToolTip('Mouse_Over_BottomOfScreen')">
                                                           
                                                    </mx:List>

                        • 10. Re: Making a list a definite length with no repeated data
                          David_F57 Level 5

                          hi,

                           

                          in your drop list <mx:List id="lstCurrentSelectedLessons"/>

                           

                          try removing dropenabled=true, not sure but this maybe what is cuasing the clash with the 'controlled' drop handler.

                           

                           

                          David.

                          1 person found this helpful
                          • 11. Re: Making a list a definite length with no repeated data
                            iamcootis Level 1

                            Awesome...that fixed it. Do you ever sleep?

                            • 12. Re: Making a list a definite length with no repeated data
                              David_F57 Level 5

                              hi,

                               

                              Glad that it worked, I figured that it would have an adverse affect..

                               

                              I think sleep was deprecated back in the nineties

                               

                              David.