Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

CL_SALV_TABLE number of rows

mucsiattila
Explorer
470

Hi!

I have an ALV created with cl_salv_table, and i need to display the total number of rows in the list header, and refresh it if any filer is applied.
I googled a lot and found only this as working solution: https://community.sap.com/t5/application-development-discussions/oo-salv-cl-salv-table-how-to-count-... 
You loop at the filers, collect them, and apply them to your itab, than count the number of rows in it.

It works but i cant beleive that there is no better way to solve this since 2011.

Can you suggest me a better solution?

(SAP ABAP, release 700)

6 REPLIES 6

Sandra_Rossi
Active Contributor
445

If your SALV is built on an ALV Grid (CL_GUI_ALV_GRID), you may get its instance (search the Web) and use the method GET_FILTERED_ENTRIES.

373

Thank you!

I managed to get the ALV with CF 'GET_GLOBALS_FROM_SLVC_FULLSCR' in the 'after_salv_function' event and get the number I wanted, and display it in the ALV list header with 'set_list_header' method of cl_salv_table display settings.
I don't know if there is a better way to do this, so feel free to  criticize my solution.

(gv_total is is the number of lines in the original table)

 

DATA: go_table  TYPE REF cl_salv_table.
DATA: gv_total  TYPE i,
      gv_totalc TYPE char10.
DATA: lo_events TYPE REF cl_salv_events_table.
lo_events ?= go_table->get_event( ).
SET HANDLER lcl_handle_events=>after_salv_function  FOR lo_events.
METHOD after_salv_function.

  DATA: lo_grid             TYPE REF TO cl_gui_alv_grid,
        lo_display          TYPE REF TO cl_salv_display_settings,
        lt_filtered_entries TYPE lvc_t_fidx,
        lv_count            TYPE i,
        lv_countc           TYPE char10,
        lv_title            TYPE lvc_title.

  IF lo_grid IS INITIAL.
    CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
      IMPORTING
        e_grid = lo_grid.
  ENDIF.

  lo_grid->get_filtered_entries( IMPORTING et_filtered_entries = lt_filtered_entries ).

  DESCRIBE TABLE lt_filtered_entries LINES lv_count.
  lv_countc = gv_total - lv_count.
  CONDENSE lv_countc.
  CONCATENATE lv_countc 'records selected out of' gv_totalc ' total' INTO lv_title SEPARATED BY space.

  lo_display = go_table->get_display_settings( ).
  lo_display->set_list_header( lv_title ).

ENDMETHOD.  

369

It's the shortest solution of all.

No need of "IF lo_grid IS INITIAL" you have defined lo_grid as local so the condition is always true. Even if you declare it global, there's almost no performance gain, so keep the code clean without IF and with lo_grid local.

If you use ABAP >= 7.02, use lv_title = |{ lines( alv_table ) - lines( lv_filtered_entries ) } records selected out of { lines( alv_table ) } total| and remove all these unclear and useless global variables. You may even get rid of lv_title and lo_display (no DATA line at all, except lo_grid).

0 Kudos
242

Unfortunately I need this program to work in ABAP 7.00, but I got rid of 'IF' and some variables, so the final versions seems to be like this:

METHOD after_salv_function.

DATA: lo_grid             TYPE REF TO cl_gui_alv_grid,
	  lo_display          TYPE REF TO cl_salv_display_settings,
	  lt_filtered_entries TYPE        lvc_t_fidx,
	  lv_total            TYPE        char10,
	  lv_filtered         TYPE        char10,
	  lv_title            TYPE        lvc_title.

CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
  IMPORTING
	e_grid = lo_grid.

lo_grid->get_filtered_entries( IMPORTING et_filtered_entries = lt_filtered_entries ).

DESCRIBE TABLE gt_tr_list          LINES lv_total.
DESCRIBE TABLE lt_filtered_entries LINES lv_filtered.

lv_filtered = lv_total - lv_filtered.

CONCATENATE lv_filtered 'records selected out of' lv_total 'total'INTO lv_title SEPARATED BY space.
CONDENSE lv_title.

lo_display = go_table->get_display_settings( ).
lo_display->set_list_header( lv_title ).

ENDMETHOD.

Thanks again!

219

The function LINES exists since ABAP 6.10:

lv_count_displayed_entries = lines( gt_tr_list ) - lines( lt_filtered_entries ).

 

0 Kudos
215

Thank you!

Works like charm!