Tuesday, 21 August 2007

ABAP: How to get the Screen Table row that the cursor is in

Getting the row that the user cursor is in accomplished by using the GET CURSOR LINE statement. As always, we need to declare a global field named something like cur_tbl_line to hold the current table line. Then during PAI processing we need to issue statements like the following : (tc_mydata is the name of the screen table control and XXX is the screen number)

MODULE user_command_XXX INPUT.

* set up the current line variable
  GET CURSOR LINE cur_tbl_line.
  cur_tbl_line = tc_mydata-top_line + cur_tbl_line - 1.

* do the classic stuff
  ok_code = ok_code_XXX.
  CLEAR ok_code_XXX.


Using this approach, you can get the selected line of the tbl_mydata table by writing code like this

FORM sync_mydata_with_selection
        f_none_selected TYPE c.

    <fs> LIKE LINE OF tbl_mydata.

  CLEAR f_none_selected.

* give precedence to the table control selection
  READ TABLE tbl_mydata
  WITH KEY sel = 'X'
  ASSIGNING  <fs>.

  IF sy-subrc <> 0.
*   No selection was made so get the current row using the
*   cursor
    IF cur_tbl_line > 0.
      READ TABLE tbl_packages INDEX cur_tbl_line
        ASSIGNING .
      f_none_selected = 'X'.     

After this is executed, the header line of tbl_mydata contains the selected row and if your user has not selected anything then f_none_selected will have a value of 'X'.

No comments :