Disabling List Selection

Recently a couple of FlexCoders asked about how to prevent certain entries from being selected in a List or Tree or ComboBox. I cobbled this together. Usual caveats apply. Hopefully it will help.
Run Demo
Download Source

20 Responses to Disabling List Selection

  1. Hi Alex,
    This is just a general question, not on lists.
    But does flex have any way to read files syncronously?
    You see, I am developing an app that really needs to read a lot of small files, in order. Having to spawn off a new event for each and every file totally will make my code into a mess.
    The files will be read locally. I just want to read my files syncronously.
    Is this possible within flex? I tried making a busy loop that checked for a flag set by the loaded event, but it seemed like the loop froze the event! Catch-22!
    I don’t suppose you know of some clever little insider trick that could be used to allow events to fire from within busy loops, so that I can freeze my code’s execution until the file is loaded? Just like how reading files is supposed to work, normally. Except without the excessive CPU usage that such a loop creates 🙁
    ————–
    There is no synchronous file I/o in the flash player. Note also that in general, a server-based app cannot access local files on the client computer. If you use Apollo/AIR, you should get much better file i/o performance, but web-based stuff will still be asynch.
    Generally, you do not loop waiting for file i/o to finish. Listen for the COMPLETE event instead.

  2. Werbeagentur says:

    Thanks alex for the interesting informations. The Download Link is very usefully and works excellent. Thanks for share it! Werbeagentur

  3. Thanks Alex for the Demo and Download link… Very usefully and helps very great 😉
    Thanks
    @ http://www.medienstern.de/werbeagentur/

  4. Weimin Cai says:

    Hi Alex,
    I’m seeking the disabling combobox. Your posting give me some ideals. And now, I have implmented the disabling ComboBox and List using my method.
    Your blog is very helpful for me.Thanks.
    I have put my disabling ComboBox to my blog http://wmcai.blog.163.com/blog/static/4802420088945053961/

  5. Theo says:

    I am looking for something like this for a datagrid.
    ————————
    Alex responds:
    You should be able to apply the same principles to DataGrid subclass

  6. Jude says:

    I have a problem with this code… When I add more items to the list, the keyboard selection goes crazy (I think it’s because of the vertical scrollbar).
    Thanks a lot!
    ——————-
    Alex responds:
    Well, like I say on every post, usual caveats apply. These examples are unsupported and not fully tested. If you find the solution feel free to post it. You can also ask on FlexCoders as someone may have already found a solution.

  7. Sreekanth says:

    Hi Alex,
    help me out from this problem,I am using a dataGrid in that one checkbox row is there,when i select that checkbox the entire row has to be disable means i cant edit that row.Any suggestions regording on this.
    Regards
    Sreekanth K
    ——————-
    Alex responds:
    You should be storing the disabled state either in the dataProvider or in some other map, and all other editors should be looking at that spot to determine their enabled state.

  8. Philip Wilder says:

    Hi Alex,
    I’m afraid I don’t know what your “usual caveats” are. If If I was guessing it would be that the code is “free for use anywhere (including commercial applications) but use at your own risk”. Is this correct? If so you might consider adding a link to your “Usual caveats” in future blog posts.
    Anyway, very helpful. Thanks,
    Philip Wilder
    ————
    Alex responds:
    That is a pretty good description of the usual caveats. Maybe I will formalize that someday.

  9. Wei says:

    The sample code works well. Does the same behavior can be achieved by override selectItem method in ListBase?

  10. Alex Harui says:

    I doubt it, but you’re welcome to try.

  11. Nick DeLong says:

    I added the following override of drawItem to draw the disabled items as grayed out:
    override protected function drawItem(item:IListItemRenderer, selected:Boolean = false, highlighted:Boolean = false, caret:Boolean = false, transition:Boolean = false):void {
    super.drawItem(item, selected, highlighted, caret, transition);
    /* Set the font color based on the item price. */
    if (disabledFunction(item.data)) {
    item.enabled = false;
    }
    }

  12. Ade says:

    Thanks Alex, sometimes the hardest part is know which hooks to use. In this case the hooks being
    mouseEventToItemRenderer and moveSelectionVertically.
    In my case I needed the selection via up/down keys to halt when a disabled item was encountered. On the off chance it helps anyone, here’s my solution based on Alexs’ solution.
    override protected function moveSelectionVertically(code:uint, shiftKey:Boolean, ctrlKey:Boolean):void
    {
    var listItem:IListItemRenderer = code == Keyboard.DOWN ? listItems[selectedIndex + 1][0] : listItems[selectedIndex – 1][0];
    var itemData:Object;
    if( listItem != null )
    itemData = listItem.data;
    if( disabledFunction == null || !disabledFunction(itemData) )
    super.moveSelectionVertically(code, shiftKey, ctrlKey);
    }

  13. DP says:

    Can we have drag and drop along with disabling a given list node? I am unable to make drag and drop work for non slectable nodes and was wondering if there was a way out?

  14. Alex Harui says:

    There is probably a way, but it might take a different strategy for disabling nodes. You might want to ask on one of the forums.

  15. Jason Rego says:

    I’m trying to do this exact thing, but I need the shift select functionality and I’m in an advanced data grid using Hierarchical data. What I’m doing is overriding the SelectItem method of the ADG, performing the super SelectItem method to get selectedItems populated, then running the following “filter” function:

    private function selectionFilter():void
    {
    var newSelectedItems:Array= new Array();

    for (var i:int = 0; i < selectedItems.length; i++)
    {
    if (selectedItems[i].Territory == "Arizona")
    {
    newSelectedItems.push(selectedItems[i]);
    }
    }

    selectedItems = newSelectedItems;
    }

    The problem I'm running into is that this 2nd change to selectedItems seems to dirty the selection list somehow that causes a subsequent shift click call to selectItem() to not generate any additional items, as soon as I perform a selectItem() call which only calls the super selectItem() method (and not the filter function), the next shift click works just fine. I tried adding a validateNow() at the end of my filter function after assigning the new selectedItems array but that didn't accomplish anything. Any ideas?

    • Alex Harui says:

      Programmatically setting selection may reset variables used for shift selection. You might have to save and restore things like anchorBookmark and anchorIndex

  16. Jean Claude says:

    Very helpful. Thanks for sharing this! I’m using in a Tilelist component and work great!

  17. fmaxx says:

    Thanx, this method work with mx:Datagrid.

  18. Henry Rabinowitz says:

    Thanks, Alex, this worked for me with mx:AdvancedDataGrid. A further question. Suppose I’d like to warn the user if they click on a row that they are not allowed to click. What is a good place to insert a function that could pop up a warning message? I tried to put one in mouseEventToItemRenderer() but I see that is not the right place at all — it gets called too often.