{"id":789,"date":"2013-11-01T11:51:44","date_gmt":"2013-11-01T10:51:44","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=789"},"modified":"2013-11-17T09:23:27","modified_gmt":"2013-11-17T08:23:27","slug":"abap-append-a-component-to-itab-dynamically","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=789","title":{"rendered":"ABAP &#8211; append a component to ITAB dynamically"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"358\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=358\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" data-orig-size=\"44,50\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Picasa&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1365690880&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"SAP\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" class=\"size-full wp-image-358 alignleft\" alt=\"SAP\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" width=\"44\" height=\"50\" \/>It&#8217;s not a trivial thing to add new field into already existing itab during run time, but there is a way how to modify the ITAB strucuture. You can add, remove or modify order of the components (fields) in the itab.\u00a0<!--more-->Let&#8217;s assume you have your itab created as data source for ALV grid. This itab is generated dynamically from a field catalog.<\/p>\n<p>But after this itab is generated, you would like to add some columns. In my case it was cell coloring information.<br \/>\nClass instance attributes:<\/p>\n<pre lang=\"abap\">mr_data     TYPE REF TO data,\r\nmt_fieldcat TYPE lvc_t_fcat.<\/pre>\n<p>I assume the field catalog has already been created. Now I create the itab dynamically and I append a &#8216;CELL_COLORS&#8217; column as additional field (of type LVC_T_SCOL &#8211; Internal table)<\/p>\n<pre lang=\"abap\">METHOD load_data.\r\n  DATA:\r\n    lr_sdescr    TYPE REF TO cl_abap_structdescr,\r\n    lr_tdescr    TYPE REF TO cl_abap_tabledescr,\r\n    lr_data      TYPE REF TO data,\r\n    lr_data_line TYPE REF TO data,\r\n\r\n    lt_components    TYPE abap_component_tab,\r\n    l_lvc_t_scol type lvc_t_scol.\r\n\r\n  FIELD-SYMBOLS:\r\n    &lt;fs_table&gt; TYPE STANDARD TABLE,\r\n    &lt;fs_comp&gt; TYPE abap_componentdescr.\r\n\r\n* Create itab from field catalog\r\n  IF mr_data IS INITIAL.\r\n    CALL METHOD cl_alv_table_create=&gt;create_dynamic_table\r\n      EXPORTING\r\n        it_fieldcatalog = mt_fieldcat\r\n      IMPORTING\r\n        ep_table        = lr_data.\r\n\r\n*   Create dynamic line of this itab\r\n    ASSIGN lr_data-&gt;* TO &lt;fs_table&gt;.\r\n    CREATE DATA lr_data_line LIKE LINE OF &lt;fs_table&gt;.\r\n\r\n*   We are creating components of the new\/updated ITAB\r\n    APPEND INITIAL LINE TO lt_components ASSIGNING &lt;fs_comp&gt;.\r\n\r\n*   We get description of the current itab\r\n    &lt;fs_comp&gt;-type ?= cl_abap_structdescr=&gt;describe_by_data_ref( lr_data_line ).\r\n*   We assign some dummy name (will not be used anyhow)\r\n    &lt;fs_comp&gt;-name = 'DATA'.\r\n\r\n*   Include all fields (result will be fields of new itab\r\n*   If 'as_include' = abap_false, then all fields of the structure \r\n*   would be added in our new itab as field of type \r\n*   \"Internal table\" with name 'DATA'\r\n    &lt;fs_comp&gt;-as_include = abap_true.\r\n\r\n*   Now we're adding new component of type \r\n*   LVC_T_SCOL - Internal table\r\n    APPEND INITIAL LINE TO lt_components ASSIGNING &lt;fs_comp&gt;.\r\n    &lt;fs_comp&gt;-type ?= cl_abap_structdescr=&gt;describe_by_data( l_lvc_t_scol ).\r\n    &lt;fs_comp&gt;-name = 'CELL_COLORS'.\r\n\r\n*   We create reference to a line description of the new itab\r\n    lr_sdescr  = cl_abap_structdescr=&gt;create( lt_components ).\r\n*   ...we create reference to the new ITAB description \r\n    lr_tdescr  = cl_abap_tabledescr=&gt;create( lr_sdescr ).\r\n\r\n*   Finally we create our new ITAB, where \r\n*   1. all fields from fieldcat are included\r\n*   2. New field of type LCV_T_SCOL is appended\r\n    CREATE DATA mr_data TYPE HANDLE lr_tdescr.\r\n  ENDIF.<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s not a trivial thing to add new field into already existing itab during run time, but there is a way how to modify the ITAB strucuture. You can add, remove or modify order of the components (fields) in the &hellip; <a href=\"https:\/\/oprsteny.cz\/?p=789\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[16,9],"tags":[446,216,215,213,214,217,218,219,220,29,212],"class_list":["post-789","post","type-post","status-publish","format-standard","hentry","category-abap","category-development","tag-abap","tag-abap_componentdescr","tag-abap_component_tab","tag-cl_abap_structdescr","tag-cl_abap_tabledescr","tag-cl_alv_table_create","tag-create_dynamic_table","tag-describe_by_data","tag-describe_by_data_ref","tag-dynamic","tag-structures"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-cJ","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/789","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=789"}],"version-history":[{"count":1,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/789\/revisions"}],"predecessor-version":[{"id":790,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/789\/revisions\/790"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}