cancel
Showing results for 
Search instead for 
Did you mean: 

How to sum a line in a web dynpro table?

former_member195355
Participant
0 Kudos

Hiya,

I have a web dynpro table line that has a bunch of currency amounts:

I'd like to total these as the user enters the values...

The only way I found was perhaps using onEnter, which gets triggered when the user hits enter:

Good so far but now I'm stuck...

How would I know which line the user has hit enter and so only the total for that line should have the value added to it?

Thanks in advance for any help.

Accepted Solutions (1)

Accepted Solutions (1)

ramakrishnappa
Active Contributor
0 Kudos

Hi Robert,

I thinks its good, if we do totalling for all the rows,which are dirty... i.e... the rows which are modified.

So, that user presses an ENTER key once and he/she can see the total gets computed for all the modified rows

Please refer the below sample code


  DATA lo_node TYPE REF TO if_wd_context_node.
  DATA lt_elements TYPE wdr_context_element_set.
  DATA ls_elements LIKE LINE OF lt_elements.
  DATA ls_data TYPE REF TO wd_this->element_table.

lo_node = wd_context->get_child_node( name = wd_this->wdctx_table ).

"Get the elements

lo_node->get_elements(
    RECEIVING
      set  = lt_elements
  ).

  LOOP AT  lt_elements INTO ls_elements.


    " Process only the changed lines

    IF ls_elements->is_changed_by_client( ) EQ abap_true.


      ls_elements->get_static_attributes(
        IMPORTING
          static_attributes = ls_data
      ).

      "Now write the logic here to sum up the values
      ls_data-total = ls_data-val1 + ls_data-val2 + ls_data-val3.

      " set the value of total
      ls_elements->set_attribute(
        EXPORTING
          value =  ls_data-total   " Attribute Value
          name  =  'TOTAL'
      ).


    ENDIF.
  ENDLOOP.

Note: Replace the node names, attribute names as per your application

Regards,

Rama

former_member195355
Participant
0 Kudos

Thanks and that is very good thinking - update all the rows in case they forget to hit enter.

Brilliant!

former_member195355
Participant
0 Kudos

Sorry Ramakrishnappa,

When creating the onEnter event do you tick transfer UI Even Parameters?

Do you know what this does?

Many thanks!

ramakrishnappa
Active Contributor
0 Kudos

Hi Robert,

The check of "Transfer UI Event Parameters" is not mandatory and it just passes the event parameters in the method signature ( like importing, changing parameters )

By default, the event parameter WDEVENT will be there in signature and

if we have checked "transfer UI Event Parameters", then the parameters associated with current event are passed as parameters viz CONTEXT_ELEMENT, index etc

Now, we no need to use the below statements to get current context element

lo_element = WDEVENT->GET_DATA( ID = 'CONTEXT_ELEMENT" ).

lo_element->get_attribute(....)

instead

we can directly access event parameter CONTEXT_ELEMENT

context_element->get_attribute( .... )

Hope I made the point clear

Regards,

Rama

former_member195355
Participant
0 Kudos

Thanks for the explanation Ramakrishnappa.

You are such a helpful person!

If you're interested, here's my code and it seems to working okay, thanks to your example:

  DATA lo_nd_rfq_reply TYPE REF TO if_wd_context_node.

  DATA: lt_rfq_reply TYPE wd_this->elements_rfq_reply,

             ls_rfq_reply LIKE LINE OF lt_rfq_reply.

  DATA lt_elements TYPE wdr_context_element_set.

  DATA ls_elements LIKE LINE OF lt_elements.

  DATA: wa_data     TYPE zps_vendor_reply.

* navigate from <CONTEXT> to <RFQ_REPLY> via lead selection

  lo_nd_rfq_reply = wd_context->get_child_node( name = wd_this->wdctx_rfq_reply ).

**Get the elements

  lo_nd_rfq_reply->get_elements( RECEIVING set  = lt_elements ).

** @TODO compute values

  LOOP AT lt_elements INTO ls_elements.

    ls_elements->get_static_attributes( IMPORTING static_attributes = wa_data ).

      itab_data-total_rfq_total = itab_data-zmiv_total_rfq + itab_data-zcv_total_rfq.

*set the value of total

      ls_elements->set_attribute( EXPORTING value =  itab_data-total_rfq_total   " Attribute Value

          name  =  'TOTAL_RFQ_TOTAL' ).

  ENDLOOP.

However  think I'm approaching this all wrong.

I think I should do this:

On OnEnter event.

-Read the current table contents and pass to the assistance class

-Do all the total calculations in the assistance class

-Pass the table back and then SET the values to the context:

I think that would be a better way of doing this.

Would you agree or would you have a different view?

I'd really value the opinion of an expert!

ramakrishnappa
Active Contributor
0 Kudos

Hi Robert,

That is a very good idea   & now you are really getting into MVC architecture guidelines

but as we don't have any database related query in our component, we can still have this logic at component level.

It will be very clean and good if we move all the calculation logic, etc to assistance class

In your case, the steps you are about to do, are correct and go ahead.

Hope you are going to do as below


  • Create a method COMPUTE_TOTAL( ) method in assistance class with parameter CT_DATA OF TYPE ZTRI_VENDOR_REPLY ( this table type for structure zps_vendor_reply)

  • Move the calculation logic to the method compute_total

          loop at ct_data into ls_data.

              a+b+c

          endloop.

  • On enter action, the below steps do the trick
    • Read the node data lo_node->get_static_attributes_table( ...... ) into table lt_data
    • call assistance class method

               wd_assist->compute_total(

                         changing

                         ct_data = lt_data

    • set the data lo_node->bind_table(...... lt_data )

Regards,

Rama

former_member195355
Participant
0 Kudos

Goodness, that sounds like a really great approach!

I'll have a go at implementing it - thanks again, you're so knowledgable about WD!

Answers (3)

Answers (3)

former_member222068
Active Participant
0 Kudos

Hi Robert,

Implement the following code :

data : lo_el_selected type ref to if_wd_context_node,

          ls_selected     type wd_this->element_table.  ( Table node )

lo_el_selected = wdevent->get_context_element(  'CONTEXT_ELEMENT'  )..

lo_el_selected->get_static_attributes(   importing static_attributes = ls_selected ).


Note: ls_selected will return you the data of the button clicked row.

Thanks & Regards,

Sankar Gelivi

former_member184578
Active Contributor
0 Kudos

Hi,

Write the below code to get the index of row on which user press enter.


   DATA lr_el_element TYPE REF TO if_wd_context_element.

   DATA lv_index TYPE i.

   lr_el_element = wdevent->get_context_element( 'CONTEXT_ELEMENT' ).

    lv_index = lr_el_element->get_index( ).

lv_index contains the index of row on which user press Enter. Then you can sum up to that row.

Regards,

Kiran

ramakrishnappa
Active Contributor
0 Kudos

Hi Robert,

You will get the current line data in the parameter WDEVENT of your event handler method ADD_TOTAL.

Use wdevent-> get_data( ) method to get context_element.

Hope this helps you.

Regards,

Rama