0 Replies Latest reply on Apr 18, 2007 7:28 AM by dolinja

    Implementing scrolling with arrow keys on a DataGrid?

    dolinja
      Hi all,

      I'm a new developer to Flex and ActionSctript, have been using it for about a month now and am quite enjoying it. My question comes down to trying to implement a feature that will scroll a DataGrid horizontally using the right and left arrow keys when the DataGrid has focus.

      I attached some event handlers to the KeyBoardEvent.KEY_DOWN of the DataGrid and started with a naive implementation:

      switch (event.keyCode) {

      case Keyboard.LEFT:
      horizontalScrollPosition -= 1;
      break;
      case Keyboard.RIGHT:
      horizontalScrollPosition += 1;
      break;
      }

      This works reasonably and scrolls the DataGrid by one column for each keystroke. The problem is that when you scroll to the end of the grid, it can keep scrolling until you get this exception:

      TypeError: Error #1010: A term is undefined and has no properties.
      at mx.controls::DataGrid/::calculateColumnSizes()
      at mx.controls::DataGrid/set horizontalScrollPosition()
      at components::CIDataGrid/::ciGridKeyDownHandler()


      The next attempt was to try and test whether we were outside of the boundaries by not scrolling by keeping the horizontalScrollPosition between 0 and the max number of columns:

      switch (event.keyCode) {

      case Keyboard.LEFT:
      if (horizontalScrollPosition > 0)
      horizontalScrollPosition -= 1;
      break;
      case Keyboard.RIGHT:
      if (horizontalScrollPosition < this.columnCount)
      horizontalScrollPosition += 1;
      break;
      }


      This doesn't work either because when you are scrolled all the way to the right, the horizontalScrollPosition actually doesn't equal the number of columns in the grid, it will be the number of the left most column in the grid. For example, in a DataGrid of 27 columns, when column 27 is visible (ie. we scrolled all the way over), the horizontal scroll position may be something like 16 or 17, depending on column widths.

      So I need to come up with a way to determine when the last column in a DataGrid becomes visible. First thought was to use the DataGrid.visible property, but this seems always set to true. Is that a bug in the Flex framework? Does anyone have any furthers ideas on how to accomplish this scrolling, it really doesn't seem like it should be that tricky of a task?

      Thanks so much!

      Regards,
      Jason