简体   繁体   中英

How to delete DDIC table records which have different id than row number in internal table?

I have an ALV with two rows. I want to delete these rows in internal table and dictionary table also. To get which rows in alv i chose, i use a method

go_selections = go_salv->get_selections( ).
go_rows = go_selections->get_selected_rows( )

Nextly, i am iterating through results LOOP AT go_rows INTO gv_row.

Inside above loop I have an another loop, which stores data from internal table into workarea. Then, i set the counter variable which holds the id of the dictionary table and delete respective row.

LOOP AT gr_data INTO lr_znewfdkey6.
     counter2 = lr_znewfdkey6-id.
              IF counter2 EQ gv_row.
                DELETE FROM znew_fdkey01 WHERE id EQ lr_znewfdkey6-id.
                MESSAGE 'Row deleted .' TYPE 'I'.

But unfortunately this works only when id of the dictionary table is equal to row number selected in alv. If I have lr_znewfdkey6-id in dictionary table, equal to for example 5, get_selected_rows( ) returns value started by one etc., and this will cause inequality.

How to fix this?

Get selected rows returns a table of line numbers.

lt_rows = lo_selections->get_selected_rows( ).

Those numbers correspond directly to the itab you loaded into the ALV. No matter if it has been sorted or filtered. It does not correspond to any fields in the database like an ID field or anything.

Assuming gr_data is the itab assigned to the ALV. Let's loop lt_rows and read gr_data at index

  LOOP AT lt_rows ASSIGNING FIELD-SYMBOL(<row>).
    READ TABLE gr_data INTO ls_data INDEX <row>.
    IF sy-subrc = 0.
      APPEND ls_data TO lt_selected.
    ENDIF.
  ENDLOOP.

After executing this will collect selected gr_data lines into lt_selected itab. To delete

LOOP AT lt_selected ASSIGNING FIELD-SYMBOL(<row>).
   DELETE TABLE gr_data FROM <row>.
ENDLOOP.

You could also simply do:

  LOOP AT lt_rows ASSIGNING FIELD-SYMBOL(<row>).
    DELETE gr_data INDEX <row>.
  ENDLOOP.

After that refresh your ALV. Should be good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM