SAP ABAP OOALV trier ses données après affichage lors de chaque 'refresh'

SAP ABAP OOALV trier ses données après affichage lors de chaque 'refresh'

Messagede Lissam54 » 31 Janvier 2018, 14:38

Bonjour, je développe en ABAP depuis 6 mois environ et je me retrouve face à un cas que je n'arrive pas à résoudre. Je m'explique :
Dans son ancienne version, mon programme ABAP devait afficher des données extraites de la base de donnée, je stock donc toutes ces données dans un table interne pour pouvoir les exploiter facilement puis j'utilise l'ALV pour les afficher. Jusque là tout va bien.
Dans la nouvelle version, le programme doit afficher ces données puis les retrier en fonction des champs affichés à chaque fois que l'utilisateur masque un champ.
Cela fonctionne...mais juste une fois...
Voici le code (que j'ai simplifié et réduit à un cas basique pour une meilleur compréhension du problème) :
Code: Tout sélectionner
REPORT ZTESTALEX01 NO STANDARD PAGE HEADING.
TYPE-POOLS: slis.
CLASS cl_event_receiver DEFINITION DEFERRED.
*&-------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&-------------------------------------------------------------------*
* text
*--------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'MAIN'.
SET TITLEBAR 'ALV_EXAMPLES'.
ENDMODULE. " STATUS_0100 OUTPUT
MODULE display_grid OUTPUT.
PERFORM read_data.
PERFORM display_grid.
ENDMODULE.
MODULE user_command_0100 INPUT.
* to react on oi_custom_events:
call method cl_gui_cfw=>dispatch.
CASE sy-ucomm.
WHEN 'BACK' OR
'EXIT' OR
'CANCEL'.
LEAVE PROGRAM.
WHEN OTHERS.
* do nothing
ENDCASE.

ENDMODULE.

TYPES: BEGIN OF ty_mara,
MATNR LIKE MARA-MATNR, "Article
ZZBIOJFA LIKE MARA-ZZBIOJFA, "Article bio
MTART LIKE MARA-MTART, "
ZZFRUIT LIKE MARA-ZZFRUIT, "
ZZALLERGENE LIKE MARA-ZZALLERGENE, "
PRDHA LIKE MARA-PRDHA, "
LABOR LIKE MARA-LABOR, "lab./bureau d'étude
END OF ty_mara.

DATA: g_cont TYPE REF TO cl_gui_custom_container,
g_grid TYPE REF TO cl_gui_alv_grid,
rcv_ev TYPE REF TO cl_event_receiver,
gs_layout TYPE lvc_s_layo,
it_mara TYPE TABLE OF ty_mara,
it_aff TYPE TABLE OF ty_mara,
wa_mara TYPE ty_mara,
it_fieldcat TYPE lvc_t_fcat,
wa_fieldcat TYPE lvc_s_fcat,
NOOUT1 TYPE STRING,
NOOUT2 TYPE STRING,
TEST TYPE C VALUE 'X',
l_valid TYPE C,
RS_SELFIELD TYPE SLIS_SELFIELD.
FIELD-SYMBOLS : <mara>,<aff>,<field> TYPE lvc_s_fcat ,<from>,<to>.
TABLES: MARA.

* CLASS lcl_events_d0100 DEFINITION
class cl_event_receiver definition.

public section.
methods:
handle_after_refresh for event after_refresh
of cl_gui_alv_grid.
endclass. "lcl_events_d0100 DEFINITION
* CLASS lcl_events_d0100 IMPLEMENTATION
class cl_event_receiver implementation.

* METHOD after_refresh *
method handle_after_refresh.
IF TEST = 'X'.
perform d0100_event_after_refresh.
ENDIF.
endmethod. "after_refresh *
endclass. "lcl_events_d0100 IMPLEMENTATION
INITIALIZATION.
PERFORM REMPLIR_AFFICHAGE.
START-OF-SELECTION.
CALL SCREEN 0100.

*&-------------------------------------------------------------------*
*& Form read_data
*&-------------------------------------------------------------------*
FORM read_data.
SELECT MATNR ZZBIOJFA MTART ZZFRUIT ZZALLERGENE PRDHA LABOR
INTO CORRESPONDING FIELDS OF TABLE it_mara
FROM MARA.
ENDFORM.

*&-------------------------------------------------------------------*
*& Form display_grid
*&-------------------------------------------------------------------*
FORM display_grid.
IF g_cont IS INITIAL.
CREATE OBJECT g_grid
EXPORTING
i_appl_events = 'X'
i_parent = cl_gui_container=>default_screen.

* create handler
CREATE OBJECT rcv_ev.

* register handler for events
SET HANDLER rcv_ev->handle_after_refresh FOR g_grid.

CALL METHOD g_grid->set_table_for_first_display
* EXPORTING
* I_BUFFER_ACTIVE =
* I_BYPASSING_BUFFER =
* I_CONSISTENCY_CHECK =
* I_STRUCTURE_NAME = 'MARA'
* IS_VARIANT =
* I_SAVE =
* I_DEFAULT = 'X'
* IS_LAYOUT =
* IS_PRINT =
* IT_SPECIAL_GROUPS =
* IT_TOOLBAR_EXCLUDING =
* IT_HYPERLINK =
* IT_ALV_GRAPHICS =
* IT_EXCEPT_QINFO =
* IR_SALV_ADAPTER =
CHANGING
it_outtab = it_aff
IT_FIELDCATALOG = it_fieldcat
* IT_SORT =
* IT_FILTER =
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.
ENDFORM.

FORM REMPLIR_AFFICHAGE.
wa_fieldcat-col_pos = 1.
wa_fieldcat-fieldname = 'MATNR'.
wa_fieldcat-tabname = 'TB_MARA'.
wa_fieldcat-ref_table = 'MARA'.
APPEND wa_fieldcat TO it_fieldcat.
clear wa_fieldcat.
wa_fieldcat-col_pos = 2.
wa_fieldcat-fieldname = 'ZZBIOJFA'.
wa_fieldcat-tabname = 'TB_MARA'.
wa_fieldcat-ref_table = 'MARA'.
APPEND wa_fieldcat TO it_fieldcat.
clear wa_fieldcat.
wa_fieldcat-col_pos = 3.
wa_fieldcat-fieldname = 'MTART'.
wa_fieldcat-tabname = 'TB_MARA'.
wa_fieldcat-ref_table = 'MARA'.
APPEND wa_fieldcat TO it_fieldcat.
clear wa_fieldcat.
wa_fieldcat-col_pos = 4.
wa_fieldcat-fieldname = 'ZZFRUIT'.
wa_fieldcat-tabname = 'TB_MARA'.
wa_fieldcat-ref_table = 'MARA'.
APPEND wa_fieldcat TO it_fieldcat.
clear wa_fieldcat.
wa_fieldcat-col_pos = 5.
wa_fieldcat-fieldname = 'ZZALLERGENE'.
wa_fieldcat-tabname = 'TB_MARA'.
wa_fieldcat-ref_table = 'MARA'.
APPEND wa_fieldcat TO it_fieldcat.
clear wa_fieldcat.
wa_fieldcat-col_pos = 6.
wa_fieldcat-fieldname = 'PRDHA'.
wa_fieldcat-tabname = 'TB_MARA'.
wa_fieldcat-ref_table = 'MARA'.
APPEND wa_fieldcat TO it_fieldcat.
clear wa_fieldcat.
ENDFORM.



FORM d0100_event_after_refresh.

CLEAR TEST. "Pour éviter le redondance de l'événement after refresh
CLEAR it_aff. "La table devant etre affichée
* CLEAR it_fieldcat[].
* if g_grid is INITIAL.
CALL METHOD g_grid->get_frontend_fieldcatalog
IMPORTING
et_fieldcatalog = it_fieldcat[].
* ENDIF.
LOOP AT it_mara ASSIGNING <mara>.
     APPEND INITIAL LINE TO it_aff ASSIGNING <aff>.
     LOOP AT it_fieldcat ASSIGNING <field> WHERE no_out IS INITIAL AND tech IS INITIAL.
         ASSIGN COMPONENT <field>-fieldname OF STRUCTURE <mara> TO <from>.
         CHECK <from> IS ASSIGNED.
         ASSIGN COMPONENT <field>-fieldname OF STRUCTURE <aff> TO <to>.
         CHECK <to> IS ASSIGNED.
         <to> = <from>.
     ENDLOOP.
ENDLOOP.

* CALL METHOD g_grid->set_frontend_fieldcatalog
* EXPORTING
* it_fieldcatalog = it_fieldcat.
* IF g_grid is NOT INITIAL.
* call METHOD g_grid->check_changed_data
* IMPORTING
* e_valid = l_valid.
* ENDIF.
* rs_selfield-refresh = l_valid.


SORT it_aff ASCENDING. "On trie toutes les données
DELETE ADJACENT DUPLICATES FROM it_aff COMPARING ALL FIELDS. "On supprime les duplicatas

IF g_grid is NOT INITIAL.
CALL METHOD g_grid->refresh_table_display."Reaffichage
ENDIF.

TEST = 'X'.

ENDFORM.

Merci d'avance , bonne journée
Alexandre
Lissam54
Posteur débutant
Posteur débutant
 
Messages: 1
Inscription: 30 Janvier 2018, 08:56

Retourner vers ABAP

Qui est en ligne

Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 0 invités

cron