‎2018 Jan 23 1:54 PM
Hi experts, i need to delete duplicate entries when the user hide fields but the value of the fieldcatalog isn't refresh. Can you help me please ? Thanks in advance for your help 😉
Here is my code :
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.
‎2018 Feb 05 11:00 AM
Your error is NOT related to the field catalog, refresh, and so on.
It's only due to the fact that you create the grid every time : G_CONT is always initial !
IF g_grid IS INITIAL. "don't use g_cont !
CREATE OBJECT g_grid
‎2018 Jan 23 2:05 PM
‎2018 Jan 23 2:22 PM
I haven't infinite loop, this ABAP works one time, when i hide a column, datas are correctly sorted but when i try to make the field displayed again, the field is empty. So i have check my program via checkpoints where the refresh is trigerred and then, i deduced that when i call get_frontend_fieldcatalog, this table is update only the first time the program enter in the loop.
Concerning the value of the field TEST, it is set to empty when the event is trigerred. This prevent the infinite loop no ? Then for use it again, i set it to X after the method After_refresh.
‎2018 Jan 23 2:47 PM
‎2018 Jan 23 3:00 PM
‎2018 Jan 23 3:00 PM
‎2018 Jan 23 3:00 PM
‎2018 Jan 23 3:01 PM
Fourth step :
On the fourth screenshot, we can see that TEST is null before the second call to refresh. And the event has been raised.

‎2018 Jan 23 3:02 PM
Fifth and sixth :
This is a view of the table it_fieldcat after the method get frontend fieldcatalog have been used. no_out value is unchanged from the moment the field has been hidden. 

‎2018 Jan 23 3:03 PM
Sorry for multiple post, the website don't accept lot of picture for one post
‎2018 Jan 23 3:54 PM
‎2018 Jan 23 3:59 PM
‎2018 Jan 30 12:54 PM
The flush should be placed after the refresh_Table_display method
‎2018 Jan 31 7:17 AM
‎2018 Jan 31 7:27 AM
Sorry, i read better and i see the problem: you get the fieldcat, change it, but you do not SET it again before the refresh!
Invoke method SET_FIELDCATALOG and then the REFRESH_TABLE_DISPLAY followed by the FLUSH
‎2018 Jan 31 8:26 AM
‎2018 Jan 24 6:27 AM
‎2018 Jan 24 8:03 AM

I lunch the program so line 192, i check at it_fieldcat-no_out value :

here, no_out is initial
‎2018 Jan 24 8:03 AM
so next step, line 197 :

i check the value of it_fieldcat :

values have been updated. So i continue the execute
‎2018 Jan 24 8:06 AM

Here is datas who are firstly displayed.

I want to hide the field Article
‎2018 Jan 24 8:07 AM

again line 192, check if the fieldcatalog is initial:

values are empty for the moment so it is initial
‎2018 Jan 24 8:09 AM

line 197 again, check fieldcatalog again :

here the table has been correctly updated
‎2018 Jan 24 8:10 AM

the display is correct so now, i want to display the field Article :

‎2018 Jan 24 8:11 AM

line 192, i check values of fieldcatalog and line 197, i have the same table so this table haven't been updated
‎2018 Jan 24 8:21 AM
so i tried to switch order of the method call like this :

and i get the same issue, after the first refresh, datas aren't get anymore
‎2018 Jan 31 8:07 AM
‎2018 Jan 31 8:39 AM

Like this, i get the a wrong no_out value for et_fieldcatalog
‎2018 Jan 31 8:42 AM
‎2018 Jan 31 9:14 AM
Maybe the probleme come from the after refresh event, if i get an other event or user command like the button to state changes before having hide/display a column ? But i don't know how to get this event, the user_command value is DTC_CONT i think but how to handle event from it ?. So the first question who must be answered is : "Why i am getting false / unupdated values in my fieldcatalog ?"
‎2018 Feb 05 11:00 AM
Your error is NOT related to the field catalog, refresh, and so on.
It's only due to the fact that you create the grid every time : G_CONT is always initial !
IF g_grid IS INITIAL. "don't use g_cont !
CREATE OBJECT g_grid
‎2018 Feb 05 12:46 PM
Thank you very much for the explanation of the error. It works perfectly.