二维码

[ooalv] 在alv中对编辑字段设置F4帮助及下拉框

Twilight发表于 2014-08-19 17:14yny038 最后回复于 2015-04-15 15:49 [复制链接] 9837 2

主要的参数设置:
lv_fldcat-f4availabl = 'X'.
lv_fldcat-ref_table = 'T582A'.
lv_fldcat-ref_field = 'ZEITB'.

程序代码:
  1. *Type pools for ALV
  2. TYPE-POOLS : slis.
  3. *structure for t582a tbale
  4. TYPES : BEGIN OF ty_table,
  5.         infty TYPE infty,
  6.         pnnnn TYPE pnnnn_d,
  7.         zrmkz TYPE dzrmkz,
  8.         zeitb TYPE dzeitb,
  9.         dname TYPE dianm,
  10.          davo TYPE davo,
  11.         davoe TYPE davoe,
  12.         END OF ty_table.
  13. *Structure for infotype text
  14. TYPES : BEGIN OF ty_itext,
  15.         infty TYPE infty,
  16.         itext TYPE intxt,
  17.         sprsl TYPE sprsl,
  18.         END OF ty_itext.
  19. *Structure for output display
  20. TYPES : BEGIN OF ty_output,
  21.         infty TYPE infty,
  22.         itext TYPE intxt,
  23.         pnnnn TYPE pnnnn_d,
  24.         zrmkz TYPE dzrmkz,
  25.         zeitb TYPE dzeitb,
  26.         dname TYPE dianm,
  27.         davo TYPE davo,
  28.         davoe TYPE davoe,
  29.        END OF ty_output.
  30. *internal table and work area declarations
  31. DATA : it_table TYPE STANDARD TABLE OF ty_table INITIAL SIZE 0,
  32.        it_output TYPE STANDARD TABLE OF ty_output INITIAL SIZE 0,
  33.        it_pbo TYPE STANDARD TABLE OF ty_output INITIAL SIZE 0,
  34.        it_ittext TYPE STANDARD TABLE OF ty_itext INITIAL SIZE 0,
  35.        wa_table TYPE ty_table,
  36.        wa_output TYPE ty_output,
  37.        wa_ittext TYPE ty_itext.
  38. *Data declarations for dropdown lists for f4
  39. DATA: it_dropdown TYPE lvc_t_drop,
  40.       ty_dropdown TYPE lvc_s_drop,
  41. *data declaration for refreshing of alv
  42.       stable TYPE lvc_s_stbl.
  43. *Global variable declaration
  44. DATA: gstring TYPE c.

  45. *Data declarations for ALV
  46. DATA: c_ccont TYPE REF TO cl_gui_custom_container,         "Custom container object
  47.       c_alvgd         TYPE REF TO cl_gui_alv_grid,         "ALV grid object
  48.       it_fcat            TYPE lvc_t_fcat,                  "Field catalogue
  49.       it_layout          TYPE lvc_s_layo.                  "Layout
  50. *ok code declaration
  51. DATA:
  52.   ok_code       TYPE ui_func.

  53. *initialization event
  54. INITIALIZATION.
  55. *start of selection event
  56. START-OF-SELECTION.
  57. *select the infotypes maintained
  58.   SELECT infty
  59.           pnnnn
  60.           zrmkz
  61.           zeitb
  62.           dname
  63.           davo
  64.           davoe
  65.           FROM t582a UP TO 10 ROWS
  66.           INTO CORRESPONDING FIELDS OF TABLE it_table.
  67. * *Select the infotype texts
  68.   IF it_table[] IS NOT INITIAL.
  69.     SELECT itext
  70.              infty
  71.              sprsl
  72.              FROM t582s
  73.              INTO CORRESPONDING FIELDS OF TABLE it_ittext
  74.              FOR ALL ENTRIES IN it_table
  75.              WHERE infty = it_table-infty
  76.              AND sprsl = 'E'.
  77.   ENDIF.

  78. *Apppending the data to the internal table of ALV output
  79.   LOOP AT it_table INTO wa_table.
  80.     wa_output-infty = wa_table-infty.
  81.     wa_output-pnnnn = wa_table-pnnnn.
  82.     wa_output-zrmkz = wa_table-zrmkz.
  83.     wa_output-zeitb = wa_table-zeitb.
  84.     wa_output-dname = wa_table-dname.
  85.     wa_output-davo = wa_table-davo.
  86.     wa_output-davoe = wa_table-davoe.
  87. * For texts
  88.     READ TABLE it_ittext INTO wa_ittext WITH KEY infty = wa_table-infty.
  89.     wa_output-itext = wa_ittext-itext.

  90.     APPEND wa_output TO it_output.
  91.     CLEAR wa_output.
  92.   ENDLOOP.
  93. * Calling the ALV screen with custom container
  94.   CALL SCREEN 0600.
  95. *On this statement double click  it takes you to the screen painter SE51.
  96. *Enter the attributes
  97. *Create a Custom container and name it CCONT and OK code as OK_CODE.
  98. *Save check and Activate the screen painter.
  99. *Now a normal screen with number 600 is created which holds the ALV grid.
  100. * PBO of the actual screen ,
  101. * Here we can give a title and customized menus
  102. *create 2 buttons with function code 'SAVE' and 'EXIT'.
  103. * GIVE A SUITABLE TITLE
  104. *&---------------------------------------------------------------------*
  105. *&      Module  STATUS_0600  OUTPUT
  106. *&---------------------------------------------------------------------*
  107. *       text
  108. *----------------------------------------------------------------------*
  109. MODULE status_0600 OUTPUT.
  110.   SET PF-STATUS 'DISP'.
  111. *  SET TITLEBAR 'ALVF4'.
  112. ENDMODULE.                 " STATUS_0600  OUTPUT

  113. * calling the PBO module ALV_GRID.
  114. *&---------------------------------------------------------------------*
  115. *&      Module  PBO  OUTPUT
  116. *&---------------------------------------------------------------------*
  117. *       text
  118. *----------------------------------------------------------------------*
  119. MODULE pbo OUTPUT.
  120. *Creating objects of the container
  121.   CREATE OBJECT c_ccont
  122.        EXPORTING
  123.           container_name = 'CCONT'.
  124. *  create object for alv grid
  125.   create object c_alvgd
  126.   exporting
  127.   i_parent = c_ccont.
  128. *  SET field for ALV
  129.   PERFORM alv_build_fieldcat.
  130. * Set ALV attributes FOR LAYOUT
  131.   PERFORM alv_report_layout.

  132.   CHECK NOT c_alvgd IS INITIAL.
  133. * Call ALV GRID
  134.   CALL METHOD c_alvgd->set_table_for_first_display
  135.     EXPORTING
  136.       is_layout                     = it_layout
  137.       i_save                        = 'A'
  138.     CHANGING
  139.       it_outtab                     = it_output
  140.       it_fieldcatalog               = it_fcat
  141.     EXCEPTIONS
  142.       invalid_parameter_combination = 1
  143.       program_error                 = 2
  144.       too_many_lines                = 3
  145.       OTHERS                        = 4.
  146.   IF sy-subrc <> 0.
  147.     MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
  148.                WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  149.   ENDIF.
  150. ENDMODULE.                 " PBO  OUTPUT
  151. *&---------------------------------------------------------------------*
  152. *&      Form  alv_build_fieldcat
  153. *&---------------------------------------------------------------------*
  154. *       text
  155. *----------------------------------------------------------------------*
  156. *      <--P_IT_FCAT  text
  157. *----------------------------------------------------------------------*
  158. *subroutine to build fieldcat
  159. FORM alv_build_fieldcat.
  160.   DATA lv_fldcat TYPE lvc_s_fcat.
  161.   CLEAR lv_fldcat.
  162.   lv_fldcat-row_pos   = '1'.
  163.   lv_fldcat-col_pos   = '1'.
  164.   lv_fldcat-fieldname = 'INFTY'.
  165.   lv_fldcat-tabname   = 'IT_OUTPUT'.
  166.   lv_fldcat-outputlen = 8.
  167.   lv_fldcat-scrtext_m = 'Infotype'.
  168.   lv_fldcat-icon = 'X'.
  169.   APPEND lv_fldcat TO it_fcat.
  170.   CLEAR lv_fldcat.
  171.   lv_fldcat-row_pos   = '1'.
  172.   lv_fldcat-col_pos   = '2'.
  173.   lv_fldcat-fieldname = 'PNNNN'.
  174.   lv_fldcat-tabname   = 'IT_OUTPUT'.
  175.   lv_fldcat-outputlen = 15.
  176.   lv_fldcat-scrtext_m = 'Structure'.
  177.   lv_fldcat-icon = ''.
  178.   APPEND lv_fldcat TO it_fcat.
  179.   CLEAR lv_fldcat.
  180.   lv_fldcat-row_pos   = '1'.
  181.   lv_fldcat-col_pos   = '3'.
  182.   lv_fldcat-fieldname = 'ITEXT'.
  183.   lv_fldcat-tabname   = 'IT_OUTPUT'.
  184.   lv_fldcat-outputlen = 60.
  185.   lv_fldcat-scrtext_m = 'Description'.
  186.   lv_fldcat-icon = ''.
  187.   APPEND lv_fldcat TO it_fcat.
  188.   CLEAR lv_fldcat.

  189.   lv_fldcat-row_pos   = '1'.
  190.   lv_fldcat-col_pos   = '5'.
  191.   lv_fldcat-fieldname = 'ZRMKZ'.
  192.   lv_fldcat-tabname   = 'IT_OUTPUT'.
  193.   lv_fldcat-outputlen = 1.
  194.   lv_fldcat-scrtext_m = 'PERIOD'.
  195.   lv_fldcat-icon = ''.
  196.   APPEND lv_fldcat TO it_fcat.
  197.   CLEAR lv_fldcat.
  198.   lv_fldcat-row_pos   = '1'.
  199.   lv_fldcat-col_pos   = '6'.
  200.   lv_fldcat-fieldname = 'ZEITB'.
  201.   lv_fldcat-tabname   = 'IT_OUTPUT'.
  202.   lv_fldcat-outputlen = 5.
  203.   lv_fldcat-scrtext_m = 'Time constraint'.
  204.   lv_fldcat-edit = 'X'.
  205. *To avail the existing F4 help these are to
  206. *be given in the field catalogue
  207.   lv_fldcat-f4availabl = 'X'.
  208.   lv_fldcat-ref_table = 'T582A'.
  209.   lv_fldcat-ref_field = 'ZEITB'.
  210.   APPEND lv_fldcat TO it_fcat.
  211.   CLEAR lv_fldcat.
  212.   lv_fldcat-row_pos   = '1'.
  213.   lv_fldcat-col_pos   = '7'.
  214.   lv_fldcat-fieldname = 'DNAME'.
  215.   lv_fldcat-tabname   = 'IT_OUTPUT'.
  216.   lv_fldcat-outputlen = 15.
  217.   lv_fldcat-scrtext_m = 'Dialogmodule'.
  218.   lv_fldcat-icon = ''.
  219.   APPEND lv_fldcat TO it_fcat.
  220.   CLEAR lv_fldcat.
  221.   lv_fldcat-row_pos   = '1'.
  222.   lv_fldcat-col_pos   = '8'.
  223.   lv_fldcat-fieldname = 'DAVO'.
  224.   lv_fldcat-tabname   = 'IT_OUTPUT'.
  225.   lv_fldcat-outputlen = 15.
  226.   lv_fldcat-scrtext_m = 'Start'.
  227.   lv_fldcat-edit = 'X'.
  228.   APPEND lv_fldcat TO it_fcat.
  229.   CLEAR lv_fldcat.
  230.   lv_fldcat-row_pos   = '1'.
  231.   lv_fldcat-col_pos   = '9'.
  232.   lv_fldcat-fieldname = 'DAVOE'.
  233.   lv_fldcat-tabname   = 'IT_OUTPUT'.
  234.   lv_fldcat-outputlen = 15.
  235.   lv_fldcat-scrtext_m = 'End'.
  236.   lv_fldcat-icon = ''.
  237.   APPEND lv_fldcat TO it_fcat.
  238.   CLEAR lv_fldcat.

  239. *To create drop down for the field 'DAVO'
  240. * with our own f4 help
  241.   ty_dropdown-handle = '1'.
  242.   ty_dropdown-value = ' '.
  243.   APPEND ty_dropdown TO it_dropdown.
  244.   ty_dropdown-handle = '1'.
  245.   ty_dropdown-value = '1'.
  246.   APPEND ty_dropdown TO it_dropdown.
  247.   ty_dropdown-handle = '1'.
  248.   ty_dropdown-value = '2'.
  249.   APPEND ty_dropdown TO it_dropdown.
  250.   ty_dropdown-handle = '1'.
  251.   ty_dropdown-value = '3'.
  252.   APPEND ty_dropdown TO it_dropdown.
  253.   CALL METHOD c_alvgd->set_drop_down_table
  254.     EXPORTING
  255.       it_drop_down = it_dropdown.

  256.   LOOP AT it_fcat INTO lv_fldcat.
  257.     CASE lv_fldcat-fieldname.
  258. ** To assign dropdown in the fieldcataogue
  259.       WHEN 'DAVO'.
  260.         lv_fldcat-drdn_hndl = '1'.
  261.         lv_fldcat-outputlen = 15.
  262.         MODIFY it_fcat FROM lv_fldcat.
  263.     ENDCASE.
  264.   ENDLOOP.
  265. ENDFORM.                    " alv_build_fieldcat
  266. *&---------------------------------------------------------------------*
  267. *&      Form  alv_report_layout
  268. *&---------------------------------------------------------------------*
  269. *       text
  270. *----------------------------------------------------------------------*
  271. *      <--P_IT_LAYOUT  text
  272. *----------------------------------------------------------------------*
  273. *Subroutine for setting alv layout
  274. FORM alv_report_layout.
  275.   it_layout-cwidth_opt = 'X'.
  276.   it_layout-col_opt = 'X'.
  277.   it_layout-zebra = 'X'.
  278. ENDFORM.                    " alv_report_layout
  279. * PAI module of the screen created. In case we use an interactive ALV or
  280. *for additional functionalities we can create OK codes
  281. *and based on the user command we can do the coding.
  282. *&---------------------------------------------------------------------*
  283. *&      Module  PAI  INPUT
  284. *&---------------------------------------------------------------------*
  285. *       text
  286. *----------------------------------------------------------------------*
  287. MODULE pai INPUT.
  288. *To change the existing values and refresh the grid
  289. *And only values in the dropdown or in the default
  290. *F4 can be given , else no action takes place for the dropdown
  291. *and error is thrown for the default F4 help and font changes to red
  292. *and on still saving, value is not changed
  293.   c_alvgd->check_changed_data( ).
  294. *Based on the user input
  295. *When user clicks 'SAVE;
  296.   CASE ok_code.
  297.     WHEN 'SAVE'.
  298. *A pop up is called to confirm the saving of changed data
  299.       CALL FUNCTION 'POPUP_TO_CONFIRM'
  300.         EXPORTING
  301.           titlebar       = 'SAVING DATA'
  302.           text_question  = 'Continue?'
  303.           icon_button_1  = 'icon_booking_ok'
  304.         IMPORTING
  305.           answer         = gstring
  306.         EXCEPTIONS
  307.           text_not_found = 1
  308.           OTHERS         = 2.
  309.       IF sy-subrc NE 0.
  310. *       MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  311. *               WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  312.       ENDIF.
  313. *When the User clicks 'YES'
  314.       IF ( gstring = '1' ).
  315.         MESSAGE 'Saved' TYPE 'S'.
  316. *Now the changed data is stored in the it_pbo internal table
  317.         it_pbo = it_output.
  318. *Subroutine to display the ALV with changed data.
  319.         PERFORM redisplay.
  320.       ELSE.
  321. *When user clicks NO or Cancel
  322.         MESSAGE 'Not Saved'  TYPE 'S'.
  323.       ENDIF.
  324. **When the user clicks the 'EXIT; he is out
  325.     WHEN 'EXIT'.
  326.       LEAVE PROGRAM.
  327.   ENDCASE.
  328.   CLEAR: ok_code.
  329. ENDMODULE.                 " PAI  INPUT
  330. *&---------------------------------------------------------------------*
  331. *&      Form  REDISPLAY
  332. *&---------------------------------------------------------------------*
  333. *       text
  334. *----------------------------------------------------------------------*
  335. *  -->  p1        text
  336. *  <--  p2        text
  337. *----------------------------------------------------------------------*
  338. FORM redisplay .
  339. *Cells of the alv are made non editable after entering OK to save
  340.   CALL METHOD c_alvgd->set_ready_for_input
  341.     EXPORTING
  342.       i_ready_for_input = 0.
  343. *Row and column of the alv are refreshed after changing values
  344.   stable-row = 'X'.
  345.   stable-col = 'X'.
  346. *REfreshed ALV display with the changed values
  347. *This ALV is non editable and contains new values
  348.   CALL METHOD c_alvgd->refresh_table_display
  349.     EXPORTING
  350.       is_stable = stable
  351.     EXCEPTIONS
  352.       finished  = 1
  353.       OTHERS    = 2.
  354.   IF sy-subrc <> 0.
  355. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  356. *            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  357.   ENDIF.
  358. ENDFORM.                    " REDISPLAY
复制代码


程序执行效果:
editable F4.png
回复

使用道具 举报

yny038
非常好的程序   学习中……   谢谢分享
回复 支持 反对

使用道具 举报

yny038
非常好的程序   学习中……   谢谢分享
回复 支持 反对

使用道具 举报

快速回帖

本版积分规则
您需要登录后才可以回帖 登录 | 注册有礼

快速回复 返回顶部 返回列表