Agregar nuevo campos dinámicos a una tabla interna

  typesbegin of lty_knbkk,
           kunnr type kunnr,
           banks type banks,
           bankl type bankk,
           banka type banka,
         end of lty_knbkk,

         begin of lty_descri,
           valor1 type  zosed_valor,
           valor2 type  zosed_valor,
         end of lty_descri.

  dataltd_bank     type standard table of lty_knbkk,
        ltd_bank_aux type standard table of lty_knbkk,
        l_typedesc   type ref to cl_abap_typedescr,
        l_struc      type ref to cl_abap_structdescr,
        gs_fcat      like line of gt_fcat,
        lt_comp      type abap_compdescr_tab,
        w_comp       like line of lt_comp,
        gp_table     type ref to data,
        gp_line      type ref to data,
        ls_pos       type i,
        l_sytabix    like sy-tabix,
        ltd_descri   type sorted table of lty_descri with non-unique key valor1.

  field-symbols<ls_bank>   type lty_knbkk,
                 <itab_line> type any,
                 <field>     type any,
                 <fs>        type any,
                 <ls_descri> type lty_descri,
                 <line_new>  type any" New line
                 <fs_new>    type any" New field value.

La cantidad de columnanas a agregar será la máxima de cantidad de bancos existentes por proveedor

Se trae los bancos realacionados por cada proveedor

  select a~kunnr a~banks a~bankl b~banka from knbk as a inner join bnka as b
    on a~banks b~banks and
       a~bankl b~bankl into table ltd_bank
    for all entries in lt_cabletra_tmp
    where a~kunnr eq lt_cabletra_tmp-kunnr.

  ltd_bank_aux[] ltd_bank[].
  sort ltd_bank_aux by banks bankl.
  delete adjacent duplicates from ltd_bank_aux comparing banks bankl.

gs_output es la estructura del ALV

*La cantidad de registros de ltd_bank_aux será la cantidad de columnas a añadir
  assign gs_output to <itab_line>.

*Se traen las descripciones cortas de la tabla ZOSTB_CONSTANTES
  select valor1 valor2 from zostb_constantes into table ltd_descri
     where modulo 'FI' and aplicacion 'ESTADO_CUENTA' and
                             programa 'ZOSFI_RPT_ESTADO_CUENTA' and
                             campo 'BANKL'.

  perform build_fieldcatalog_0100 changing gt_fcat.


Se agrega los nuevo campos dinámicos al catálogo de campos 


  loop at ltd_bank_aux assigning <ls_bank>.
    clear gs_fcat.
    ls_pos 98.
    concatenate 'BANK_' <ls_bank>-bankl into gs_fcat-fieldname.
    gs_fcat-outputlen '000005'.
*    w_comp-type_kind to gs_fcat-inttype,
    gs_fcat-intlen '000005'.
    gs_fcat-checkbox 'X'.
    gs_fcat-edit ''.
    gs_fcat-scrtext_l <ls_bank>-banka.
    gs_fcat-scrtext_m <ls_bank>-banka.

    read table ltd_descri assigning <ls_descri> with key valor1 <ls_bank>-bankl.
    if sy-subrc 0.
      gs_fcat-scrtext_s <ls_descri>-valor2.
    endif.

    add to ls_pos.
    gs_fcat-col_pos ls_pos.
    append gs_fcat to gt_fcat.
  endloop.


Se crea la tabla interna dinámica


  call method cl_alv_table_create=>create_dynamic_table
    exporting
      it_fieldcatalog           gt_fcat
    importing
      ep_table                  gp_table
    exceptions
      generate_subpool_dir_full 1
      others                    2.

  assign gp_table->to <itab>.
  create data gp_line like line of <itab>.
  assign gp_line->to <line_new>.
  unassign <itab_line>.


Se asginan los valores


  loop at gt_output into gs_output.
* Move value to each component.
    l_sytabix sy-tabix.

    loop at gt_fcat into gs_fcat.
      assign component gs_fcat-fieldname
        of structure <line_new> to <fs_new>.

      case gs_fcat-fieldname.
        when others.
          assign component gs_fcat-fieldname
            of structure gs_output to <fs>.
          if sy-subrc 0.
            move <fs> to <fs_new>.
          else.
            loop at ltd_bank assigning <ls_bank> where kunnr gs_output-kunnr.
              if gs_fcat-fieldname+0(5'BANK_'.
                if gs_fcat-fieldname+5(2<ls_bank>-bankl.
                  <fs_new> 'X'.
                endif.
              endif.
            endloop.
          endif.
      endcase.
    endloop.

    append <line_new> to <itab>.
    clear <line_new>.
  endloop.





Comentarios

Entradas populares