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: 

Serial No in ALV list

Former Member
0 Kudos
1,537

Hi everyone,

i am gerarating a alv list which gisplays all the PO's that have been half delivered, but i need to add a serial no col for this list, so that if i sort the list on any of the fields the serial no should be intact,

plz help,

Shri Hari

10 REPLIES 10

Former Member
0 Kudos
248

Hi,

You can add an extra field in the output table

DATA: d_index like sy-tabix.

Then while processing

READ TABLE tablename INDEX d_index.

IF sy-subrc = 0.

tablename-serailno = d_index.

tablename-

.

.

.

endif.

Try this one.

Thanks & Regards,

Judith.

0 Kudos
248

sorry i could'nt get u, i have a internal table that has to be put on the report as a ALV list but they also need a Serial .No as the first col. this can be done by taking a extra variable and getting the output is fine, but when the user sorts the list with the P.O then even the S. No being a part of the internal table gets mixed up and the complete report is a mess,

plz help,

Shri Hari.

0 Kudos
248

Hi Shri,

I tried with the sorting and it is not possible to restrict the S.No to get sorted.

If i find any further solution will let u know.

Thanks & Regards,

Judith.

Former Member
0 Kudos
248

Hi Bathula,

Let me welcome you to SDN first...

I just think this is what you are expecting...Try this and let me know if it does not solve your problem...

REPORT ZC1_SORT_ALV_LIST NO STANDARD PAGE HEADING MESSAGE-ID zc1addmsg.

type-pools: slis.

tables: vbak.

data: begin of i_vbak,

v_sno(6) type c,

VBELN like vbak-vbeln,

ERDAT like vbak-erdat,

ERZET like vbak-erzet,

ERNAM like vbak-ernam,

waerk like vbak-waerk,

end of i_vbak.

data: i_vbak_tab like table of i_vbak with header line,

v_count(6) type n.

----


  • ALV-GRID DATA DECLARATION.

----


data: i_fieldcatalog type slis_t_fieldcat_alv, " FIELD CATALOG.

w_fieldcatalog type slis_fieldcat_alv,

i_layout type slis_layout_alv, " LAYOUT DECLARATION.

v_variant type disvariant,

v_repid like sy-repid.

select-options: s_vbeln for vbak-vbeln.

----


  • START-OF-SELECTION.

----


start-of-selection.

perform get_data.

perform insert_serial_number.

perform generate_alv_fieldcatalogue.

perform display_alv_report .

&----


*& Form get_data

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


form get_data.

select vbeln erdat erzet ernam waerk from vbak

into corresponding fields of table i_vbak_tab

where vbeln in s_vbeln.

if sy-subrc <> 0.

message e006.

endif.

endform. " get_data

&----


*& Form insert_serial_number

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


form insert_serial_number.

v_count = 1.

loop at i_vbak_tab.

i_vbak_tab-v_sno = v_count.

modify i_vbak_tab transporting v_sno.

v_count = v_count + 1.

endloop.

endform. " insert_serial_number

&----


*& Form generate_alv_fieldcatalogue

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


form generate_alv_fieldcatalogue.

move sy-repid to v_repid.

i_layout-zebra = 'X'.

i_layout-no_input = 'X'.

i_layout-box_tabname = 'I_VBAK_TAB'.

v_variant-report = v_repid.

call function 'REUSE_ALV_FIELDCATALOG_MERGE'

exporting

i_program_name = v_repid

i_internal_tabname = 'I_VBAK'

i_inclname = v_repid

changing

ct_fieldcat = i_fieldcatalog

exceptions

inconsistent_interface = 1

program_error = 2

others = 3.

if sy-subrc <> 0.

message id sy-msgid type sy-msgty number sy-msgno

with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

endif.

loop at i_fieldcatalog into w_fieldcatalog.

case w_fieldcatalog-fieldname.

when 'V_SNO'.

w_fieldcatalog-seltext_l = 'Serial Number'.

w_fieldcatalog-outputlen = 15 .

endcase.

modify i_fieldcatalog from w_fieldcatalog.

endloop.

endform. " generate_alv_fieldcatalogue

&----


*& Form display_alv_report

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


form display_alv_report.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = v_repid

I_GRID_TITLE = 'Test Program'

IS_LAYOUT = i_layout

IT_FIELDCAT = i_fieldcatalog[]

I_SAVE = 'A'

IS_VARIANT = v_variant

TABLES

t_outtab = i_vbak_tab

EXCEPTIONS

PROGRAM_ERROR = 1

OTHERS = 2

.

IF sy-subrc <> 0.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

endform. " display_alv_report

Cheers...

Kathir~

Former Member
0 Kudos
248

Hi,

I sorted the fields based on VBELN and ERNAM and the serial numbers appeared correctly..

Let me know if you requirement is different..

Cheers..

Kathir~

0 Kudos
248

Dear Balakrishnan,

Firstly i would like thank u for the warm welcome.

The code that u gave executes, but after the report is displayed try sorting the grid by the time field, then the Serial number gets distorted, i need the serial no intact,

with regards,

Shri Hari.

Former Member
0 Kudos
248

Hi,

Are you trying to say that when ever you sort based on a particular field you want the serial numbers to generated from 1 to N based on that particular field? i.e.

S.No |Field-1| Field-2

1 | A | 10

2 | B | 30

Sorting in descending order on field-1

S.No |Field-1| Field-2

1 | B | 30

2 | A | 10

Is this what you are looking for? Can you please clarify?

Cheers

Kathir~

Former Member
0 Kudos
248

Hi,

You can place two custom buttons for sorting fields in ascending order and descending order. When the button is clicked get the OK_CODE and perform the sorting as

case r_ucomm.

when 'ASOR'. "Sorting for ascending order

Sort i_vbak_tab by <selected field>

v_count = 1.

loop at i_vbak_tab.

i_vbak_tab-v_sno = v_count.

modify i_vbak_tab transporting v_sno.

v_count = v_count + 1.

endloop.

after this you can refresh the alv for displaying the grid. Now the internal table is sorted and the serial number are reassigned. Similarly you create for the descending order.

Hope this will be an option you can give a try.

Please let me know if this does not help you..

Cheers

Kathir~

0 Kudos
248

Hi Sri Hari,

I think the earlier logic works fine . If the alv is implemented with Object Oriented concept then this example will be helpful.The user command can be handled before SAP handles it in the event before_user_command of cl_gui_alv_grid and after handling the event you can avoid further processing. I have done the same for the copy of standard program ERGP2020 and handled it .Here I did not go for any custom buttons but handled the standard SAP provided buttons.

And here is the code.......

REPORT ztest LINE-SIZE 90 NO STANDARD PAGE HEADING.

DATA :

t_column TYPE lvc_t_col,

fs_column TYPE lvc_s_col.

DATA: txt_report LIKE dokhl-object. "Reportname für Erläuterungsaufruf

DATA: BEGIN OF mylist OCCURS 20, " Internal table for preparing

sno(4) TYPE n,

zuordnung(8) TYPE c, " the list

belegnr(10) TYPE c,

ba(2) TYPE c,

bldatum TYPE d,

bs(2) TYPE c,

s(4) TYPE c VALUE ' ',

wrg(4) TYPE c,

betrag TYPE p DECIMALS 2,

END OF mylist.

DATA: ok_code LIKE sy-ucomm,

g_container TYPE scrfname VALUE 'LIBS_GRID_CONTROL_CONTAINER',

grid TYPE REF TO cl_gui_alv_grid,

g_custom_container TYPE REF TO cl_gui_custom_container.

DATA: gt_fieldcatalog TYPE lvc_t_fcat,

gs_layout TYPE lvc_s_layo.

DATA: seed1 TYPE i VALUE '123', " Rand number seed 1

seed2 TYPE i VALUE '2345', " Rand number seed 2

seed3 TYPE i VALUE '23'. " Rand number seed 3

*----


*

  • CLASS cls_event_handler DEFINITION

*----


*

*

*----


*

CLASS cls_event_handler DEFINITION.

PUBLIC SECTION .

METHODS :

handle_before_user_command FOR EVENT before_user_command OF

cl_gui_alv_grid IMPORTING e_ucomm.

ENDCLASS. "cls_event_handler DEFINITION

*----


*

  • CLASS cls_event_handler IMPLEMENTATION

*----


*

*

*----


*

CLASS cls_event_handler IMPLEMENTATION.

METHOD handle_before_user_command.

IF e_ucomm EQ '&SORT_ASC' OR e_ucomm EQ '&SORT_DSC'.

CALL METHOD grid->get_selected_columns

IMPORTING

et_index_columns = t_column.

IF sy-subrc EQ 0.

IF t_column[] IS INITIAL.

MESSAGE 'Select one column' TYPE 'I'.

ENDIF.

READ TABLE t_column INDEX 1 INTO fs_column.

IF sy-subrc EQ 0.

IF e_ucomm EQ '&SORT_ASC'.

SORT mylist BY (fs_column-fieldname).

else.

SORT mylist BY (fs_column-fieldname) descending.

ENDIF.

LOOP AT mylist INTO mylist.

MOVE sy-tabix TO mylist-sno.

MODIFY mylist FROM mylist.

ENDLOOP.

CALL METHOD grid->refresh_table_display

EXCEPTIONS

finished = 1

OTHERS = 2.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

CALL METHOD grid->set_table_for_first_display

EXPORTING

is_layout = gs_layout

CHANGING

it_outtab = mylist[]

it_fieldcatalog = gt_fieldcatalog

EXCEPTIONS

invalid_parameter_combination = 1

program_error = 2

too_many_lines = 3

OTHERS = 4.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

ENDIF.

ENDIF.

CALL METHOD grid->set_user_command

EXPORTING

i_ucomm = space.

ENDIF.

ENDMETHOD. "handle_before_user_command

ENDCLASS. "cls_event_handler IMPLEMENTATION

************************************************************************

                                                      • Main Program *******************************

************************************************************************

START-OF-SELECTION.

PERFORM fill_itab.

PERFORM fieldcatalog_init USING gt_fieldcatalog[].

PERFORM layout_init USING gs_layout.

PERFORM main.

************************************************************************

                                                      • Form Routines ******************************

************************************************************************

*----


*

  • FORM MAIN *

*----


*

  • Main output routine *

*----


*

FORM main.

CALL SCREEN 100.

ENDFORM. "main

*----


*

  • MODULE PBO OUTPUT *

*----


*

MODULE pbo OUTPUT.

SET PF-STATUS 'MAIN1'.

SET TITLEBAR 'MAIN100'.

IF g_custom_container IS INITIAL.

CREATE OBJECT g_custom_container

EXPORTING container_name = g_container.

CREATE OBJECT grid

EXPORTING i_parent = g_custom_container.

DATA :

obj_event TYPE REF TO cls_event_handler.

CREATE OBJECT obj_event.

SET HANDLER obj_event->handle_before_user_command FOR grid.

CALL METHOD grid->set_table_for_first_display

EXPORTING

is_layout = gs_layout

CHANGING

it_outtab = mylist[]

it_fieldcatalog = gt_fieldcatalog

EXCEPTIONS

invalid_parameter_combination = 1

program_error = 2

too_many_lines = 3

OTHERS = 4.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

ENDIF.

ENDMODULE. "pbo OUTPUT

*----


*

  • MODULE PAI INPUT *

*----


*

MODULE pai INPUT.

  • to react on oi_custom_events:

CALL METHOD cl_gui_cfw=>dispatch.

CASE ok_code.

WHEN '&F03' OR '&F15' OR '&F12'.

PERFORM exit_program.

WHEN 'ERLE'.

txt_report = 'ERGP2020'.

CALL FUNCTION 'ERGO_TEXT_SHOW'

EXPORTING

textname = txt_report

id = 'RE'

langu = sy-langu.

WHEN OTHERS.

  • do nothing

ENDCASE.

CLEAR ok_code.

ENDMODULE. "pai INPUT

*----


*

  • FORM EXIT_PROGRAM *

*----


*

FORM exit_program.

LEAVE PROGRAM.

ENDFORM. "exit_program

*----


*

  • FORM FILL_ITAB *

*----


*

  • Fills the internal table for the list output *

*----


*

FORM fill_itab.

DATA: ran TYPE f,

rani TYPE i,

ranp TYPE p DECIMALS 2,

belnr TYPE i VALUE 1600000040,

date TYPE d.

date = '19920417'.

DO 18 TIMES.

MOVE '19920316' TO mylist-zuordnung.

ADD 1 TO belnr.

MOVE belnr TO mylist-belegnr.

MOVE 'DA' TO mylist-ba.

PERFORM random(ergphelp) USING seed1 seed2 seed3 ran.

rani = 10 * ran.

IF rani > 7.

ADD 1 TO date.

ENDIF.

MOVE date TO mylist-bldatum.

MOVE '01' TO mylist-bs.

PERFORM random(ergphelp) USING seed1 seed2 seed3 ran.

rani = 10 * ran.

IF rani > 7.

MOVE text-w10 TO mylist-wrg. "USD

ELSE.

MOVE text-w20 TO mylist-wrg. "DM

ENDIF.

PERFORM random(ergphelp) USING seed1 seed2 seed3 ran.

ranp = 1000000 * ran.

IF mylist-wrg = text-w10.

ranp = ranp / 2.

ENDIF.

MOVE ranp TO mylist-betrag.

MOVE sy-index TO mylist-sno.

APPEND mylist.

ENDDO.

ENDFORM. "fill_itab

*----


*

  • FORM FIELDCAT_INIT

*

*----


*

FORM fieldcatalog_init USING lt_fieldcatalog TYPE lvc_t_fcat.

DATA: ls_fieldcatalog TYPE lvc_s_fcat.

CLEAR ls_fieldcatalog.

ls_fieldcatalog-fieldname = 'SNO'.

ls_fieldcatalog-datatype = 'N'.

ls_fieldcatalog-reptext = text-u99.

ls_fieldcatalog-coltext = text-u99.

ls_fieldcatalog-seltext = text-u99.

ls_fieldcatalog-tooltip = text-u99.

ls_fieldcatalog-key = 'X'.

APPEND ls_fieldcatalog TO lt_fieldcatalog.

CLEAR ls_fieldcatalog.

ls_fieldcatalog-fieldname = 'ZUORDNUNG'.

ls_fieldcatalog-datatype = 'C'.

ls_fieldcatalog-reptext = text-u10.

ls_fieldcatalog-coltext = text-u10.

ls_fieldcatalog-seltext = text-u10.

ls_fieldcatalog-tooltip = text-u10.

ls_fieldcatalog-key = 'X'.

APPEND ls_fieldcatalog TO lt_fieldcatalog.

CLEAR ls_fieldcatalog.

ls_fieldcatalog-fieldname = 'BELEGNR'.

ls_fieldcatalog-datatype = 'C'.

ls_fieldcatalog-reptext = text-u20.

ls_fieldcatalog-coltext = text-u20.

ls_fieldcatalog-seltext = text-u20.

ls_fieldcatalog-tooltip = text-u20.

ls_fieldcatalog-key = 'X'.

APPEND ls_fieldcatalog TO lt_fieldcatalog.

CLEAR ls_fieldcatalog.

ls_fieldcatalog-fieldname = 'BA'.

ls_fieldcatalog-datatype = 'C'.

ls_fieldcatalog-reptext = text-u30.

ls_fieldcatalog-coltext = text-u30.

ls_fieldcatalog-seltext = text-u30.

ls_fieldcatalog-tooltip = text-u30.

APPEND ls_fieldcatalog TO lt_fieldcatalog.

CLEAR ls_fieldcatalog.

ls_fieldcatalog-fieldname = 'BLDATUM'.

ls_fieldcatalog-datatype = 'DATS'.

ls_fieldcatalog-reptext = text-u40.

ls_fieldcatalog-coltext = text-u40.

ls_fieldcatalog-seltext = text-u40.

ls_fieldcatalog-tooltip = text-u40.

APPEND ls_fieldcatalog TO lt_fieldcatalog.

CLEAR ls_fieldcatalog.

ls_fieldcatalog-fieldname = 'BS'.

ls_fieldcatalog-datatype = 'C'.

ls_fieldcatalog-reptext = text-u50.

ls_fieldcatalog-coltext = text-u50.

ls_fieldcatalog-seltext = text-u50.

ls_fieldcatalog-tooltip = text-u50.

APPEND ls_fieldcatalog TO lt_fieldcatalog.

CLEAR ls_fieldcatalog.

ls_fieldcatalog-fieldname = 'S'.

ls_fieldcatalog-datatype = 'C'.

ls_fieldcatalog-reptext = text-u60.

ls_fieldcatalog-coltext = text-u60.

ls_fieldcatalog-seltext = text-u60.

ls_fieldcatalog-tooltip = text-u60.

APPEND ls_fieldcatalog TO lt_fieldcatalog.

CLEAR ls_fieldcatalog.

ls_fieldcatalog-fieldname = 'WRG'.

ls_fieldcatalog-datatype = 'C'.

ls_fieldcatalog-reptext = text-u70.

ls_fieldcatalog-coltext = text-u70.

ls_fieldcatalog-seltext = text-u70.

ls_fieldcatalog-tooltip = text-u70.

APPEND ls_fieldcatalog TO lt_fieldcatalog.

CLEAR ls_fieldcatalog.

ls_fieldcatalog-fieldname = 'BETRAG'.

ls_fieldcatalog-datatype = 'CURR'.

ls_fieldcatalog-outputlen = '15'.

ls_fieldcatalog-reptext = text-u80.

ls_fieldcatalog-coltext = text-u80.

ls_fieldcatalog-seltext = text-u80.

ls_fieldcatalog-tooltip = text-u80.

ls_fieldcatalog-cfieldname = 'WRG'.

APPEND ls_fieldcatalog TO lt_fieldcatalog.

CLEAR ls_fieldcatalog.

ENDFORM. "fieldcatalog_init

*----


*

  • FORM layout_init *

*----


*

  • ........ *

*----


*

  • --> LS_LAYOUT *

*----


*

FORM layout_init USING ls_layout TYPE lvc_s_layo.

  • ls_layout-no_toolbar = 'X'.

ls_layout-zebra = 'X'.

ENDFORM. "layout_init

jayanthi_jayaraman
Active Contributor
0 Kudos
248

Hi,

This is the sample code which I am having for refreshing internal table after every 5 seconds.

Try whether it is useful.

>********************************************************************

  • This report displays User's info (SM04) using the FM : *

  • REUSE_ALV_LIST_DISPLAY *

  • The list is auto-refreshed (refresh time : 5 seconds) *

----


TYPE-POOLS: slis. " ALV Global Types

DATA :

gt_user LIKE uinfo OCCURS 0 WITH HEADER LINE. " User info in SM04

----


START-OF-SELECTION.

PERFORM f_read_data.

PERFORM f_display_data.

----


  • Form F_LIRE_DATA

----


FORM f_read_data.

REFRESH gt_user.

  • Get User's info

CALL FUNCTION 'THUSRINFO'

TABLES

usr_tabl = gt_user.

  • Wait in a task

PERFORM f_call_rfc_wait.

ENDFORM. " F_READ_DATA

----


  • Form F_DISPLAY_DATA

----


FORM f_display_data.

DEFINE m_sort.

add 1 to ls_sort-spos.

ls_sort-fieldname = &1.

append ls_sort to lt_sort.

END-OF-DEFINITION.

DEFINE m_event_exit.

clear ls_event_exit.

ls_event_exit-ucomm = &1.

ls_event_exit-after = 'X'.

append ls_event_exit to lt_event_exit.

END-OF-DEFINITION.

DATA :

ls_layout TYPE slis_layout_alv,

lt_sort TYPE slis_t_sortinfo_alv,

ls_sort TYPE slis_sortinfo_alv,

lt_event_exit TYPE slis_t_event_exit,

ls_event_exit TYPE slis_event_exit.

  • Build Sort Table

m_sort 'ZEIT'.

  • Build Event Exit Table

m_event_exit '&NTE'. " Refresh

ls_layout-zebra = 'X'.

ls_layout-colwidth_optimize = 'X'.

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

i_callback_program = sy-cprog

i_callback_user_command = 'USER_COMMAND'

is_layout = ls_layout

i_structure_name = 'UINFO'

it_sort = lt_sort

it_event_exit = lt_event_exit

TABLES

t_outtab = gt_user.

ENDFORM. " F_DISPLAY_DATA

----


  • FORM USER_COMMAND *

----


FORM user_command USING i_ucomm TYPE syucomm

is_selfield TYPE slis_selfield. "#EC CALLED

CASE i_ucomm.

WHEN '&NTE'.

PERFORM f_read_data.

is_selfield-refresh = 'X'.

SET USER-COMMAND '&OPT'. " Optimize columns width

ENDCASE.

ENDFORM. " USER_COMMAND

----


  • Form F_CALL_RFC_WAIT

----


FORM f_call_rfc_wait.

DATA lv_mssg(80). "#EC NEEDED

  • Wait in a task

CALL FUNCTION 'RFC_PING_AND_WAIT' STARTING NEW TASK '001'

PERFORMING f_task_end ON END OF TASK

EXPORTING

seconds = 5 " Refresh time

busy_waiting = space

EXCEPTIONS

RESOURCE_FAILURE = 1

communication_failure = 2 MESSAGE lv_mssg

system_failure = 3 MESSAGE lv_mssg

OTHERS = 4.

ENDFORM. " F_CALL_RFC_WAIT

----


  • Form F_TASK_END

----


FORM f_task_end USING u_taskname.

DATA lv_mssg(80). "#EC NEEDED

  • Receiving task results

RECEIVE RESULTS FROM FUNCTION 'RFC_PING_AND_WAIT'

EXCEPTIONS

RESOURCE_FAILURE = 1

communication_failure = 2 MESSAGE lv_mssg

system_failure = 3 MESSAGE lv_mssg

OTHERS = 4.

CHECK sy-subrc EQ 0.

SET USER-COMMAND '&NTE'. " Refresh

ENDFORM. " F_TASK_END

Regards,

J.Jayanthi