Loading ...
Sorry, an error occurred while loading the content.
 

AS help. Multi deleting from a list.

Expand Messages
  • wesflex73
    I am sure there is an easy answer for this but I have tried a few things and not sure where to go from here. I have a list that is populated from an array in a
    Message 1 of 6 , May 1, 2005
      I am sure there is an easy answer for this but I have tried a few
      things and not sure where to go from here. I have a list that is
      populated from an array in a function.

      <mx:List id="myStateList" dataProvider="{statesList.states}"
      multipleSelection="true" />


      The array in the list is states:

      AZ
      CA
      NV
      MD
      NY

      I would like to delete multiple items with one click of a button. So
      if I select CA and MD, I would like to delete them from the list.

      My button has this call.

      <mx:Button label="Remove States" click="statesList.dStates
      (myStateList.selectedIndices)"/>

      That returns an array of (1,3)

      My Actionscript page (statesList) has this function.

      function dStates(delState:Array):Void {

      for ( var i = 0; i < delState.length; i++){
      states.removeItemAt(delState[i]);
      }
      }

      But what happens is that this will delete CA and NY. I understand why
      this is happening (because states is getting reset and the second
      time through the loop, NY is in the 3rd position of the array) so how
      else can I do this.

      Thank you for any help.
    • Jeff Tapper
      Heres a quick answer from my undercafineated brain
      Message 2 of 6 , May 1, 2005
        Heres a quick answer from my undercafineated brain
        <?xml version="1.0" encoding="utf-8"?>
        <mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml">
        <mx:List id="myStateList" dataProvider="{statesList}"
        multipleSelection="true" />
        <mx:Button label="Remove States" click="dStates(event)"/>
        <mx:Script>
        <![CDATA[
        var statesList:Array=new Array("AZ","CA","NV","MD","NY");
        function dStates(event:Object):Void {
        var lb = myStateList;
        for ( var i = lb.length; i >=0 ; i--){
        if(isInArray(lb.getItemAt(i),lb.selectedItems)){
        myStateList.removeItemAt(i);
        }
        }
        }
        function isInArray(item:String,array:Array):Boolean{
        for(var i=0;i<array.length;i++){
        if(array[i] == item){
        return true;
        }
        }
        return false;
        }
        ]]>
        </mx:Script>
        </mx:Application>

        Essentially loop over all items in the list box, compare each to the
        selectedItems. If a match, delete. The may well be a more elegant
        solution, but I'd need coffee for that


        At 06:47 AM 5/1/2005, you wrote:
        >I am sure there is an easy answer for this but I have tried a few
        >things and not sure where to go from here. I have a list that is
        >populated from an array in a function.
        >
        ><mx:List id="myStateList" dataProvider="{statesList.states}"
        >multipleSelection="true" />
        >
        >
        >The array in the list is states:
        >
        >AZ
        >CA
        >NV
        >MD
        >NY
        >
        >I would like to delete multiple items with one click of a button. So
        >if I select CA and MD, I would like to delete them from the list.
        >
        >My button has this call.
        >
        ><mx:Button label="Remove States" click="statesList.dStates
        >(myStateList.selectedIndices)"/>
        >
        >That returns an array of (1,3)
        >
        >My Actionscript page (statesList) has this function.
        >
        >function dStates(delState:Array):Void {
        >
        > for ( var i = 0; i < delState.length; i++){
        > states.removeItemAt(delState[i]);
        > }
        >}
        >
        >But what happens is that this will delete CA and NY. I understand why
        >this is happening (because states is getting reset and the second
        >time through the loop, NY is in the 3rd position of the array) so how
        >else can I do this.
        >
        >Thank you for any help.
        >
        >
        >
        >
        >
        >----------
        >Yahoo! Groups Links
        > * To visit your group on the web, go to:
        > *
        > <http://groups.yahoo.com/group/flexcoders/>http://groups.yahoo.com/group/flexcoders/
        >
        > *
        > * To unsubscribe from this group, send an email to:
        > *
        > <mailto:flexcoders-unsubscribe@yahoogroups.com?subject=Unsubscribe>flexcoders-unsubscribe@yahoogroups.com
        >
        > *
        > * Your use of Yahoo! Groups is subject to the
        > <http://docs.yahoo.com/info/terms/>Yahoo! Terms of Service.
      • Darron J. Schall
        ... Just change the line in the loop to: states.removeItemAt(delState[i]-i); Everytime you remove an item, all of the indexes need to be shifted left 1. If
        Message 3 of 6 , May 1, 2005
          wesflex73 wrote:

          >function dStates(delState:Array):Void {
          >
          > for ( var i = 0; i < delState.length; i++){
          > states.removeItemAt(delState[i]);
          > }
          >}
          >
          >

          Just change the line in the loop to:

          states.removeItemAt(delState[i]-i);

          Everytime you remove an item, all of the indexes need to be shifted left
          1. If you remove 2 items, they need to be shifted left 2. Becuase the
          value of i through every iteration is the number of items you have
          previously removed, i acts as the amount you need to shift by... which
          makes the calculation pretty easy.

          -d
        • david.aaron@thatplanet.net
          Have you considered de-incrementing the loop? Take the last one first then the compression would not change the lesser values. I think this might be more
          Message 4 of 6 , May 1, 2005
            Have you considered de-incrementing the loop? Take the last one first then the compression would not change the lesser values. I think this might be more computationally more efficient if the array was larger than 50.
             
            ----- Original Message -----
            From: wesflex73
            Sent: Sunday, May 01, 2005 11:47 AM
            Subject: [flexcoders] AS help. Multi deleting from a list.

            I am sure there is an easy answer for this but I have tried a few
            things and not sure where to go from here. I have a list that is
            populated from an array in a function.

            <mx:List id="myStateList" dataProvider="{statesList.states}"
            multipleSelection="true" />
                                               

            The array in the list is states:

            AZ
            CA
            NV
            MD
            NY

            I would like to delete multiple items with one click of a button. So
            if I select CA and MD, I would like to delete them from the list.

            My button has this call.

            <mx:Button label="Remove States" click="statesList.dStates
            (myStateList.selectedIndices)"/>
                                               
            That returns an array of (1,3)

            My Actionscript page (statesList) has this function.

            function dStates(delState:Array):Void {
                       
                          for ( var i = 0; i < delState.length; i++){
                                 states.removeItemAt(delState[i]);
                                }
            }

            But what happens is that this will delete CA and NY. I understand why
            this is happening (because states is getting reset and the second
            time through the loop, NY is in the 3rd position of the array) so how
            else can I do this.

            Thank you for any help.



          • Darron J. Schall
            ... If you re really worried about the efficiency you shouldn t use removeItemAt, but rather just call: states.splice( index, 1 ); That will delete a single
            Message 5 of 6 , May 1, 2005
              david.aaron@... wrote:

              > Have you considered de-incrementing the loop? Take the last one first
              > then the compression would not change the lesser values. I think this
              > might be more computationally more efficient if the array was larger
              > than 50.

              If you're really worried about the efficiency you shouldn't use
              removeItemAt, but rather just call:

              states.splice( index, 1 );

              That will delete a single element at a specified index. Then, when
              you're done deleting all elements, you'll want to dispatch a
              modelChanged event so that all of the listening elements to the data
              provider get updated:

              states.dispatchEvent({type:"modelChanged"});

              Doing it the above way will avoid the modelChanged event being
              dispatched for every element removal, avoiding all of that extra
              processing. Rather, you can "batch" your changes via splice then notify
              all of the listeners at the end.

              And yes, looping over the elements backwards would be slightly faster
              than subtracting i again each iteration..

              -d
            • Wes Leonardo
              Thanks all for your help. I went with the states.removeItemAt(delState[i]-i); solution since it was the quickest to implement. I did have to add
              Message 6 of 6 , May 1, 2005
                Thanks all for your help.
                 
                I went with the states.removeItemAt(delState[i]-i); solution since it was the quickest to implement. I did have to add delStates.sort() in the beginning of the function since selectedIndencies() could be set to (1,0,4,3) depending on which order you select the states in the list. 
                 
                Darron thanks for the idea of slice and dispatchEvent. I will look into that when I have more time.
                 
                Thanks.

                "Darron J. Schall" <darron@...> wrote:
                david.aaron@... wrote:

                > Have you considered de-incrementing the loop? Take the last one first
                > then the compression would not change the lesser values. I think this
                > might be more computationally more efficient if the array was larger
                > than 50.

                If you're really worried about the efficiency you shouldn't use
                removeItemAt, but rather just call:

                states.splice( index, 1 );

                That will delete a single element at a specified index.  Then, when
                you're done deleting all elements, you'll want to dispatch a
                modelChanged event so that all of the listening elements to the data
                provider get updated:

                states.dispatchEvent({type:"modelChanged"});

                Doing it the above way will avoid the modelChanged event being
                dispatched for every element removal, avoiding all of that extra
                processing.  Rather, you can "batch" your changes via splice then notify
                all of the listeners at the end.

                And yes, looping over the elements backwards would be slightly faster
                than subtracting i again each iteration..

                -d


                Do you Yahoo!?
                Make Yahoo! your home page
              Your message has been successfully submitted and would be delivered to recipients shortly.