{"id":843,"date":"2013-11-17T18:40:01","date_gmt":"2013-11-17T17:40:01","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=843"},"modified":"2013-11-17T18:50:04","modified_gmt":"2013-11-17T17:50:04","slug":"alv-tutorial-edit-save-changes","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=843","title":{"rendered":"ALV tutorial 11 &#8211; Edit &#038; Save changes"},"content":{"rendered":"<p>In this tutorial I&#8217;ll show how to enable only the specific cells of a grid to be editable.<!--more--><\/p>\n<p>First I&#8217;ll prepare some demonstration data. It will be a list of materials and their maintenance status. If status is set to &#8216;A&#8217; we would like to disable editing of status in the grid.<\/p>\n<table>\n<tbody>\n<tr>\n<th>MATNR (type MATNR, Primary key)<\/th>\n<th>PSTAT (type PSTAT_D)<\/th>\n<\/tr>\n<tr>\n<td>123456<\/td>\n<td>K<\/td>\n<\/tr>\n<tr>\n<td>654321<\/td>\n<td>E<\/td>\n<\/tr>\n<tr>\n<td>567890<\/td>\n<td>D<\/td>\n<\/tr>\n<tr>\n<td>876543<\/td>\n<td>B<\/td>\n<\/tr>\n<tr>\n<td>900000<\/td>\n<td>A<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Now we create a function group with screen (i.e. 0200) where we&#8217;ll draw our ALV grid.<br \/>\nIn the screen layout editor create a custom container and name it i.e. <em>GO_GRID_CONTAINER<\/em>.<br \/>\nAlso create a custom PF-STATUS with two functions: UPDATE and REFRESH<\/p>\n<p>The flow logic for the screen will be simple:<\/p>\n<pre lang=\"abap\">PROCESS BEFORE OUTPUT.\r\n  MODULE pbo_0200.\r\n*\r\nPROCESS AFTER INPUT.\r\n  MODULE pai_0200.<\/pre>\n<p>The PBO_0200 module is used to gather data, create field catalog and display the ALV. Here follows its code<\/p>\n<pre lang=\"abap\">MODULE pbo_0200 OUTPUT.\r\n  DATA:\r\n    ls_layout  TYPE lvc_s_layo,\r\n    ls_variant TYPE disvariant.\r\n\r\n  FIELD-SYMBOLS:\r\n    &lt;fs_fcat&gt; TYPE lvc_s_fcat.\r\n\r\n* In this user status we create two functions:\r\n* UPDATE - save all changes and refresh grid\r\n* REFRESH - refresh grid with data from Z-table\r\n  SET PF-STATUS 'PF_STATUS'.\r\n\r\n* Initial creation of container and grid\r\n  IF gr_container IS INITIAL.\r\n    CREATE OBJECT gr_container\r\n      EXPORTING\r\n*       This is reference to the object we made in Layout editor\r\n        container_name = 'GO_GRID_CONTAINER'.\r\n\r\n*   Parent is our container created above\r\n    CREATE OBJECT gr_grid\r\n      EXPORTING\r\n        i_parent = gr_container.\r\n\r\n*   Simplest field catalog creation\r\n    CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'\r\n      EXPORTING\r\n        i_structure_name = 'ZMATSTAT'\r\n      CHANGING\r\n        ct_fieldcat      = gt_fieldcat.\r\n\r\n*   !!! IMPORTANT !!!\r\n*   We have to mark all required columns as editable !!!\r\n    READ TABLE gt_fieldcat ASSIGNING &lt;fs_fcat&gt; WITH KEY fieldname = 'PSTAT'.\r\n    &lt;fs_fcat&gt;-edit = abap_true.\r\n\r\n*   Load data from database and mark the\r\n*   appropriate field as R\/O\r\n    PERFORM reload_data.\r\n\r\n*   Set the field name carrying info about cell styles (RW \/ RO)\r\n    ls_layout-stylefname = 'CELL_STYLES'.\r\n\r\n    ls_variant-report   = sy-repid.\r\n    ls_variant-username = sy-uname.\r\n\r\n*   !!! IMPORTANT !!!\r\n*   We register the ENTER event so the manual changes\r\n*   are propagated back to GT_DATA\r\n    gr_grid-&gt;register_edit_event( i_event_id = cl_gui_alv_grid=&gt;mc_evt_enter ).\r\n\r\n*   Draw the ALV\r\n    gr_grid-&gt;set_table_for_first_display(\r\n       EXPORTING\r\n         is_layout             = ls_layout\r\n         is_variant            = ls_variant\r\n         i_save                = 'A'\r\n         i_default             = 'X'\r\n       CHANGING\r\n         it_fieldcatalog       = gt_fieldcat\r\n         it_outtab             = gt_data ).\r\n  ENDIF.\r\nENDMODULE.                 \" PBO_0200  OUTPUT<\/pre>\n<p>Module PAI_0200 is used to process user interaction and here follows its code<\/p>\n<pre lang=\"abap\">MODULE pai_0200 INPUT.\r\n  DATA:\r\n    l_matstat    TYPE zmatstat.\r\n  FIELD-SYMBOLS:\r\n    &lt;fs_data&gt; LIKE LINE OF gt_data.\r\n\r\n  CASE sy-ucomm.\r\n    WHEN 'BACK' OR 'LEAVE' OR 'CANCEL'.\r\n      SET SCREEN 0.\r\n\r\n    WHEN 'UPDATE'.\r\n*    Update DB Z-table\r\n      LOOP AT gt_data ASSIGNING &lt;fs_data&gt; WHERE matnr IS NOT INITIAL.\r\n        MOVE-CORRESPONDING &lt;fs_data&gt; TO l_matstat.\r\n        MODIFY zmatstat FROM l_matstat.\r\n      ENDLOOP.\r\n\r\n*     Reload data database and mark the\r\n*     appropriate fields as R\/O\r\n      PERFORM reload_data.\r\n      gr_grid-&gt;refresh_table_display( ).\r\n\r\n    WHEN 'REFRESH'.\r\n      PERFORM reload_data.\r\n      gr_grid-&gt;refresh_table_display( ).\r\n  ENDCASE.\r\nENDMODULE.                 \" PAI_0200  INPUT<\/pre>\n<p>The helper FORM <em>RELOAD_DATA<\/em> is used to gather data from database and mark the appropriate fields as read\/only<\/p>\n<pre lang=\"abap\">FORM reload_data.\r\n  DATA:\r\n    l_lvc_s_styl TYPE lvc_s_styl.\r\n  FIELD-SYMBOLS:\r\n    &lt;fs_data&gt; LIKE LINE OF gt_data.\r\n\r\n  CLEAR gt_data[].\r\n* Read the data\r\n  SELECT *\r\n    INTO CORRESPONDING FIELDS OF TABLE gt_data\r\n    FROM zmatstat.\r\n\r\n* Loop over all rows of data and set PSTAT\r\n* as R\/O in case its value = 'A'\r\n  LOOP AT gt_data\r\n    ASSIGNING &lt;fs_data&gt;\r\n    WHERE pstat = 'A'.\r\n\r\n    l_lvc_styl-style = cl_gui_alv_grid=&gt;mc_style_disabled.\r\n    l_lvc_styl-fieldname = 'PSTAT'.\r\n\r\n*   INSERT command MUST be used \r\n*   because field CELL_STYLES is a SORTED TABLE\r\n    INSERT l_lvc_styl INTO TABLE &lt;fs_data&gt;-cell_styles.\r\n  ENDLOOP.\r\nENDFORM.                    \"reload_data<\/pre>\n<p>If we now call our screen from ABAP, we&#8217;ll get the following:<\/p>\n<p><a href=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TUTORIAL_11_01.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"846\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=846\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TUTORIAL_11_01.png\" data-orig-size=\"287,225\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&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=\"Edit cells + disable some of them\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TUTORIAL_11_01.png\" class=\"size-full wp-image-846 alignnone\" alt=\"Edit cells + disable some of them\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TUTORIAL_11_01.png\" width=\"287\" height=\"225\" \/><\/a><\/p>\n<p>If we modify some data, change the value of PSTAT in some row to &#8216;A&#8217; and save the changes:<\/p>\n<p><a href=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TUTORIAL_11_02.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"847\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=847\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TUTORIAL_11_02.png\" data-orig-size=\"295,226\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&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=\"Edit cells &amp;#8211; status after manual changes are saves\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TUTORIAL_11_02.png\" class=\"size-full wp-image-847 alignnone\" alt=\"Edit cells - status after manual changes are saves\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TUTORIAL_11_02.png\" width=\"295\" height=\"226\" \/><\/a><\/p>\n<p>&#8230;and data in database:<\/p>\n<p><a href=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TUTORIAL_11_03.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"848\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=848\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TUTORIAL_11_03.png\" data-orig-size=\"477,246\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&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=\"Corresponding data is updated in database\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TUTORIAL_11_03.png\" class=\"size-medium wp-image-848 alignnone\" alt=\"Corresponding data is updated in database\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TUTORIAL_11_03-300x154.png\" width=\"300\" height=\"154\" srcset=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TUTORIAL_11_03-300x154.png 300w, https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TUTORIAL_11_03.png 477w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial I&#8217;ll show how to enable only the specific cells of a grid to be editable.<\/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,86,136,9,222],"tags":[446,210,227,226,231,232,229,230,228],"class_list":["post-843","post","type-post","status-publish","format-standard","hentry","category-abap","category-alv","category-alv-tutorial","category-development","category-dynpro","tag-abap","tag-cl_gui_alv_grid","tag-cl_gui_custom_container","tag-lvc_fieldcatalog_merge","tag-lvc_s_styl","tag-lvc_t_styl","tag-mc_evt_enter","tag-mc_style_disabled","tag-register_edit_event"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-dB","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/843","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=843"}],"version-history":[{"count":8,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/843\/revisions"}],"predecessor-version":[{"id":854,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/843\/revisions\/854"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}