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.
Showing results for 
Search instead for 
Did you mean: 

Page Number in ALV

Former Member
0 Kudos

HI ,

i need to display page numbers in ALV header like

Page Number: X/Y (shown in the case of printing)

(X: current page number, Y: total page number)

so tell me the procedure to display .



Former Member
0 Kudos

Check the following code:

 call function 'REUSE_ALV_GRID_DISPLAY'
            i_callback_program      = gd_repid
            i_callback_top_of_page   = 'TOP-OF-PAGE'  "see FORM
            is_layout               = gd_layout
            it_fieldcat             = fieldcatalog[]
            i_save                  = 'X'
            t_outtab                = it_ekko
            program_error           = 1
            others                  = 2.

* Form  TOP-OF-PAGE                                                 *
* ALV Report Header                                                 *
Form top-of-page.
*ALV Header declarations
data: t_header type slis_t_listheader,
      wa_header type slis_listheader,
      t_line like wa_header-info,
      ld_lines type i,
      ld_linesc(10) type c.

* Title
  wa_header-typ  = 'H'.
  wa_header-info = 'Page No: '.
  concatenate wa_header-info sy-PAGNO into wa_header-info.
  append wa_header to t_header.
  clear wa_header.

            it_list_commentary = t_header.
*            i_logo             = 'Z_LOGO'.

Former Member
0 Kudos

Step 1: Display each new document in a new page

To achieve this below routine is written to populate the IT_SORT table parameter of ALV function module REUSE_ALV_GRID_DISPLAY. And ALV will automatically insert page break whenever a new document is encountered.


c_spos TYPE SLIS_SPOS VALUE '01', " Column position

c_up TYPE SLIS_SOUP VALUE 'X', " Sorting order

c_table TYPE tabname VALUE 'I_FINAL'," Name of o/p table

c_group TYPE SLIS_CTRLS VALUE '*', " Sorting group

c_fld2 TYPE fieldname VALUE 'IDCNGA04-IDCN037'." Field name(Document no)* Prepare sort table

PERFORM prepare_sort_table CHANGING i_sort[].FORM prepare_sort_table CHANGING pi_sort TYPE slis_t_sortinfo_alv. CLEAR st_sort.

st_sort-spos = c_spos. " Sort sequence

st_sort-fieldname = c_fld2. " Document no

st_sort-tabname = c_table. " Table name

st_sort-up = c_up. " Ascending

st_sort-group = c_group. " Group* Populate sort table

APPEND st_sort TO pi_sort.ENDFORM. "prepare_sort_tableStep 2: Reset page number to 1 for each new document

From ALV function module we cannot control the page numbers as it will automatically increment the page counter whenever a new page is printed. So to achieve this we have written our custom code within the TOP_OF_PAGE routine.

Register TOP_OF_PAGE routine with the TOP_OF_PAGE event in IT_EVENTS table parameter of the ALV function module.

  • Prepare event table

PERFORM prepare_event_table.

FORM prepare_event_table .* Populate top-of-page event

st_event-name = slis_ev_top_of_page.

st_event-form = slis_ev_top_of_page.

APPEND st_event TO i_event. CLEAR st_event.ENDFORM. " prepare_event_table Custom code to control the page number in TOP_OF_PAGE routine

Code block marked in bold are used to control the page number. As in our case we are using ALV grid to display the report so during report display page number is not visible. It is only visible in print preview or at the time of printing.

FORM top_of_page.* Local variable declaration

STATICS: l_comm TYPE syucomm. " Store user command* Declaration of local variables

DATA: l_line_size TYPE sylinsz, " Line size

l_line TYPE slis_listheader, " Hold list header

l_currdoc TYPE idcn037, " Current doc.

l_currtabix TYPE sytabix. " Current index* Check for print or print preview

IF ( sy-ucomm = c_prin OR

sy-ucomm = c_rnt_prev OR

sy-ucomm = c_rnt ). IF l_comm <> sy-ucomm.

CLEAR : g_page_cnt. ENDIF. " l_comm <> sy-ucomm* Store current table index

l_currtabix = sy-tabix.

CLEAR st_final.* If current index is 1 then start page numbering from 1

IF l_currtabix = 1.* Read 1st record and store the document no

READ TABLE i_final INDEX l_currtabix INTO st_final.

g_prevdoc = st_final-idcnga04-idcn037.* Start page numbering from 1

g_page_cnt = 1.

g_prevtabix = 1. ELSE.* Read the table line

READ TABLE i_final INDEX l_currtabix INTO st_final.* Store the current document

l_currdoc = st_final-idcnga04-idcn037.* If the current doc. is same as previou doc.

  • increament the page no, otherwise start it from 1

IF l_currdoc = g_prevdoc.* Increament the page no

g_page_cnt = g_page_cnt + 1.

ELSE.* Start page from 1

g_page_cnt = 1.* Store current doc. as previous doc.

g_prevdoc = l_currdoc. ENDIF. " l_currdoc = g_prevdoc ENDIF. " l_currtabix = 1 ENDIF. " sy-ucomm = c_prin OR IF g_page_cnt = 1.* Store the user command

l_comm = sy-ucomm. ENDIF. " g_page_cnt = 1* Display page no

IF g_page_cnt > 0 .* Store the report width

l_line_size = sy-linsz.* Calculate position

l_line_size = l_line_size - 10.* Display page no

WRITE AT l_line_size 'Page no:'(021).* Calculate position

l_line_size = l_line_size + 5. WRITE AT l_line_size g_page_cnt. ENDIF. " g_page_cnt > 0 REFRESH st_list_top_of_page.* Populate company name

CLEAR l_line.

l_line-typ = c_typ.

l_line-info = g_comp_name.

APPEND l_line TO st_list_top_of_page.* Populate heading

CLEAR l_line.

l_line-typ = c_typ.

l_line-info = text-020.

APPEND l_line TO st_list_top_of_page.* At the time of printing or print preview display additional info

  • on 1st page

IF g_page_cnt = 1 AND

( sy-ucomm = c_prin OR

sy-ucomm = c_rnt_prev OR

sy-ucomm = c_rnt ). CLEAR l_line.

l_line-typ = 'A'.* Populate Staff name



INTO l_line-info


APPEND l_line TO st_list_top_of_page. CLEAR l_line.

l_line-typ = 'A'.* Populate Accountant



INTO l_line-info

SEPARATED BY ':'. APPEND l_line TO st_list_top_of_page. CLEAR l_line.

l_line-typ = 'A'.* Populate Book keeper



INTO l_line-info

SEPARATED BY ':'. APPEND l_line TO st_list_top_of_page. l_line-typ = 'A'.

l_line-info = space.

APPEND l_line TO st_list_top_of_page. ENDIF.* Display list header



i_logo = space

it_list_commentary = st_list_top_of_page.* No sy-subrc check is requiredENDFORM. " top_of_pageStep 3: Display report

Display the ALV report using 'REUSE_ALV_GRID_DISPLAY' function module.



i_callback_program = l_prog " Call back progrm

it_fieldcat = i_fieldcat_msg " Field catalog

is_layout = st_layout " Layout

it_sort = i_sort[] " Sort table

i_save = c_save " 'A'

is_variant = l_variant " ALV variant

it_events = i_event " ALV events

is_print = st_print " Print parameters


t_outtab = i_final " Output table


program_error = 1