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: 

How to create a button in ALV grid.

Former Member
0 Kudos

How to create a button in a ALV grid? How can I see the function codes of the buttons present in ALv grid?

3 REPLIES 3

Former Member
0 Kudos

Hi,

Go to Screen GUI in Systems and Double click to GUI status.

Then check the codes.

also please have a look at demoprogram

SALV_DEMO_TABLE_FUNCTIONS

BCALV_GRID_05

Reward if useful!

Former Member
0 Kudos

Hi

Pushbuttons On The List

To make a cell to be displayed as a pushbutton, we have two steps. Firstly, insert a new inner table of type “LVC_T_STYL” into your list data table.

Code Part 25 – Inserting an inner table to store cell display styles

*--- Internal table holding list data

DATA BEGIN OF gt_list OCCURS 0 .

INCLUDE STRUCTURE SFLIGHT .

DATA rowcolor(4) TYPE c .

DATA cellcolors TYPE lvc_t_scol .

DATA carrid_handle TYPE int4 .

DATA connid_handle TYPE int4 .

DATA cellstyles TYPE lvc_t_styl .

DATA END OF gtlist .

Fill this inner table for each field to be displayed as pushbutton.

Code Part 26 - A sample code to make the cell at row 7 and column ‘SEATSMAX’ displayed as pushbutton

DATA ls_style TYPE lvc_s_styl .

...

READ TABLE gt_list INDEX 7 .

ls_style-fieldname = 'SEATSMAX' .

ls_style-style = cl_gui_alv_grid=>mc_style_button .

APPEND ls_style TO gt_list-cellstyles .

MODIFY gt_list INDEX 7 .

As usual, we state our list data table field related with styles in the layout structure at field ‘STYLEFNAME’.

e.g. ps_layout-stylefname = 'CELLSTYLES' .

Button click event is handled like hotspot click via the event “button_click” through its parameters “es_col_id” and “es_row_no” which contain the address of the clicked pushbutton cell.

Adding Your Own Functions

ALV Grid control has an open door letting you to add your own functions triggered by a button press on the ALV toolbar. For this, we mainly utilize two of ALV Grid events. We use the event “toolbar” to add the button and the event “user_command” to implement the new function.

In the method handling the “toolbar” event, we define a new button by filling a structure and appending it to the table attribute “mt_toolbar” of the object to whose reference we can reach via the parameter “e_object” of the event.

FORM handle_toolbar USING i_object TYPE REF TO cl_alv_event_toolbar_set .

DATA: ls_toolbar TYPE stb_button.

CLEAR ls_toolbar.

MOVE 3 TO ls_toolbar-butn_type.

APPEND ls_toolbar TO i_object->mt_toolbar.

CLEAR ls_toolbar.

MOVE 'PER' TO ls_toolbar-function. "#EC NOTEXT

MOVE icon_display_text TO ls_toolbar-icon.

MOVE 'Passenger Info'(201) TO ls_toolbar-quickinfo.

MOVE 'Passenger Info'(201) TO ls_toolbar-text.

MOVE ' ' TO ls_toolbar-disabled. "#EC NOTEXT

APPEND ls_toolbar TO i_object->mt_toolbar.

CLEAR ls_toolbar.

MOVE 'EXCH' TO ls_toolbar-function. "#EC NOTEXT

MOVE 2 TO ls_toolbar-butn_type.

MOVE icon_calculation TO ls_toolbar-icon.

MOVE 'Payment in Other Currencies'(202) TO ls_toolbar-quickinfo.

MOVE ' ' TO ls_toolbar-text.

MOVE ' ' TO ls_toolbar-disabled. "#EC NOTEXT

APPEND ls_toolbar TO i_object->mt_toolbar.

ENDFORM .

The fields of the structure we fill are as follows:

Field

Description

FUNCTION

The function code for the function

BUTN_TYPE

Button type that will be added to the toolbar. Available button types are:

0

Button (normal)

1

Menu and default button

2

Menu

3

Separator

4

Radio button

5

Checkbox

6

Menu entry

ICON

Icon for the button (optional)

TEXT

Text for the button (optional)

QUICKINFO

Quick info for the button (optional)

DISABLED

Adds the button as disabled

In the Code Part 22, we are adding a separator line and two buttons one of which is a normal button whereas the other is a menu button. To handle a menu button which as added by choosing ‘1’ or ‘2’ as the button type, we must also implement some coding at the method handling the event “menu_button” to define functions as to be subentries. The functions of these subentries are also handled under the event “user_command”.

FORM handle_menu_button USING i_object TYPE REF TO cl_ctmenu

i_ucomm TYPE syucomm .

CASE i_ucomm .

WHEN 'EXCH' .

CALL METHOD i_object->add_function

EXPORTING

fcode = 'EU'

text = 'Euro' .

CALL METHOD i_object->add_function

EXPORTING

fcode = 'TRL'

text = 'Turkish Lira' .

.. ..

ENDCASE .

ENDFORM. " handle_menu_button

Now, to implement what to be executed when our button is pressed or our subentry is selected we need to program our functions in the method handling the event “user_command”.

FORM handle_user_command USING i_ucomm TYPE syucomm .

DATA lt_selected_rows TYPE lvc_t_roid .

DATA ls_selected_row TYPE lvc_s_roid .

CALL METHOD gr_alvgrid->get_selected_rows

IMPORTING

et_row_no = lt_selected_rows .

READ TABLE lt_selected_rows INTO ls_selected_row INDEX 1 .

IF sy-subrc ne 0 .

MESSAGE s000(su) WITH 'Select a row!'(203) .

ENDIF .

CASE i_ucomm .

WHEN 'CAR' .

READ TABLE gt_list INDEX ls_selected_row-row_id .

IF sy-subrc = 0 .

CALL FUNCTION 'ZDISPLAY_CARRIER_INFO'

EXPORTING carrid = gt_list-carrid

EXCEPTIONS carrier_not_found = 1

OTHERS = 2.

IF sy-subrc NE 0 .

*--Exception handling

ENDIF .

ENDIF .

WHEN 'EU' .

READ TABLE gt_list INDEX ls_selected_row-row_id .

IF sy-subrc = 0 .

CALL FUNCTION 'ZPOPUP_CONV_CURR_AND_DISPLAY'

EXPORTING monun = 'EU'

quant = gt_list-paymentsum.

ENDIF .

.. ..

ENDCASE .

ENDFORM .

As you can see, we are using the method “get_selected_rows” to get which row is selected. Since the button with function ‘EXCH’ branches into subfunctions, we do not implement anything for it. Instead, we implement functional codes for subfunctions.

After all, to make ALV show our additional buttons, we must call the method “set_toolbar_interactive” for the ALV Grid instance after the instance is created.

e.g. CALL METHOD gr_alvgrid->set_toolbar_interactive .

Regards

Preeti Yadav

<b><i>

Reward if useful</i></b>

Former Member
0 Kudos

11. How can I display an icon in ALV? (Common requirement is traffic light icon).

http://www.sapfans.com/forums/viewtopic.php?t=79424

http://www.sapfans.com/forums/viewtopic.php?t=24512