Program to Test Line Selection & Scrolling within Document

ABAP Questions:
I have generated a list containing say 20 lines,  I need to display a checkbox on each line.  Based on the user selection on the checkbox I need to capture the lines choosed by the user.  How to do this?
 
*Program to Test Line Selection & Scrolling within Document:Rajiv
*                Creating Text file of selected data
* carton selection
*-----------------------------------------------------------
REPORT ZRJ001
LINE-SIZE 120 LINE-COUNT 60 NO STANDARD PAGE HEADING.
*------------------------------------------------
* Defining Tables
*-----------------------------------------------
TABLES : ZPACK,ZTRN.

*------------------------------------------------
* Defining Internal Tables
*-----------------------------------------------
*DATA : IZPACK LIKE ZPACK OCCURS 0 WITH HEADER LINE.
DATA : BEGIN OF IZPACK OCCURS 0,
        ZPKSLIP_NO LIKE ZPACK-ZPKSLIP_NO,
        ZBATCH_NO  LIKE ZPACK-ZBATCH_NO,
        ZCARTON_NO LIKE ZPACK-ZCARTON_NO,
        ZDATE      LIKE ZPACK-ZDATE,
        ZMATNR     LIKE ZPACK-ZMATNR,
        ZGROSS_WT  LIKE ZPACK-ZGROSS_WT,
        ZTARE_WT   LIKE ZPACK-ZTARE_WT,
        ZNET_WT    LIKE ZPACK-ZNET_WT,
        ZGRADE     LIKE ZPACK-ZGRADE,
        ZTRNTYP    LIKE ZPACK-ZTRN_TYP,
       END OF IZPACK.
*For Screen Command
DATA : BEGIN OF SCR1_OK_CODE OCCURS 0,
        OK_CODE LIKE SY-UCOMM,
       END OF SCR1_OK_CODE.

DATA : BEGIN OF SCR2_OK_CODE OCCURS 0,
        OK_CODE LIKE SY-UCOMM,
       END OF SCR2_OK_CODE.
*For Text file data
DATA : BEGIN OF ISEL OCCURS 0,
        ZBATCH_NO  LIKE ZPACK-ZBATCH_NO,
        ZCARTON_NO LIKE ZPACK-ZCARTON_NO,
        ZDATE      LIKE ZPACK-ZDATE,
        ZMATNR     LIKE ZPACK-ZMATNR,
        ZGROSS_WT  LIKE ZPACK-ZGROSS_WT,
        ZTARE_WT   LIKE ZPACK-ZTARE_WT,
        ZNET_WT    LIKE ZPACK-ZNET_WT,
       END OF ISEL.


*------------------------------------------------
* variables / constants
*-----------------------------------------------
DATA: MHD1 LIKE SY-TITLE,
      MHD2 LIKE SY-TITLE,
      MHD3 LIKE SY-TITLE,
      CH(1) TYPE C VALUE '|',
      CHK_BOX,
      CHK_BOX_VAL,
      LINE_NO LIKE SY-LINNO,
      PAGE_NO LIKE SY-PAGNO,
      SEL_CART TYPE I,
      IZPACK_LINE TYPE I,
      NEW_SCREEN,
      SRNO TYPE I.
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS: ZSHIP_PT FOR ZPACK-ZSHIP_PT NO-EXTENSION NO INTERVALS
                          OBLIGATORY DEFAULT '5102'.
*                ZMERGE FOR ZPACK-ZMERGE_NO. "Merge No.
SELECTION-SCREEN END OF BLOCK B1.

AT SELECTION-SCREEN.
  IF SY-UCOMM NE '%001'. "Multiple Selection
    IF ZSHIP_PT IS INITIAL.
      MESSAGE E055(00).
    ENDIF.
*    IF ZMERGE IS INITIAL.
      SELECT * FROM ZPACK INTO CORRESPONDING FIELDS OF TABLE IZPACK
               WHERE ZSHIP_PT IN ZSHIP_PT.
*    ELSE.
*      SELECT * FROM ZPACK INTO CORRESPONDING FIELDS OF TABLE IZPACK
*               WHERE ZSHIP_PT IN ZSHIP_PT AND ZMERGE_NO IN ZMERGE.
*    ENDIF.
    IF SY-SUBRC NE 0.
      MESSAGE E058(00) WITH ZSHIP_PT.
    ENDIF.
  ENDIF.
*At Line Selection
*-----------------
AT LINE-SELECTION.
  PERFORM SELECT_CURRENT_LINE.

*Top of Page Operation
*---------------------
TOP-OF-PAGE.
  MHD1 = 'WELSPUN SYNTEX LIMITED'.
  MHD2 = 'Data Selection from Displayed Document.'.
  MHD3 = 'Select Challan to Make selected List'.
  PERFORM DATA_HEADER.
  SET LEFT SCROLL-BOUNDARY COLUMN 15.

*User Command Choosen
*---------------------
AT USER-COMMAND.
  CASE SY-UCOMM.
    WHEN 'LEFT'.
      SCROLL LIST LEFT.
    WHEN 'RIGH'.
      SCROLL LIST RIGHT.
    WHEN 'TOP'.
      SCROLL LIST TO FIRST PAGE.
    WHEN 'BOTT'.
      SCROLL LIST TO LAST PAGE.
    WHEN 'PREV'.
      SCROLL LIST BACKWARD.
    WHEN 'NEXT'.
      SCROLL LIST FORWARD.
    WHEN 'SELA'.
      PERFORM SELECT_DESELECT_ALL USING 'X'.
    WHEN 'SELD'.
      PERFORM SELECT_DESELECT_ALL USING 'SPACE'.
    WHEN 'SELC'.
      PERFORM SELECT_CURRENT_LINE.
    WHEN 'CONT'.
      PERFORM GENERATE_INSPECTION_LIST USING 'X'.
    WHEN 'LIST'.
      PERFORM GENERATE_INSPECTION_LIST USING SPACE.
    WHEN 'FILE'.
      PERFORM GENERATE_INSPECTION_LIST USING 'Y'.
  ENDCASE.

* SCROLL LIST TO PAGE pag.
* SCROLL LIST TO COLUMN col.

START-OF-SELECTION.
  PERFORM LIST_OK_CODE.
  SET PF-STATUS 'CARTLIST' EXCLUDING SCR2_OK_CODE.
  PERFORM SELECT_DATA.
  PERFORM LIST_DATA.

END-OF-SELECTION.

*&---------------------------------------------------------------------*
*&      Form  LIST_OK_CODE
*&---------------------------------------------------------------------*
FORM LIST_OK_CODE. " Only for EXCLUDING OPTION
  APPEND 'LIST' TO SCR1_OK_CODE.
  APPEND 'SELA' TO SCR1_OK_CODE.
  APPEND 'SELD' TO SCR1_OK_CODE.
  APPEND 'SELC' TO SCR1_OK_CODE.
  APPEND 'CONT' TO SCR1_OK_CODE.
  APPEND 'FILE' TO SCR1_OK_CODE.
  APPEND 'LEFT' TO SCR2_OK_CODE.
  APPEND 'RIGH' TO SCR2_OK_CODE.
  APPEND 'TOP'  TO SCR2_OK_CODE.
  APPEND 'BOTT' TO SCR2_OK_CODE.
  APPEND 'PREV' TO SCR2_OK_CODE.
  APPEND 'NEXT' TO SCR2_OK_CODE.
ENDFORM.                    " LIST_OK_CODE

*&---------------------------------------------------------------------*
*&      Form  SELECT_DATA
*&---------------------------------------------------------------------*
FORM SELECT_DATA.
*Data already selected when checking.
ENDFORM.                    " SELECT_DATA

*&---------------------------------------------------------------------*
*&      Form  LIST_DATA
*&---------------------------------------------------------------------*
FORM LIST_DATA.
  SORT IZPACK BY ZPKSLIP_NO ZBATCH_NO.
  LOOP AT IZPACK.
*    AT NEW ZPKSLIP_NO.
*      FORMAT COLOR 5 INVERSE.
*      WRITE: /02  CHK_BOX AS CHECKBOX, 'Challan No :' COLOR 6
*                  INTENSIFIED, IZPACK-ZPKSLIP_NO hotspot on.
*      FORMAT RESET.
*    ENDAT.
    FORMAT COLOR 1 INVERSE.
    WRITE:/ CHK_BOX AS CHECKBOX.
    WRITE:  CH NO-GAP ,(13) IZPACK-ZCARTON_NO UNDER 'Carton No.' NO-GAP.
    WRITE:  CH NO-GAP ,(10) IZPACK-ZDATE    UNDER 'Packing Date' NO-GAP.
    WRITE:  CH NO-GAP ,(10) IZPACK-ZGROSS_WT UNDER 'Gross Qty.'  NO-GAP.
    WRITE:  CH NO-GAP ,(10) IZPACK-ZTARE_WT UNDER 'Tare Qty.'    NO-GAP.
    WRITE:  CH NO-GAP ,(10) IZPACK-ZNET_WT  UNDER 'Net Qty.'     NO-GAP.
    WRITE:  CH NO-GAP ,(18) IZPACK-ZMATNR   UNDER 'Material No.' NO-GAP.
    WRITE:  CH NO-GAP ,(12) IZPACK-ZBATCH_NO UNDER 'Lot No.' NO-GAP.
    WRITE:  CH NO-GAP ,(5) IZPACK-ZGRADE   UNDER 'Grade'         NO-GAP.
    WRITE:  CH NO-GAP ,(5) IZPACK-ZTRNTYP UNDER 'Type'          NO-GAP.
    WRITE: '|'.
    FORMAT RESET.

  ENDLOOP.
ENDFORM.                    " LIST_DATA

*&---------------------------------------------------------------------*
*&      Form  DATA_HEADER
*&---------------------------------------------------------------------*
FORM DATA_HEADER.
  FORMAT COLOR COL_GROUP.
  CALL FUNCTION 'Z_YHEAD_PRINT'
       EXPORTING
            TITLE1 = MHD1
            TITLE2 = MHD2
            TITLE3 = MHD3
            COLOR  = 'X'
       EXCEPTIONS
            OTHERS = 1.
  FORMAT RESET.
  FORMAT COLOR COL_HEADING.
  WRITE :/02 'Carton No.',
          17 'Packing Date',
          30 'Gross Qty.',
          45 'Tare Qty.',
          60 'Net Qty.',
          75 'Material No.',
          95 'Lot No.',
          105 'Grade',
          112 'Type'.
  ULINE.
  FORMAT RESET.
ENDFORM.                    " DATA_HEADER

*&---------------------------------------------------------------------*
*&      Form  SELECT_CURRENT_LINE
*&---------------------------------------------------------------------*
FORM SELECT_CURRENT_LINE.
  READ CURRENT LINE FIELD VALUE CHK_BOX INTO CHK_BOX_VAL.
  IF CHK_BOX_VAL IS INITIAL.
    CHK_BOX_VAL = 'X'.
  ELSE.
    CHK_BOX_VAL = SPACE.
  ENDIF.
  MODIFY CURRENT LINE FIELD VALUE CHK_BOX FROM CHK_BOX_VAL.

ENDFORM.                    " SELECT_CURRENT_LINE

*&---------------------------------------------------------------------*
*&      Form  SELECT_DESELECT_ALL
*&---------------------------------------------------------------------*
*      -->P_0245   text
*----------------------------------------------------------------------*
*FORM SELECT_DESELECT_ALL USING    VALUE(P_0245).
FORM SELECT_DESELECT_ALL USING P_VALUE.
  LINE_NO = 8.
  PAGE_NO = 1.
  DO.
    IF LINE_NO GT SY-LINCT.
      LINE_NO = 8.
      ADD 1 TO PAGE_NO.
    ENDIF.
    READ LINE LINE_NO OF PAGE PAGE_NO.
    MODIFY LINE LINE_NO OF PAGE PAGE_NO
      FIELD VALUE CHK_BOX FROM P_VALUE.
    IF SY-SUBRC NE 0.
      EXIT.
    ENDIF.
    ADD 1 TO LINE_NO.
  ENDDO.

ENDFORM.                    " SELECT_DESELECT_ALL

*&---------------------------------------------------------------------*
*&      Form  GENERATE_INSPECTION_LIST
*&---------------------------------------------------------------------*
FORM GENERATE_INSPECTION_LIST USING P_CHECK.
  LINE_NO = 8.
  PAGE_NO = 1.
  CLEAR:NEW_SCREEN,SRNO,SEL_CART.
  DO.
    IF LINE_NO GT SY-LINCT.
      LINE_NO = 8.
      ADD 1 TO PAGE_NO.
    ENDIF.
    READ LINE LINE_NO OF PAGE PAGE_NO FIELD
      VALUE CHK_BOX INTO CHK_BOX_VAL.
    IF SY-SUBRC EQ 0.
      IF CHK_BOX_VAL = 'X'.
        ADD 1 TO SEL_CART.
        IF P_CHECK EQ ' '. " Only Checking
          PERFORM CREATE_LIST.
        ENDIF.
        IF P_CHECK = 'Y'. "Replace data to table ISEL for text file
          MOVE : IZPACK-ZBATCH_NO TO ISEL-ZBATCH_NO,
                 IZPACK-ZMATNR TO ISEL-ZMATNR,
                 IZPACK-ZCARTON_NO TO ISEL-ZCARTON_NO,
                 IZPACK-ZDATE TO ISEL-ZDATE,
                 IZPACK-ZGROSS_WT TO ISEL-ZGROSS_WT,
                 IZPACK-ZTARE_WT TO ISEL-ZTARE_WT,
                 IZPACK-ZNET_WT TO ISEL-ZNET_WT.
          APPEND ISEL.
        ENDIF.
      ENDIF.
      CHK_BOX_VAL = SPACE.
    ELSE.
      EXIT.
    ENDIF.
    ADD 1 TO LINE_NO.
  ENDDO.
  IF SEL_CART = 0.
    MESSAGE S398(00) WITH 'Please Select the Carton first'.
  ELSE.
    MESSAGE S398(00) WITH SEL_CART 'Cartons were selected'.
    IF P_CHECK = 'Y'.
      PERFORM CREATE_FILE. "To Created Text file of Selected Cartons
    ENDIF.
  ENDIF.
ENDFORM.                    " GENERATE_INSPECTION_LIST

*&---------------------------------------------------------------------*
*&      Form  CREATE_LIST
*&---------------------------------------------------------------------*
FORM CREATE_LIST.
  IF NEW_SCREEN IS INITIAL.
    NEW_SCREEN = 'X'.
    SET TITLEBAR 'INSP_LIST'.
    SET PF-STATUS 'CARTLIST' EXCLUDING SCR1_OK_CODE.
    MHD1 = 'WELSPUN SYNTEX LIMITED'.
    MHD2 = 'ZPACK Data For Scroll Test.'.
    MHD3 = 'List of Selected Cartons'.
    PERFORM DATA_HEADER.
  ENDIF.
  IF SY-LINNO = 1.
*    PERFORM HEADER2.
  ENDIF.

  IZPACK_LINE = LINE_NO - 7.
  READ TABLE IZPACK INDEX IZPACK_LINE.
  ADD 1 TO SRNO.
  FORMAT COLOR 6 INVERSE.
  WRITE:/ CH NO-GAP ,(13) IZPACK-ZCARTON_NO UNDER 'Carton No.' NO-GAP.
  WRITE:  CH NO-GAP ,(10) IZPACK-ZDATE    UNDER 'Packing Date' NO-GAP.
  WRITE:  CH NO-GAP ,(10) IZPACK-ZGROSS_WT UNDER 'Gross Qty.'  NO-GAP.
  WRITE:  CH NO-GAP ,(10) IZPACK-ZTARE_WT UNDER 'Tare Qty.'    NO-GAP.
  WRITE:  CH NO-GAP ,(10) IZPACK-ZNET_WT  UNDER 'Net Qty.'     NO-GAP.
  WRITE:  CH NO-GAP ,(18) IZPACK-ZMATNR   UNDER 'Material No.' NO-GAP.
  WRITE:  CH NO-GAP ,(12) IZPACK-ZBATCH_NO UNDER 'Lot No.'     NO-GAP.
  WRITE:  CH NO-GAP ,(5) IZPACK-ZGRADE   UNDER 'Grade'         NO-GAP.
  WRITE:  CH NO-GAP ,(5) IZPACK-ZTRNTYP UNDER 'Type'           NO-GAP.
  WRITE: '|'.
  WRITE:/ SY-ULINE(110).
  FORMAT RESET.
*WRITE :/ SY-PFKEY.
ENDFORM.                    " CREATE_LIST

*&---------------------------------------------------------------------*
*&      Form  CREATE_FILE
*&---------------------------------------------------------------------*
FORM CREATE_FILE.
  DATA: BEGIN OF ITAB_FIELDS OCCURS 0,
        IFIELD_NAME(15) TYPE C,
        END OF ITAB_FIELDS.

  DATA: MNAME(4) TYPE C,
        G_ROW TYPE I,
        G_FILENAME LIKE RLGRAP-FILENAME. "Defining Filename.

  LOOP AT ISEL.
    G_ROW = G_ROW + 1.
  ENDLOOP.
  CONCATENATE 'C:\CHAL' SY-DATUM+6(2) SY-DATUM+4(2) '.DAT' INTO
  G_FILENAME.
  MESSAGE S899(MM) WITH 'Created file' G_FILENAME.
* APPEND FIELD NAME
  MOVE G_ROW TO ITAB_FIELDS-IFIELD_NAME.
  APPEND ITAB_FIELDS.
  CLEAR ITAB_FIELDS.

* START DOWNLOAD
  CALL FUNCTION 'WS_DOWNLOAD'
      EXPORTING
           FILENAME                = G_FILENAME
           FILETYPE                = 'DAT'
           COL_SELECT              = ' '
           COL_SELECTMASK          = ';'
       TABLES
            DATA_TAB                = ISEL
            FIELDNAMES              = ITAB_FIELDS
      EXCEPTIONS
           FILE_OPEN_ERROR         = 1
           FILE_WRITE_ERROR        = 2
           INVALID_FILESIZE        = 3
           INVALID_TYPE            = 4
           NO_BATCH                = 5
           UNKNOWN_ERROR           = 6
           INVALID_TABLE_WIDTH     = 7
           GUI_REFUSE_FILETRANSFER = 8
           CUSTOMER_ERROR          = 9
           OTHERS                  = 10
            .
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

* END OF DOWNLOAD
  MESSAGE S899(MM) WITH 'Created file' G_FILENAME.

ENDFORM.                    " CREATE_FILE
With Compliments: Rajiv Ranjan Singh

ABAP Tips

Fast Links:
Get help for your ABAP problems
Do you have a ABAP Question?

ABAP Books
ABAP Certification, BAPI, Java, Web Programming, Smart Forms, Sapscripts Reference Books

ABAP Tips
ABAP Forum for Discussion and Samples Program Codes for Abapers

Best regards,
SAP Basis, ABAP Programming and Other IMG Stuff
http://www.erpgreat.com

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.