كيف يمكنني إزالة عناصر من مجموعة مع وظيفة التصفية، ثم قم بإضافة العناصر الجديدة التي لا تستوفي معايير التصفية؟

StackOverflow https://stackoverflow.com/questions/513498

سؤال

ولدي مجموعة، وأريد لإزالة كافة العناصر التي تحتوي على خاصية معينة لتعيين صحيح. يمكنني استخدام filterFunction لتحقيق ذلك. سؤالي هو: كيف يمكنني إضافة عناصر جديدة إلى مجموعة التي تلك الخاصية لتعيين صحيح؟ لا يزال تطبيق filterFunction، ولا يتم إضافة هذا البند ....

هل يجب تكرار خلال مجموعة كاملة وإزالة العناصر في وقت واحد؟ وأعتقد أن هذا هو بالضبط ما تحديث () لا.

وشكرا.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
    <mx:Script>
        private function hideSpecialItems():void
        {
            items.filterFunction = 
                function (item:Object):Boolean
                {
                    return item.isSpecial;
                }

            items.refresh();

            trace(items.length.toString()); // 2
        }

        private function addSpecialItem():void
        {
            items.addItem({name: "new Special Item", isSpecial: true});

            trace(items.length.toString()); // Item is added - returns 3
        }

        private function addNormalItem():void
        {
            items.addItem({name: "new Item", isSpecial: false});

            trace(items.length.toString()); // Item not added - returns 2
        }
    </mx:Script>

    <mx:ApplicationControlBar>
        <mx:Button label="Hide Items That Aren't Special" click="hideSpecialItems();" />

        <mx:Button label="Add a Normal Item" click="addNormalItem();" />

        <mx:Button label="Add a Special Item" click="addSpecialItem();" />
    </mx:ApplicationControlBar>

    <mx:ArrayCollection id="items">
        <mx:Array>
            <mx:Object name="item 1" isSpecial="{false}" />
            <mx:Object name="item 2" isSpecial="{false}" />
            <mx:Object name="item 3" isSpecial="{false}" />
            <mx:Object name="item 4" isSpecial="{true}" />
            <mx:Object name="item 5" isSpecial="{true}" />
            <mx:Object name="item 6" isSpecial="{false}" />
        </mx:Array>
    </mx:ArrayCollection>

    <mx:DataGrid dataProvider="{items}" />
</mx:Application>
هل كانت مفيدة؟

المحلول

وfilterFunction لا في الواقع إزالة العناصر من ArrayCollection. هو فقط يخفي لهم من وجهة النظر. لا يزال بإمكانك رؤية كل البنود في الممتلكات ArrayCollection.source.

إذا قمت بإضافة بنود جديدة في حين لا تزال تطبق filterFunction، هم أيضا عرضة للتصفية.

لإزالة بشكل دائم العناصر من قائمة، وتحويله إلى صفيف واستخدام Array#filter.

var newCollection:ArrayCollection = 
    new ArrayCollection(oldCollection.toArray().filter(myFilterFunction))
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top