ABAP/DEV/Динамические концепции

Материал из Викиучебника — открытых книг для открытого мира
Перейти к навигации Перейти к поиску
  1 REPORT  zdynamic_alv MESSAGE-ID zdynamic_alv.
  2 ***********************************************************************
  3 *---------------------------------------------------------------------*
  4 *                Data Declaration
  5 *---------------------------------------------------------------------*
  6 *Type-Pools
  7 TYPE-POOLS : rsds.
  8 *Type Declaration
  9 TYPES : ty_tabname(30) TYPE c,
 10         BEGIN OF ty_fieldinfo,
 11         text(60),
 12         END OF ty_fieldinfo,
 13         BEGIN OF ty_keyfields,
 14           fieldname TYPE fieldname,
 15         END OF ty_keyfields.
 16 *Variable Declaration
 17 DATA : v_tabname     TYPE help_info-tabname,
 18        v_fldnam      TYPE fieldname,
 19        v_lines       TYPE i,
 20        v_display     TYPE c,
 21        v_selfields   TYPE string,
 22        v_okcode      TYPE sy-ucomm,
 23        v_selid       TYPE rsdynsel-selid,
 24        v_actnum      TYPE sy-tfill,
 25        v_title       TYPE lvc_title.
 26 * Reference Variable Declaration
 27 DATA : ref_dyntab   TYPE REF TO data,
 28        ref_struct   TYPE REF TO cl_abap_structdescr,
 29        ref_alv      TYPE REF TO cl_salv_table,
 30        ref_func     TYPE REF TO cl_salv_functions,
 31        ref_display  TYPE REF TO cl_salv_display_settings.
 32 *Field-Symbol Declaration
 33 FIELD-SYMBOLS : <fs_wa> TYPE abap_compdescr,
 34                <fs_dyntab> TYPE STANDARD TABLE.
 35 *Internal Table and Work Area Declaration
 36 DATA : it_fldtab     TYPE STANDARD TABLE OF help_value,
 37        it_valuetab   TYPE STANDARD TABLE OF ty_tabname,
 38        it_dfies      TYPE STANDARD TABLE OF dfies,
 39        it_fieldinfo  TYPE STANDARD TABLE OF ty_fieldinfo,
 40        it_return     TYPE STANDARD TABLE OF ddshretval,
 41        it_dynpfields TYPE STANDARD TABLE OF dynpread,
 42        it_fcat       TYPE lvc_t_fcat,
 43        it_expr       TYPE rsds_texpr,
 44        it_where      TYPE rsds_twhere,
 45        it_seltab     TYPE STANDARD TABLE OF rsdstabs,
 46        it_selfields  TYPE STANDARD TABLE OF rsdsfields,
 47        it_keyfields  TYPE STANDARD TABLE OF ty_keyfields,
 48        it_wherecond   TYPE STANDARD TABLE OF rsdswhere,
 49        wa_wherecond  TYPE rsdswhere ,
 50        wa_keyfields  TYPE ty_keyfields,
 51        wa_seltab     TYPE rsdstabs,
 52        wa_selfields  TYPE rsdsfields,
 53        wa_where      TYPE rsds_where,
 54        wa_fcat       TYPE lvc_s_fcat,
 55        wa_dynpfields TYPE dynpread,
 56        wa_return     TYPE ddshretval,
 57        wa_fieldinfo  TYPE ty_fieldinfo,
 58        wa_dfies      TYPE dfies,
 59        wa_fldtab     TYPE help_value,
 60        wa_valuetab   TYPE ty_tabname.
 61 *---------------------------------------------------------------------*
 62 *                 Selection-Screen block1
 63 *---------------------------------------------------------------------*
 64 SELECTION-SCREEN BEGIN OF BLOCK block1 WITH FRAME TITLE text-000.
 65 PARAMETERS : p_table TYPE tabname OBLIGATORY.       "Table Name
 66 SELECTION-SCREEN END OF BLOCK block1.
 67 *---------------------------------------------------------------------*
 68 *                 Selection-Screen block2
 69 *---------------------------------------------------------------------*
 70 SELECTION-SCREEN BEGIN OF BLOCK block2 WITH FRAME TITLE text-002.
 71 PARAMETERS : p_fnam1  TYPE fieldname,     "Field name from Given Table
 72              p_fnam2  TYPE fieldname,
 73              p_fnam3  TYPE fieldname,
 74              p_fnam4  TYPE fieldname,
 75              p_fnam5  TYPE fieldname,
 76              p_fnam6  TYPE fieldname,
 77              p_fnam7  TYPE fieldname,
 78              p_fnam8  TYPE fieldname,
 79              p_fnam9  TYPE fieldname,
 80              p_fnam10 TYPE fieldname.
 81 SELECTION-SCREEN END OF BLOCK block2.
 82 *---------------------------------------------------------------------*
 83 *                 Selection-Screen block3
 84 *---------------------------------------------------------------------*
 85 SELECTION-SCREEN BEGIN OF BLOCK block3 WITH FRAME TITLE text-004.
 86 PARAMETERS : p_max  TYPE i.                                               "Max Number of rows
 87 SELECTION-SCREEN END OF BLOCK block3.
 88 *---------------------------------------------------------------------*
 89 *                At selection-Screen Output
 90 *---------------------------------------------------------------------*
 91 AT SELECTION-SCREEN OUTPUT.
 92   SET PF-STATUS  'PFSTAT1'.
 93   SET TITLEBAR    'TIT1'.
 94 *---------------------------------------------------------------------*
 95 *                F4 Help for Table name
 96 *---------------------------------------------------------------------*
 97 AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_table.
 98   PERFORM f4help_table_name.
 99 *---------------------------------------------------------------------*
100 *                F4 Help for Field names
101 *---------------------------------------------------------------------*
102 AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fnam1.
103   PERFORM f4help_fieldname.
104 *---------------------------------------------------------------------*
105 *                At Selection-Screen
106 *---------------------------------------------------------------------*
107 AT SELECTION-SCREEN.
108   CLEAR : v_okcode.
109   v_okcode = sy-ucomm.
110   CASE v_okcode.
111     WHEN 'SLOP'.        "Menu Item to display select options for given table key fields
112       PERFORM get_selet_options.
113     WHEN 'DISP'.          " Menu Item  to display ALV
114       PERFORM display_alv.
115     WHEN 'BACK'.
116       LEAVE PROGRAM.
117     WHEN 'CLR'.         " Menu item To clear all selections
118       CLEAR : p_table,
119               p_fnam1,
120               p_fnam2,
121               p_fnam3,
122               p_fnam4,
123               p_fnam5,
124               p_fnam6,
125               p_fnam7,
126               p_fnam8,
127               p_fnam9,
128               p_fnam10,
129               p_max,
130               wa_where.
131       REFRESH : it_where.
132   ENDCASE.
133 *---------------------------------------------------------------------*
134 *                At Selection-Screen On Exit Command
135 *---------------------------------------------------------------------*
136 AT SELECTION-SCREEN ON EXIT-COMMAND.
137   CLEAR : v_okcode.
138   v_okcode = sy-ucomm.
139   CASE v_okcode.
140     WHEN 'TOP'  OR
141          'EXIT'.
142       LEAVE PROGRAM.
143   ENDCASE.
144 *&---------------------------------------------------------------------&*
145 *&                 Subroutines                                                        &*
146 *&---------------------------------------------------------------------&*
147 *&---------------------------------------------------------------------*
148 *&      Form  f4help_table_name
149 *&---------------------------------------------------------------------*
150 FORM f4help_table_name .
151   CLEAR   : v_tabname,
152             v_fldnam,
153             v_display.
154   CLEAR   : it_valuetab,
155             it_fldtab,
156             wa_valuetab,
157             wa_fldtab.
158 
159   REFRESH : it_valuetab,
160              it_fldtab.
161   v_tabname = 'DD02L'.
162   v_fldnam  = 'TABNAME'.
163   v_display = ' '.
164 *Fill fieldtab
165   wa_fldtab-tabname    = 'DD02L'.
166   wa_fldtab-fieldname  = 'TABNAME'.
167   wa_fldtab-selectflag = 'X'.
168   APPEND wa_fldtab TO it_fldtab.
169 *Fill Value Table
170   SELECT tabname
171     FROM dd02l
172     INTO TABLE it_valuetab
173    WHERE tabclass  = 'TRANSP' AND
174          applclass = 'VA'     OR
175          applclass = 'ME'     OR
176          applclass = 'FB'.
177 *Call F4 Function
178   CALL FUNCTION 'F4TOOL_F4FUNCTION_BRIDGE'
179     EXPORTING
180       tabname        = v_tabname
181       fieldname      = v_fldnam
182       display_only   = v_display
183       window_title   = text-001
184     IMPORTING
185       selected_value = p_table
186     TABLES
187       value_tab      = it_valuetab
188       fields_tab     = it_fldtab.
189 
190 ENDFORM.                    " f4help_table_name
191 
192 *&---------------------------------------------------------------------*
193 *&      Form  f4help_fieldname
194 *&---------------------------------------------------------------------*
195 FORM f4help_fieldname .
196   CLEAR   : wa_fldtab,
197             it_fldtab,
198             v_tabname,
199             v_fldnam.
200   REFRESH : it_fldtab,
201             it_fieldinfo.
202   v_tabname = 'DD03T'.
203   v_fldnam  = 'FIELDNAME'.
204 *Getting Field Info
205   CALL FUNCTION 'DDIF_FIELDINFO_GET'
206     EXPORTING
207       tabname        = p_table
208       langu          = sy-langu
209     TABLES
210       dfies_tab      = it_dfies
211     EXCEPTIONS
212       not_found      = 1
213       internal_error = 2.
214 
215 *Fill Value table
216   LOOP AT it_dfies INTO wa_dfies.
217     wa_fieldinfo-text = wa_dfies-fieldname.
218     APPEND wa_fieldinfo TO it_fieldinfo.
219     CLEAR wa_fieldinfo.
220     wa_fieldinfo-text = wa_dfies-fieldtext.
221     APPEND wa_fieldinfo TO it_fieldinfo.
222     CLEAR wa_fieldinfo.
223   ENDLOOP.
224   CLEAR   : it_dfies,
225             wa_dfies.
226   REFRESH : it_dfies.
227 *Fill field Table
228   wa_dfies-tabname    = 'DD03T'.
229   wa_dfies-fieldname  = 'FIELDNAME'.
230   wa_dfies-keyflag = 'X'.
231   APPEND wa_dfies TO it_dfies.
232   CLEAR wa_dfies.
233   wa_dfies-tabname    = 'DD03T'.
234   wa_dfies-fieldname  = 'DDTEXT'.
235   wa_dfies-keyflag    = ' '.
236   APPEND wa_dfies TO it_dfies.
237   CLEAR wa_dfies.
238 *Call F4 function
239   CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
240     EXPORTING
241       retfield        = v_fldnam
242       dynpprog        = sy-repid
243       dynpnr          = sy-dynnr
244 *     dynprofield     = 'P_FNAM1'    "This should not be passed.DOnno reason.because of this getting problem in display.
245       window_title    = text-003
246       value_org       = 'C'
247       multiple_choice = 'X'
248     TABLES
249       value_tab       = it_fieldinfo
250       field_tab       = it_dfies
251       return_tab      = it_return
252     EXCEPTIONS
253       parameter_error = 1
254       no_values_found = 2.
255   IF sy-subrc EQ 0.
256     IF NOT it_return[] IS INITIAL.
257       DESCRIBE TABLE it_return LINES v_lines.
258       IF v_lines GE 11.
259         MESSAGE i000 WITH 'No Authorization To select more than 10 fields'.
260       ELSE.
261         LOOP AT it_return INTO wa_return.
262           CASE sy-tabix.
263             WHEN '1'.
264               wa_dynpfields-fieldname  = 'P_FNAM1'.
265               wa_dynpfields-fieldvalue = wa_return-fieldval.
266               APPEND wa_dynpfields TO it_dynpfields.
267             WHEN '2'.
268               wa_dynpfields-fieldname  = 'P_FNAM2'.
269               wa_dynpfields-fieldvalue = wa_return-fieldval.
270               APPEND wa_dynpfields TO it_dynpfields.
271             WHEN '3'.
272               wa_dynpfields-fieldname  = 'P_FNAM3'.
273               wa_dynpfields-fieldvalue = wa_return-fieldval.
274               APPEND wa_dynpfields TO it_dynpfields.
275             WHEN '4'.
276               wa_dynpfields-fieldname  = 'P_FNAM4'.
277               wa_dynpfields-fieldvalue = wa_return-fieldval.
278               APPEND wa_dynpfields TO it_dynpfields.
279             WHEN '5'.
280               wa_dynpfields-fieldname  = 'P_FNAM5'.
281               wa_dynpfields-fieldvalue = wa_return-fieldval.
282               APPEND wa_dynpfields TO it_dynpfields.
283             WHEN '6'.
284               wa_dynpfields-fieldname  = 'P_FNAM6'.
285               wa_dynpfields-fieldvalue = wa_return-fieldval.
286               APPEND wa_dynpfields TO it_dynpfields.
287             WHEN '7'.
288               wa_dynpfields-fieldname  = 'P_FNAM7'.
289               wa_dynpfields-fieldvalue = wa_return-fieldval.
290               APPEND wa_dynpfields TO it_dynpfields.
291             WHEN '8'.
292               wa_dynpfields-fieldname  = 'P_FNAM8'.
293               wa_dynpfields-fieldvalue = wa_return-fieldval.
294               APPEND wa_dynpfields TO it_dynpfields.
295             WHEN '9'.
296               wa_dynpfields-fieldname  = 'P_FNAM9'.
297               wa_dynpfields-fieldvalue = wa_return-fieldval.
298               APPEND wa_dynpfields TO it_dynpfields.
299             WHEN '10'.
300               wa_dynpfields-fieldname  = 'P_FNAM10'.
301               wa_dynpfields-fieldvalue = wa_return-fieldval.
302               APPEND wa_dynpfields TO it_dynpfields.
303           ENDCASE.
304         ENDLOOP.
305 *Updating Screen Fields
306         CALL FUNCTION 'DYNP_VALUES_UPDATE'
307           EXPORTING
308             dyname               = sy-cprog
309             dynumb               = sy-dynnr
310           TABLES
311             dynpfields           = it_dynpfields
312           EXCEPTIONS
313             invalid_abapworkarea = 1
314             invalid_dynprofield  = 2
315             invalid_dynproname   = 3
316             invalid_dynpronummer = 4
317             invalid_request      = 5
318             no_fielddescription  = 6
319             undefind_error       = 7.
320       ENDIF.
321     ENDIF.
322   ELSE.
323     MESSAGE i000 WITH 'Please select atleast one field'.
324   ENDIF.
325 ENDFORM.                    " f4help_fieldname*&---------------------------------------------------------------------*
326 *&      Form  create_dynamic_inttab
327 *&-------------------------------------------------------------------*
328 FORM create_dynamic_inttab .
329   CLEAR   : wa_fcat.
330   REFRESH : it_fcat.
331 *Get structure
332   ref_struct ?= cl_abap_typedescr=>describe_by_name( p_table ).
333 *Build Field catlog
334   LOOP AT ref_struct->components ASSIGNING <fs_wa>.
335     IF <fs_wa>-name = p_fnam1 OR
336        <fs_wa>-name = p_fnam2 OR
337        <fs_wa>-name = p_fnam3  OR
338        <fs_wa>-name = p_fnam4 OR
339        <fs_wa>-name = p_fnam5 OR
340        <fs_wa>-name = p_fnam6 OR
341        <fs_wa>-name = p_fnam7 OR
342        <fs_wa>-name = p_fnam8 OR
343        <fs_wa>-name = p_fnam9 OR
344        <fs_wa>-name = p_fnam10.
345       wa_fcat-ref_table = p_table.
346       wa_fcat-fieldname = <fs_wa>-name .
347       wa_fcat-datatype  = <fs_wa>-type_kind.
348       wa_fcat-decimals  = <fs_wa>-decimals.
349       wa_fcat-col_opt   = ' '.              "Optimizaion is kept always
350       APPEND wa_fcat TO it_fcat.
351     ENDIF.
352   ENDLOOP.
353 *Create Dynamic Internal Table
354   CALL METHOD cl_alv_table_create=>create_dynamic_table
355     EXPORTING
356       it_fieldcatalog           = it_fcat
357     IMPORTING
358       ep_table                  = ref_dyntab
359     EXCEPTIONS
360       generate_subpool_dir_full = 1
361       OTHERS                    = 2.
362   IF sy-subrc <> 0.
363     MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
364                WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
365   ELSE.
366     ASSIGN ref_dyntab->* TO <fs_dyntab>.
367   ENDIF.
368 ENDFORM.                    " create_dynamic_inttab
369 *&---------------------------------------------------------------------*
370 *&      Form  get_data
371 *&---------------------------------------------------------------------*
372 FORM get_data .
373   CONCATENATE p_fnam1 p_fnam2 p_fnam3 p_fnam4 p_fnam5 p_fnam6 p_fnam7 p_fnam8 p_fnam9 p_fnam10
374         INTO  v_selfields
375  SEPARATED BY space.
376 *Dynamic Select
377   IF wa_where-where_tab IS INITIAL.
378     SELECT (v_selfields)
379       FROM (p_table)
380       INTO TABLE <fs_dyntab>
381      UP TO p_max ROWS.
382   ELSE.
383     SELECT (v_selfields)
384       FROM (p_table)
385       INTO TABLE <fs_dyntab>
386      UP TO p_max ROWS
387      WHERE (wa_where-where_tab).
388   ENDIF.
389 ENDFORM.                    " get_data
390 *&---------------------------------------------------------------------*
391 *&      Form  display_alv
392 *&---------------------------------------------------------------------*
393 FORM display_alv .
394 *Validation for Fields
395   IF p_fnam1 IS INITIAL AND
396      p_fnam1 IS INITIAL AND
397      p_fnam1 IS INITIAL AND
398      p_fnam1 IS INITIAL AND
399      p_fnam1 IS INITIAL AND
400      p_fnam1 IS INITIAL AND
401      p_fnam1 IS INITIAL AND
402      p_fnam1 IS INITIAL AND
403      p_fnam1 IS INITIAL AND
404      p_fnam1 IS INITIAL .
405     MESSAGE i000 WITH 'Please enter Atleast one field'.
406   ELSE.
407     PERFORM create_dynamic_inttab.
408     PERFORM get_data.
409 *Main Method To generate ALV
410     TRY.
411         CALL METHOD cl_salv_table=>factory
412           IMPORTING
413             r_salv_table = ref_alv
414           CHANGING
415             t_table      = <fs_dyntab>.
416       CATCH cx_salv_msg .
417     ENDTRY.
418 *Activate ALV Generic Functions
419     PERFORM set_alv_functions.
420 *Display Settings
421     PERFORM set_display_settings.
422 *Display ALV
423     CALL METHOD ref_alv->display.
424   ENDIF.
425 ENDFORM.                    " display_alv*&---------------------------------------------------------------------*
426 *      Form  GET_SELET_OPTIONS
427 *&---------------------------------------------------------------------*
428 FORM get_selet_options .
429 *Fill Fieldtab and Table Tab
430   PERFORM fill_selectiontabs.
431 *Initialization of Selection Screen
432   CALL FUNCTION 'FREE_SELECTIONS_INIT'
433     EXPORTING
434       kind                     = 'T'
435     IMPORTING
436       selection_id             = v_selid
437       where_clauses            = it_where
438       expressions              = it_expr
439       number_of_active_fields  = v_actnum
440     TABLES
441       tables_tab               = it_seltab
442       fields_tab               = it_selfields
443     EXCEPTIONS
444       fields_incomplete        = 1
445       fields_no_join           = 2
446       field_not_found          = 3
447       no_tables                = 4
448       table_not_found          = 5
449       expression_not_supported = 6
450       incorrect_expression     = 7
451       illegal_kind             = 8
452       area_not_found           = 9
453       inconsistent_area        = 10
454       kind_f_no_fields_left    = 11
455       kind_f_no_fields         = 12
456       too_many_fields          = 13
457       dup_field                = 14
458       field_no_type            = 15
459       field_ill_type           = 16
460       dup_event_field          = 17
461       node_not_in_ldb          = 18
462       area_no_field            = 19.
463   IF sy-subrc EQ 0.
464     CALL FUNCTION 'FREE_SELECTIONS_DIALOG'
465       EXPORTING
466         selection_id            = v_selid
467         title                   = ' '
468         as_window               = 'X'
469         tree_visible            = ' '
470         as_subscreen            = ' '
471       IMPORTING
472         where_clauses           = it_where
473         expressions             = it_expr
474         number_of_active_fields = v_actnum
475       TABLES
476         fields_tab              = it_selfields
477       EXCEPTIONS
478         internal_error          = 1
479         no_action               = 2
480         selid_not_found         = 3
481         illegal_status          = 4.
482     IF sy-subrc EQ 0.
483       READ TABLE it_where INTO wa_where WITH KEY tablename = p_table.
484     ENDIF.
485   ENDIF.
486 ENDFORM.                    " GET_SELET_OPTIONS
487 *&---------------------------------------------------------------------*
488 *&      Form  fill_selectiontabs
489 *&---------------------------------------------------------------------*
490 
491 FORM fill_selectiontabs .
492   CLEAR   : wa_seltab,
493             wa_selfields.
494   REFRESH : it_seltab,
495             it_selfields.
496 *Fill Tabletab
497   wa_seltab-prim_tab = p_table.
498   APPEND wa_seltab TO it_seltab.
499 *Fill fields Tab
500   SELECT fieldname
501     FROM dd03l
502     INTO TABLE it_keyfields
503    WHERE tabname   = p_table AND
504          keyflag   = 'X'     AND                       "To select only key fields
505          fieldname NE 'MANDT'.
506   LOOP AT it_keyfields INTO wa_keyfields.
507     wa_selfields-tablename = p_table.
508     wa_selfields-fieldname = wa_keyfields-fieldname.
509     APPEND wa_selfields TO it_selfields.
510   ENDLOOP.
511 ENDFORM.                    " fill_selectiontabs
512 *&---------------------------------------------------------------------*
513 *&      Form  set_alv_functions
514 *&---------------------------------------------------------------------*
515 FORM set_alv_functions .
516 
517 *reate refrence for cl_salv_functions
518   CALL METHOD ref_alv->get_functions
519     RECEIVING
520       value = ref_func.
521 *Activate All ALV generic Functions
522   ref_func->set_all( abap_true ).
523 ENDFORM.                    " set_alv_functions
524 *&---------------------------------------------------------------------*
525 *      Form  set_display_settings
526 *&---------------------------------------------------------------------*
527 FORM set_display_settings .
528 *Get Dynamic Title depends on Table selected
529   SELECT SINGLE ddtext
530     FROM dd02t
531     INTO v_title
532    WHERE tabname     = p_table AND
533          ddlanguage  = sy-langu.
534   CONCATENATE v_title ':' 'Display Mode' INTO v_title SEPARATED BY space.
535 *Create reference for cl_salv_display_settings
536   ref_display  = ref_alv->get_display_settings( ).
537 *Set Title Bar(List Header)
538   ref_display->set_list_header( v_title ).
539 ENDFORM.                    " set_display_settings