March 12, 2009

Flex Cookbook Recipe 5.8 Allow Certain items in a list to be selectable, Doesn’t quite work correctly

Filed under: ActionScript, Bug Fixes, Flex — Anthony @ 11:38 am

Ok I needed to disable Heading elements inside a List Component in Flex, and i was being too lazy to work it out for myself so i picked up the Flex cookbook Adobe had been kind enough to send me and yes... It was there. Now the Mouse removal was straight forward and worked like a charm. But the keyboard events were a different story. The code didn't work for several reasons.
1. Key directions were not being assigned.
2. When a disabled item was at either the top or the bottom of the list it would stop you being able to continue down / up the list.
To fix this i added the following code.

 
/**
* @inheritDoc.
* set our offscreen items to at least 4 so
* that if the last / first visible item
* is disabled we can still move the view
* up or down without getting stuck?
*/
override public function initialize():void
{
   super.initialize( );
   offscreenExtraRowsOrColumns =
   Math.max( 4, offscreenExtraRowsOrColumns );
}
 
/**
* @inheritDoc.
* capture key directions.
*/
override protected function keyDownHandler(event:KeyboardEvent):void
{
  if( event.keyCode == Keyboard.UP )
  {
     _selectionIsAbove = true;
 
  }else if( event.keyCode == Keyboard.DOWN )
  {
    _selectionIsAbove = false;
  }
    super.keyDownHandler( event );
}
 

And then I changed the finishKeySelection( ) method as follows.

 
/**
* @inheritDoc.
* check to see if our selected item is
* allowed to be selected or not.
*/
override protected function finishKeySelection():void
{
  super.finishKeySelection( );
 
  var i : int;
  var rowCount : int = listItems.length;
  var count : Number = 0;
 
  var item : IListItemRenderer = listItems[ caretIndex -
		verticalScrollPosition + offscreenExtraRowsTop ][ 0 ];
 
  if( item )
  {
    if( item.data )
    {
      if( disabledFilterFunction( item.data ) )
      {
	//trace( 'ITEM IS DISABLED : ' + disabledFilterFunction( item.data ) );
	//trace( 'SELECTION IS ABOVE : ' + _selectionIsAbove );
	var currIndex : int = caretIndex - verticalScrollPosition;
	if( _selectionIsAbove )
	{
	  //LookUp
	  i = currIndex + offscreenExtraRowsTop;
	  count = 0;
	  while( i > 0 )
	  {
	    item = listItems[ i ][ 0 ];
	    if( !disabledFilterFunction( item.data ) )
	    {
	      selectedIndex = selectedIndex + count;
	      return;
	    }
	      count --;
	      i--;
	    }
	    selectedIndex = selectedIndex +1;
	    //trace( 'CANT GO UP ANYMORE' );
	    return;
 
	    }else{
	    //Look down.
	    i = currIndex + offscreenExtraRowsTop;
	    count = 0;
	    while( i < rowCount )
	    {
	      item = listItems[ i ][ 0 ];
	      if( !disabledFilterFunction( item.data ) )
	      {
		selectedIndex = selectedIndex + count;
		return;
	      }
	      count ++;
	      i++;
	    }
 
	    selectedIndex = selectedIndex -1;
	    //trace( 'CANT GO DOWN ANYMORE' );
	    return;
        }
      }
    }
  }
}
 

I dont know if this will help anyone but im sure glad i got it working.

No Comments »

No comments yet.

RSS feed for comments on this post. | TrackBack URI

Leave a comment

XHTML ( You can use these tags): <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .