on 03-06-2012 10:39 AM
Dear Experts,
I am trying to develop a simple calculator application in abap web dynpro .
but i am not able to enable the buttons (1 to 9) . that is what i want is like how it happens in a normal calculator if we press 1 , then in the screen, 1 comes and if 11 then twice we press 1. Like wise i want if the button 1 is enabled then in the input field it should take 1 and if 11 then it should take 11. Kindly give some suggestions to develop this application.
Regards
Swarnadeepta
Hi,
Step 1: Create an attribute of type Integer in the view controller.
Step 2: Add an UI element 'Input Field' and bind the value property with the attribute created in the earlier step.
Step 3: Create a method 'INPUT' in the view with an importing parameter 'IV_BUTTON_PRESSED'.
a) Read the context attribute value in local variable lv_input.
b) If it is initial set the new value as that of the import parameter.
c) Else, lv_input = lv_input * 10 + import parameter
d) Set the value of the local variable to the context node attribute.
-
DATA lo_el_context TYPE REF TO if_wd_context_element.
DATA ls_context TYPE wd_this->element_context.
DATA lv_input TYPE wd_this->element_context-input.
get element via lead selection
lo_el_context = wd_context->get_element( ).
get single attribute
lo_el_context->get_attribute(
EXPORTING
name = `INPUT`
IMPORTING
value = lv_input ).
IF lv_input IS INITIAL.
lv_input = iv_button_pressed.
ELSE.
lv_input = lv_input * 10 + iv_button_pressed.
ENDIF.
set single attribute
lo_el_context->set_attribute(
name = `INPUT`
value = lv_input ).
-
Step 4: Add the buttons and give method names under 'onAction' property.
Step 5: Call the method 'INPUT' and pass the value accordingly in the methods created in the earlier step.
-
for button 1
wd_this->entry(
iv_button_pressed = 1 " integer
).
-
Regards,
Sayan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi sayan,
Thank you so very much for the response.. and it's working absolutely fine.. all the buttons are enabled now.. it's taking 2 as Two and 22 as twenty two. thank you.. And one more request , can you please suggest me how to enable the operator buttons e.g. 55+25 = 80.
Kindly help.
Regards ,
Swarnadeepta
Hi experts ,
Can you please suggest me how can i enable the operator buttons i.e + , - , X , / .
Regards,
Swarnadeepta
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Swarnadeepta,
To enable the operation buttons you need to modify the existing logic.
Step 1: Create two more context node attributes 'OPERATION' and 'FLAG'
Step 2: Define a method for the action button.
Step 3: In the above method set the attributes so created e.g. For Button 'ADD' keep flag = 'X' and operation = 'ADD'.
Step 4: Define a global variable gv_previous under 'ATTRIBUTES' tab.
Step 5: In the method for numeric buttons add the following logic
a) Read the value of attributes 'INPUT' (containing the value of the input field), 'OPERATION' and 'FLAG'.
b) If FLAG = 'X' i.e. operation button was pressed then save the value in the input field in global attribute gv_previous and reset the value of the input field to 0.
c) else, follow the steps mentioned in the earlier post i.e.
wd_this->entry(
iv_button = 1 " integer
).
d) Finally reset the FLAG attribute.
Step 6: In the method defined for action button '=' add the following logic:
a) Read the value of the attributes 'INPUT' and 'OPERATION'
b) Add the following logic
case ls_operation-operation.
when 'ADD'.
lv_input = lv_input + wd_this->gv_previous.
when 'SUB'.
lv_input = wd_this->gv_previous - lv_input.
when 'MUL'.
lv_input = lv_input * wd_this->gv_previous.
when 'DIV'.
lv_input = wd_this->gv_previous / lv_input.
when others.
endcase.
c) set the value of the attribute 'INPUT' with the value of lv_input.
Hope the above solution helps.
Please give points if it was helpful.
Regards,
Sayan
Hi Sayan..
I tried.. but i don't know it's not working.. i debugged and checked.. what happening is .. while entering the first value(suppose 1 ) it's taking it as 1 after performing the (plus) action when i am trying to enter the second value ( say 2) , instead of taking it as 2 it's taking it as 12 (twelve).. even in the = action also addition is not getting perform.. hence as a result it is showing 12 only where as it should have shown 12 = 3 ..
i am not able to understand what happening..
Kindly suggest..
NB - below are the steps i followed..
Step 1) i created two more attributes (Operation and flag.) .
step 2) i defined one more action method for " equal to ".
step 3) Then in the numeric button i read all the attributes and checked if the flag value is set or not.
IF lv_flag = 'TRUE'.
wd_this->gv_prev = lv_num1.
lv_num1 = '0'.
ELSE.
wd_this->input_validate(
lv_button_pressed = 1 " int4
).
ENDIF.
step 4 ) this is the step where operator action would be performed..
suppose for " Addition "
i read both the operation and flag attribute and set their values to lv_operation = 'ADD' and lv_flag = 'TRUE'.
step 5 ) now the flag value is set.. inside the action method for button 2 it will check for the flag, whether it is set or not..
IF lv_flag = 'TRUE'.
wd_this->GV_PREV = lv_num1.
clear lv_num1.
ELSE.
wd_this->input_validate(
lv_button_pressed = 2 " int4
).
ENDIF.
this is the step where it is taking the wrong value.. i.e as the flag is set it should not have gone to the ELSE part.. but it 's going to the else and part and checking the input_validate method..
there it's taking lv_num1 = 1 " num1 is my input field
here also going to the else part...
IF lv_num1 IS INITIAL.
lv_num1 = lv_button_pressed.
ELSE.
lv_num1 = lv_num1 * 10 + lv_button_pressed.
ENDIF.
hence it 's taking it as 12 (twelve) instead of 2 (two)
Regards ,
Swarnadeepta
Hi Swarnadeepta,
In Step 3 replace the following code
IF lv_flag = 'TRUE'.
wd_this->gv_prev = lv_num1.
lv_num1 = '0'.
ELSE.
wd_this->input_validate(
lv_button_pressed = 1 " int4
).
ENDIF.
by
IF lv_flag = 'TRUE'.
wd_this->gv_prev = lv_num1.
lv_num1 = '0'.
***set the input field with the new value
***you need to initiaize the input field once you press any operation button
ENDIF.
wd_this->input_validate(
lv_button_pressed = 1 " int4
).
***clear the flag attribute
***otherwise if you press 12 after operation button it will not be taken into consideration
I think it should work...let me know if you need any more help:)
Regards,
Sayan
Hi Sayan,
It's stil not working..
As per your suggestion , i changed my code at the Step - 3..
IF lv_flag = 'TRUE'.
wd_this->gv_prev = lv_num1.
clear lv_num1.
ENDIF.
wd_this->input_validate(
lv_button_pressed = 1 " int4
).
clear lv_flag.
i am checking for suppose say 11 + 21 = 32 ..
When i debugged the application, below things i found..
step 1 ) when i pressed the button 1, it's going inside the OnActionButton1 method,
there it's checking for lv_flag .. As at the first step it is in the deactivated mode..
Hence it will directly go to
wd_this->input_validate(
lv_button_pressed = 1 " int4
).
clear lv_flag.
Now inside the input_validate()..
1) As the lv_num1 is initial, hence it will take lv_num1 = lv_button_pressed.
Now again button 1 was pressed , so that in the lv_num1 became 11 (eleven).
Step 2 ) When i pressed the button + , it's going to OnActionButton_Plus method,
here it's taking lv_operation = 'ADD' and lv_flag = 'T'.
Step 3) When i pressed the button 2 , it's going inside the method OnActionButton2.,
while reading the num1 attribute into the variable lv_num1, instead of 0 it is taking 11 into lv_num1.
it is taking 2 as 112 and 3 when pressed, it's taking it as 1123
Hence the answer is becoming 1134..
Thats why only it's taking throwing the wrong answer..
I don't understand where i should check for the lclear lv_num1 so that it would take it's correct value while inserting the number for the second time..
Regards,
Swarnadeepta.
Hi Swarnadeepta,
I developed a calculator in web dynpro...please go through the following code. I have made a few changes with respect to modularization but the basic concept is still the same.
-
Method to Enter Data on the screen
-
METHOD enter_data .
DATA lv_input TYPE i.
DATA lv_flag TYPE c.
***Read input in the screen
wd_this->get_input(
IMPORTING
ev_input = lv_input " integer
).
***See whether flag is set. If yes save the present value in global attribute gv_previous.
wd_this->get_flag(
IMPORTING
ev_flag = lv_flag " wdy_boolean
).
IF lv_flag = 'X'.
wd_this->gv_previous = lv_input.
lv_input = 0.
ENDIF.
***Modify screen input
IF lv_input IS INITIAL.
lv_input = iv_number.
ELSE.
lv_input = lv_input * 10 + iv_number.
ENDIF.
***Set the new value of input field
wd_this->set_input(
iv_input = lv_input " integer
).
***Reset the flag
wd_this->set_flag(
iv_flag = '' " wdy_boolean
).
ENDMETHOD.
-
Use the above method on button click
-
method ONACTIONONE .
wd_this->enter_data(
iv_number = 1 " integer
).
endmethod.
-
Method to Register Operations
-
method ENTER_OPERATION .
wd_this->set_flag(
iv_flag = 'X' " wdy_boolean
).
wd_this->set_operation(
iv_operation = iv_operation " string
).
endmethod.
-
Use of above method in operations button
-
method ONACTIONADD .
wd_this->enter_operation(
iv_operation = 'ADD' " string
).
endmethod.
-
Method to calculate
-
method ONACTIONEQL .
DATA lv_operation TYPE string.
DATA lv_input TYPE i.
***Read screen input
wd_this->get_input(
IMPORTING
ev_input = lv_input " integer
).
***read operation requested
wd_this->get_operation(
IMPORTING
ev_operation = lv_operation " string
).
CASE lv_operation.
WHEN 'ADD'.
lv_input = wd_this->gv_previous + lv_input.
WHEN 'SUB'.
lv_input = wd_this->gv_previous - lv_input.
WHEN 'MUL'.
lv_input = wd_this->gv_previous * lv_input.
WHEN 'DIV'.
lv_input = wd_this->gv_previous / lv_input.
WHEN OTHERS.
ENDCASE.
***Set the new value of input field
wd_this->set_input(
iv_input = lv_input " integer
).
***Clear the operation attribute
wd_this->set_operation(
iv_operation = '' " string
).
endmethod.
-
Getter Methods example for attribute INPUT
-
method GET_INPUT .
DATA lo_el_context TYPE REF TO if_wd_context_element.
DATA ls_context TYPE wd_this->element_context.
DATA lv_input TYPE wd_this->element_context-input.
get element via lead selection
lo_el_context = wd_context->get_element( ).
@TODO handle not set lead selection
IF lo_el_context IS INITIAL.
ENDIF.
get single attribute
lo_el_context->get_attribute(
EXPORTING
name = `INPUT`
IMPORTING
value = lv_input ).
EV_INPUT = lv_input.
endmethod.
-
Setter Methods example for attribute INPUT
-
method SET_INPUT .
DATA lo_el_context TYPE REF TO if_wd_context_element.
DATA ls_context TYPE wd_this->element_context.
DATA lv_input TYPE wd_this->element_context-input.
get element via lead selection
lo_el_context = wd_context->get_element( ).
@TODO handle not set lead selection
IF lo_el_context IS INITIAL.
ENDIF.
@TODO fill attribute
lv_input = iv_input.
set single attribute
lo_el_context->set_attribute(
name = `INPUT`
value = lv_input ).
endmethod.
-
Hope this will be helpful. Let me know if you have any doubt.
Its working fine for me.
Regards,
Sayan
User | Count |
---|---|
95 | |
11 | |
11 | |
10 | |
9 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.