Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

ALV wrong row_id in METHOD handle_data_changed when grid is sorted

horst_winter
Explorer
0 Likes
2,964

Hi everybody,

we've made a small data maintenance report with the ALV grid using the class CL_GUI_ALV_GRID.

My problem is the behaviour of ALV, when the grid is autosorted on a field, which is changed by a user. Within the hanlde method of the event data_changed I get the internal table er_data_changed->mt_good_cells containing entries (of the type lvc_s_modi) for every field which was changed. The field ROW_ID containes the row id of the internal table of the grid, in which the change was made by the user. This ROW_ID can be wrong, when the change the user made leads to an autosort of the table, which sorts the table in a different order. When you then access the ALV-table by the row-id you get the wrong, not changed row back.

Here a short extract of the code:

******************************************************************************

METHOD handle_data_changed.

...

LOOP AT er_data_changed->mt_good_cells INTO ls_good.

READ TABLE gt_alv INDEX ls_good-row_id ASSIGNING <l_line_of_alv>.

<i>* wrong line of the ALV grid is in <l_line_of_alv></i>

...

ENDLOOP.

...

ENDMETHOD.

******************************************************************************

Have you any idea, how this can be avoided?

Is it possible to disable the autosort <b>before</b> handle_changed is triggert and to enable it afterwards again?

I'm thankful for any idea.

Regards,

Horst

Hi everybody,

we've made a small data maintenance report with the ALV grid using the class CL_GUI_ALV_GRID.

My problem is the behaviour of ALV, when the grid is autosorted on a field, which is changed by a user. Within the hanlde method of the event data_changed I get the internal table er_data_changed->mt_good_cells containing entries (of the type lvc_s_modi) for every field which was changed. The field ROW_ID containes the row id of the internal table of the grid, in which the change was made by the user. This ROW_ID can be wrong, when the change the user made leads to an autosort of the table, which sorts the table in a different order. When you then access the ALV-table by the row-id you get the wrong, not changed row back.

Here a short extract of the code:

******************************************************************************

METHOD handle_data_changed.

...

LOOP AT er_data_changed->mt_good_cells INTO ls_good.

READ TABLE gt_alv INDEX ls_good-row_id ASSIGNING <l_line_of_alv>.

<i>* wrong line of the ALV grid is in <l_line_of_alv></i>

...

ENDLOOP.

...

ENDMETHOD.

******************************************************************************

Have you any idea, how this can be avoided?

Is it possible to disable the autosort <b>before</b> handle_changed is triggert and to enable it afterwards again?

I'm thankful for any idea.

Regards,

Horst

3 REPLIES 3
Read only

Former Member
0 Likes
1,853

I don't do it this way myself.

I use a "generalized" ALV class using a dynamic table --then the latest state of the GRID data is always in your dynamic table <dyn_table>. I have a copy of the original as well in <orig_table>.

This means at either event ON DATA CHANGED or ON DATA CHANGED FINISHED I've got the latest state of the table even if sorted and the last cell(s) / row(s) selected

Here's the code

  • INCLUDE ZZJIMBOXX_INCL.

  • Generic ALV class etc for use as INCLUDE Jimbo 2007.

DEFINE col_name.

READ TABLE it_fldcat INTO wa_it_fldcat INDEX &1.

wa_it_fldcat-coltext = &2.

wa_it_fldcat-outputlen = &3.

modify it_fldcat from wa_it_fldcat index &1.

END-OF-DEFINITION.

DEFINE toolbar_funcs.

CLEAR ls_toolbar.

MOVE 0 TO ls_toolbar-butn_TYPE.

MOVE &1 TO ls_toolbar-function.

MOVE SPACE TO ls_toolbar-disabled.

MOVE &2 TO ls_toolbar-icon.

MOVE &3 TO ls_toolbar-quickinfo.

APPEND ls_toolbar TO e_object->mt_toolbar.

END-OF-DEFINITION.

INCLUDE <icon>.

FIELD-SYMBOLS :

<fs1> TYPE STANDARD TABLE,

<fs0> TYPE STANDARD TABLE,

<dyn_table> TYPE STANDARD TABLE,

<orig_table> TYPE STANDARD TABLE,

<dyn_wa> TYPE ANY.

CLASS zcl_alv_test DEFINITION DEFERRED.

DATA:

z_object TYPE REF TO zcl_alv_test. "Instantiate our class

  • Attributes

DATA:

it_fldcat TYPE lvc_t_fcat,

i_gridtitle TYPE lvc_title,

wa_it_fldcat TYPE lvc_s_fcat,

new_table TYPE REF TO DATA,

dy_table TYPE REF TO DATA,

inserted_tab TYPE lvc_t_moce,

deleted_tab TYPE LVC_T_MOCE,

changed_tab TYPE REF TO DATA,

is_layout TYPE LVC_S_LAYO,

modified_cells_tab TYPE LVC_T_MODI,

dy_line TYPE REF TO DATA.

CLASS zcl_alv_test DEFINITION.

PUBLIC SECTION.

METHODS:

constructor

IMPORTING

z_object TYPE REF TO zcl_alv_test,

display_grid

IMPORTING

g_outtab TYPE STANDARD TABLE

g_fldcat TYPE lvc_t_fcat

CHANGING

it_fldcat TYPE lvc_t_fcat

GT_OUTTAB TYPE STANDARD TABLE,

change_title

IMPORTING

i_gridtitle TYPE lvc_title,

refresh_grid,

set_cursor

IMPORTING

row_id TYPE lvc_s_row

column_id TYPE lvc_s_col

row_no TYPE lvc_s_roid,

build_dynamic_structures

IMPORTING

my_line TYPE ANY

calling_program TYPE sy-repid

EXPORTING

dy_table TYPE REF TO DATA

CHANGING

it_fldcat TYPE lvc_t_fcat .

PRIVATE SECTION.

*

  • Attributes

*

DATA:

lr_rtti_struc TYPE REF TO cl_abap_structdescr,

zog LIKE LINE OF lr_rtti_struc->components,

zogt LIKE table of zog,

struct_grid_lset TYPE lvc_s_layo,

e_row TYPE lvc_s_row,

e1_row TYPE i,

e_value TYPE c,

e1_col TYPE i,

e_column TYPE lvc_s_col,

es_rowid TYPE lvc_s_roid,

es_row_id TYPE LVC_S_ROW,

es_col_id TYPE LVC_S_COL,

es_row_no TYPE lvc_s_roid,

grid_container1 TYPE REF TO cl_gui_custom_container,

grid1 TYPE REF TO cl_gui_alv_grid,

ls_layout TYPE kkblo_layout,

lt_fieldcat_wa TYPE kkblo_fieldcat,

gt_outtab TYPE REF TO DATA,

l_mode TYPE raw4,

celltab TYPE lvc_t_styl,

wa_celltab TYPE lvc_s_styl,

lt_fieldcat TYPE kkblo_t_fieldcat,

l_tabname TYPE slis_tabname,

ls_toolbar TYPE stb_button,

caller TYPE sy-repid,

g_outtab1 TYPE REF TO DATA,

g_fldcat1 TYPE REF TO DATA.

*

  • Event Receivers - These methods are entered

  • when the specified event occurs

*

EVENTS: before_user_command.

METHODS:

on_user_command

FOR EVENT before_user_command OF cl_gui_alv_grid

IMPORTING

e_ucomm

sender,

on_toolbar

FOR EVENT toolbar OF cl_gui_alv_grid

IMPORTING

e_object

e_interactive,

on_dubbelklik

FOR EVENT double_click OF cl_gui_alv_grid

IMPORTING

e_row

e_column

es_row_no,

handle_data_changed

FOR EVENT data_changed OF cl_gui_alv_grid

IMPORTING

er_data_changed,

handle_data_changed_finished

FOR EVENT data_changed_finished OF cl_gui_alv_grid

IMPORTING

e_modified

et_good_cells,

*

  • Rest of the methods

*

process,

dubbelklik

IMPORTING

e_row TYPE lvc_s_row

e_column TYPE lvc_s_col

es_row_no TYPE lvc_s_roid,

return_structure

IMPORTING

my_line TYPE ANY,

create_dynamic_fcat

EXPORTING

it_fldcat TYPE lvc_t_fcat,

create_dynamic_table

IMPORTING

it_fldcat TYPE lvc_t_fcat

EXPORTING

dy_table TYPE REF TO DATA,

download_to_excel,

refresh.

ENDCLASS. "zcl_alv_test DEFINITION

*

  • Implementation definition

*

CLASS zcl_alv_test IMPLEMENTATION.

*

  • Constructor

  • create our reference / instance to cl_gui_alv_grid

*

METHOD constructor.

CREATE OBJECT grid_container1

EXPORTING

container_name = 'CCONTAINER1'.

CREATE OBJECT grid1

EXPORTING

i_parent = grid_container1.

*

  • Set event handlers

*

SET HANDLER z_object->on_user_command for grid1.

SET HANDLER z_object->on_toolbar for grid1.

SET HANDLER Z_OBJECT->handle_data_changed FOR grid1.

SET HANDLER Z_OBJECT->handle_data_changed_finished FOR grid1.

SET HANDLER Z_OBJECT->on_dubbelklik FOR grid1.

CALL METHOD grid1->register_edit_event

EXPORTING

i_event_id = cl_gui_alv_grid=>mc_evt_enter.

ENDMETHOD. "constructor

*

  • Rest of the methods

*

METHOD on_dubbelklik.

CALL METHOD me->dubbelklik

EXPORTING

e_row = e_row

e_column = e_column

es_row_no = es_row_no.

ENDMETHOD. "on_dubbelklik

*

METHOD set_cursor.

CALL METHOD grid1->set_current_cell_via_id

EXPORTING

is_row_id = row_id

is_column_id = column_id

is_row_no = row_no.

ENDMETHOD.

METHOD handle_data_changed.

call method grid1->get_current_cell

IMPORTING

e_row = e1_row

e_value = e_value

e_col = e1_col

es_row_id = es_row_id

es_col_id = es_col_id

es_row_no = es_row_no.

changed_tab = er_data_changed->mp_mod_rows.

inserted_tab = er_data_changed->mt_inserted_rows.

deleted_tab = er_data_changed->mt_deleted_rows.

modified_cells_tab = er_data_changed->mt_mod_cells.

PERFORM data_changed IN PROGRAM (caller) IF FOUND

USING changed_tab

inserted_tab

deleted_tab

modified_cells_tab.

ENDMETHOD. "handle_data_changed

METHOD handle_data_changed_finished.

ENDMETHOD. "handle_data_changed_finished

METHOD return_structure.

lr_rtti_struc ?= cl_abap_structdescr=>DESCRIBE_BY_DATA( my_line ).

zogt[] = lr_rtti_struc->components.

ENDMETHOD. "return_structure

METHOD create_dynamic_fcat.

LOOP AT zogt INTO zog.

CLEAR wa_it_fldcat.

wa_it_fldcat-fieldname = zog-name .

wa_it_fldcat-dataTYPE = zog-TYPE_kind.

wa_it_fldcat-intTYPE = zog-TYPE_kind.

wa_it_fldcat-intlen = zog-length.

wa_it_fldcat-decimals = zog-decimals.

wa_it_fldcat-coltext = zog-name.

wa_it_fldcat-lowercase = 'X'.

APPEND wa_it_fldcat TO it_fldcat .

ENDLOOP.

ENDMETHOD. "create_dynamic_fcat

METHOD download_to_excel.

assign g_outtab1->* to <fs0>.

assign g_fldcat1->* to <fs1>.

CALL FUNCTION 'LVC_TRANSFER_TO_KKBLO'

EXPORTING

it_fieldcat_lvc = <fs1>

  • is_layout_lvc = m_cl_variant->ms_layout

is_tech_complete = ' '

IMPORTING

es_layout_kkblo = ls_layout

et_fieldcat_kkblo = lt_fieldcat.

LOOP AT lt_fieldcat INTO lt_fieldcat_wa.

CLEAR lt_fieldcat_wa-tech_complete.

IF lt_fieldcat_wa-tabname IS initial.

lt_fieldcat_wa-tabname = '1'.

MODIFY lt_fieldcat FROM lt_fieldcat_wa.

ENDIF.

l_tabname = lt_fieldcat_wa-tabname.

ENDLOOP.

CALL FUNCTION 'ALV_XXL_CALL'

EXPORTING

i_tabname = l_tabname

is_layout = ls_layout

it_fieldcat = lt_fieldcat

i_title = sy-title

TABLES

it_outtab = <fs0>

EXCEPTIONS

fatal_error = 1

no_display_possible = 2

others = 3.

IF sy-subrc <> 0.

message id sy-msgid TYPE 'S' number sy-msgno

with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

ENDMETHOD. "download_to_excel

METHOD change_title.

CALL METHOD grid1->set_gridtitle

EXPORTING

i_gridtitle = i_gridtitle.

ENDMETHOD. "CHANGE_TITLE

METHOD create_dynamic_table.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fldcat

IMPORTING

ep_table = dy_table.

ENDMETHOD. "create_dynamic_table

METHOD build_dynamic_structures.

caller = calling_program.

CALL METHOD me->return_structure

EXPORTING

my_line = my_line.

CALL METHOD me->create_dynamic_fcat

IMPORTING

it_fldcat = it_fldcat.

CALL METHOD me->create_dynamic_table

EXPORTING

it_fldcat = it_fldcat

IMPORTING

dy_table = dy_table.

ENDMETHOD. "build_dynamic_structures

METHOD display_grid.

GET REFERENCE OF g_outtab INTO g_outtab1.

GET REFERENCE OF g_fldcat INTO g_fldcat1.

struct_grid_lset-edit = 'X'. "To enable editing in ALV

struct_grid_lset-grid_title = 'TEST ALV USE generic class'.

struct_grid_lset-ctab_fname = 'T_CELLCOLORS'.

struct_grid_lset-stylefname = 'CELLTAB'.

CALL METHOD grid1->set_ready_for_input

EXPORTING

i_ready_for_input = '1'.

CALL METHOD grid1->set_table_for_first_display

EXPORTING

is_layout = struct_grid_lset

CHANGING

it_outtab = gt_outtab

it_fieldcatalog = it_fldcat.

ENDMETHOD. "display_grid

METHOD on_user_command.

CASE e_ucomm.

WHEN 'EXIT'.

LEAVE PROGRAM.

WHEN 'EXCEL'.

CALL METHOD me->download_to_excel.

WHEN 'SAVE'.

WHEN 'PROC'.

CALL METHOD me->process.

WHEN 'REFR'.

CALL METHOD me->refresh.

ENDCASE.

ENDMETHOD. "on_user_command

METHOD on_toolbar.

  • customize this section with your own Buttons

  • When a button is pressed method ON_USER_COMMAND is entered

toolbar_funcs 'EXIT' icon_system_end 'Click2exit'.

toolbar_funcs 'SAVE' icon_system_save 'Savedata'.

toolbar_funcs 'EDIT' icon_toggle_display_change 'Edit data'.

toolbar_funcs 'PROC' icon_businav_process 'Process'.

toolbar_funcs 'EXCEL' icon_xxl 'Excel'.

toolbar_funcs 'REFR' icon_refresh 'Refresh'.

ENDMETHOD. "on_toolbar

METHOD refresh_grid.

CALL METHOD cl_gui_cfw=>flush.

CALL METHOD grid1->refresh_table_display.

ENDMETHOD. "refresh_grid

METHOD refresh.

PERFORM refresh IN PROGRAM (caller) IF FOUND.

ENDMETHOD. "refresh

METHOD process.

PERFORM process IN PROGRAM (caller) IF FOUND.

ENDMETHOD. "process

METHOD dubbelklik.

perform dubbelklik IN PROGRAM (caller) IF FOUND

USING e_row

e_column

es_row_no.

ENDMETHOD. "dubbelklik

ENDCLASS. "zcl_alv_test IMPLEMENTATION

Now for example I can use this program to fill a grid

Program ZJIMBOTESTX.

INCLUDE ZZJIMBOXX_INCL. "Code is above

TABLES : SPFLI.

TYPES: BEGIN OF s_elements.

INCLUDE STRUCTURE spfli..

TYPES: END OF s_elements.

DATA:

t_elements TYPE TABLE OF s_elements, "refers to our ITAB

my_line TYPE s_elements.

START-OF-SELECTION.

CALL SCREEN 100.

END-OF-SELECTION.

MODULE status_0100 OUTPUT.

CREATE OBJECT z_object

EXPORTING z_object = z_object.

CALL METHOD z_object->build_dynamic_structures

EXPORTING

my_line = my_line

calling_program = sy-repid

IMPORTING

dy_table = dy_table

CHANGING

it_fldcat = it_fldcat.

  • Here before displaying you can change the field catalog to

  • adjust your own column names.

*col_name col-nr 'your name' output length.

col_name 2 'Carrier' 5.

col_name 3 'Flt' 4.

col_name 4 'Dep Ctry' 8.

col_name 5 'Dep City' 8.

col_name 6 'Airport' 6.

  • fill dynmic table and display

PERFORM populate_dynamic_itab.

is_layout-zebra = 'X'.

CALL METHOD z_object->display_grid

EXPORTING

g_outtab = <dyn_table>

g_fldcat = it_fldcat

CHANGING

it_fldcat = it_fldcat

gt_outtab = <dyn_table>.

SET PF-STATUS '0001'.

SET TITLEBAR '000'.

ENDMODULE.

MODULE user_command_0100 INPUT.

CASE sy-ucomm.

WHEN 'BACK'.

LEAVE PROGRAM.

WHEN 'EXIT'.

LEAVE PROGRAM.

WHEN 'RETURN'.

LEAVE PROGRAM.

WHEN OTHERS.

ENDCASE.

ENDMODULE.

FORM populate_dynamic_itab.

ASSIGN dy_table->* TO <dyn_table>.

CREATE DATA dy_line LIKE LINE OF <dyn_table>.

ASSIGN dy_line->* TO <dyn_wa>.

SELECT *

UP TO 200 rows

FROM SPFLI

INTO CORRESPONDING FIELDS OF TABLE <dyn_table>.

  • save a copy (original table). Use same fcat as ist table.

  • create 2nd Dyn table to hold original data

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fldcat

IMPORTING

ep_table = dy_table.

ASSIGN dy_table->* TO <orig_table>.

CREATE DATA dy_line LIKE LINE OF <orig_table>.

ASSIGN dy_line->* TO <dyn_wa>.

<orig_table> = <dyn_table>.

ENDFORM.

FORM DATA_CHANGED

USING

changed_tab

inserted_tab

deleted_tab

modified_cells_tab.

ENDFORM.

FORM process.

  • Orig table is in dynamic table <orig_table>

  • ALV GRID changed table is in <dyn_table>.

Loop AT <orig_table> INTO <dyn_wa>.

  • Do what you want

  • end

ENDLOOP.

ENDFORM.

FORM refresh.

  • change data for example delete some lines.

*DELETE <dyn_table> from 1 to 16.

CALL METHOD z_object->refresh_grid.

ENDFORM.

FORM dubbelklik

USING

e_row TYPE lvc_s_row

e_column TYPE lvc_s_col

es_row_no TYPE lvc_s_roid.

SET TITLEBAR '001'.

i_gridtitle = 'Grid Title Changed'.

CALL METHOD z_object->change_title

EXPORTING

i_gridtitle = i_gridtitle.

PERFORM refresh.

CALL METHOD z_object->set_cursor

EXPORTING

row_id = e_row

column_id = e_column

row_no = es_row_no.

ENDFORM.

Now yoy can sort / delete / insert / or wahtever.

The code is pretty general so it should cover all your needs.

All your application ever needs to do is simply define a structure and fill it.The class will generate the dynamic table and FCAT.

Modify the class to rmeove toolbar buttons wuou don't use or need.

This class aslo includes a download to EXCEL with column headings which I often find useful.

I don't bother with mt_good cells etc etc as you can manipulate the <dyn_table> directly.

Cheers

Jimbo

Read only

0 Likes
1,853

Hi Jimbo,

thanks a lot for your help.

Our application runs now since more than a year and we use the ALV functionalty quiet deep. Due to our strict change procedure and necessary effort I will not be able to switch to your smart class.

My intention was to find an easy solution, which can be implemented quickly and where the tests can be restricted just to the changed matter. To implement your class would mean to us, that the whole report would have to be tested from scratch.

I would be very thankful for an easier solution, which would avoid the autosort for a while or similar.

Nevertheless thanks a lot for your suggestion.

Kind regards,

Horst

Read only

0 Likes
1,853

Any answer received ? because it occurs that we do have the same kind of problem here ...

Thx for the feedback