Tag Archive for 'button'

SAP: Clickable Icon Button in ALV fields


Adding a button inside an ALV fields is a very simple task to do which needs only a few line of codes. what I always do is I create a field on ALV and populate that field with an Icon of my choice and declare it as a Hotspot so it will react on a single click and it works perfectly just like a button.

First Thing to do is to specify a field in our internal table that we will use to hold the Icon(refer to code #1).

 
TYPES: BEGIN OF t_alvbutton,
          icon TYPE string,      "icon field
          kunnr LIKE kna1-kunnr,
	  name1 LIKE kna1-name1,
	END OF t_alvbutton.
 
DATA: it_alvbutton    TYPE STANDARD TABLE OF t_alvbutton,
      wa_alvbutton	TYPE t_alvbutton.

Proceed to the data retrieval and populate the it_alvbutton. I assume you know how to retrieve data from database so there’s no need to show you the select statement for our example. After that, loop on every records of it_alvbutton to specify the Icon that we want to display(refer to code #2).

 
LOOP AT it_alvbutton INTO wa_alvbutton.
	wa_alvbutton-icon =  '@0X@'.
	MODIFY it_alvbutton FROM wa_alvbutton.
ENDLOOP.

@0X@ is the code for print icon. For the complete list of icons, check it out here.

Now the important part is to define a field in ALV that will hold the icon. To show you how simple it is to do that, please refer to code #3.

 
DATA:	ls_fieldcat TYPE slis_fieldcat_alv,
	lt_fieldcat TYPE slis_t_fieldcat_alv.
 
DEFINE m_fieldcat.
	add 1 to ls_fieldcat-col_pos.
	ls_fieldcat-fieldname = &1.
	ls_fieldcat-seltext_l = &2.
	ls_fieldcat-outputlen = &3.
	ls_fieldcat-hotspot = &4.   "X to declare the field as a hotspot
	ls_fieldcat-icon = &5.      "X to declare the field as an icon
	append ls_fieldcat to lt_fieldcat.
END-OF-DEFINITION.
 
m_fieldcat 'ICON' 'Print' '20' 'X' 'X'.  "this specific field is a hotspot and an icon
m_fieldcat 'KUNNR' 'Customer No.' '10'.
m_fieldcat 'NAME1' 'Customer Name' '35'.
m_fieldcat . . .
m_fieldcat . . .
 
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
	. . .
	. . .

The figure below shows an example of print icon as a button.

Icon Button in ALV

Now refer to code #4 to handle the command when a user clicks on the icon button.

 
FORM f_user_command USING ucomm LIKE sy-ucomm
		  v_selfld TYPE slis_selfield.
 
IF ucomm = '&IC1' AND v_selfld-fieldname = 'ICON'.
	"code goes in here when user clicks on our print button
ENDIF.

Creative Commons License