4

Is there an easy way of retrieving the ALV data that is displayed when there are also filters used on that ALV?

The ALV used is an object of CL_GUI_ALV_GRID. When showing it to the user, there is a filter placed on it by default. The user also has a button that processes the data in the ALV. How can I make sure the process only works with the data that is displayed, even if the user places his own filters on the ALV?

e.g: An ALV gets created from an itab that has 10 rows, but because there is also a filter passed on the ALV, only 8 rows are showing. When pressing a button, I only want to work with the 8 rows currently showing to the user.

I have tried finding a function module for this purpose but I can only find a FM which works with the selected rows in an ALV.

EDIT: Further, there is a method called get_filtered_entries, but it only retrieves those entries that are NOT displayed. Using this will be quite time-consuming to make the translation to displayed entries. get_filtered_entries

Thanks in advance.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Erik
  • 361
  • 5
  • 16

1 Answers1

5

GET_FILTERED_ENTRIES returns a table of excluded row indices. You just have to skip those in your processing.

" Copy original table
DATA(lit_buffer) = it_out[]. 

" Get excluded rows
o_grid->get_filtered_entries(
  IMPORTING
    et_filtered_entries = DATA(lit_index)
).

" Reverse order to keep correct indizes; thnx futu
SORT lit_index DESCENDING.

" Remove excluded rows from buffer
LOOP AT lit_index ASSIGNING FIELD-SYMBOL(<index>).
  DELETE lit_buffer INDEX <index>.
ENDLOOP.

EDIT: I debugged cl_gui_alv_grid a little and it doesn't seems like that a filtered version of the original table exists at all. The lines get filtered, sorted, grouped and immediately transferred into a table of cells. Looks like it is nearly impossible to get the displayed rows without a performance drawback.

  • What would you say is the most efficient way? Load all the non-displayed entries in an itab, copying the source itab, looping over the copy and deleting the entries that are also present in the non-displayed itab? I need a table of displayed entries that I can pass to other functions. – Erik Sep 19 '17 at 11:16
  • @Erik Copying and modifying the original table seems to be the only option. I've added some code in my answer. I'm also not aware of another way for deleting by a table of indices –  Sep 19 '17 at 11:21
  • @Erik There are also some interesting options under `APPEND` (`LINES OF ... FROM ... TO ...`). I'm gonna test this later –  Sep 19 '17 at 11:26
  • 1
    I implemented the code to test it out. I had to change FROM to INDEX in the delete statement. Works like a charm. Thank you very much! – Erik Sep 19 '17 at 11:48
  • @Erik Ups, you're right. `FROM` would be horrible there ^^ –  Sep 19 '17 at 12:25
  • 3
    I never was brave enough to do it this way... are you sure it works and it does not mess up the index of the table when you loop it and delete entries? Example: lines 1,2,3,4,5. First loop you delete 2. In the second loop your indexes are 1,2,3,4. (3 = 2, 4 = 3, 5 = 4)? Understand my concern? At least I expected that you sort your lit_index DESCENDING...? Can someone clarify this? – futu Sep 19 '17 at 13:06
  • @futu You're right too. It's probably not a good idea to post code which hasn't been tested appropriately. My bad... –  Sep 19 '17 at 13:18
  • okay, I am glad, that it is clear now. For a second I thought SAP is capable of having something implemented to delete in loop operations and keeping indexes stable. What could have been really confusing in other scenarios – futu Sep 19 '17 at 13:31
  • @futu nice catch! – Erik Sep 19 '17 at 14:58