cancel
Showing results for 
Search instead for 
Did you mean: 

Many times dynamic generate UIElement at Runtime ???

Former Member
0 Kudos

Hello Everyone,

I wanna generate a UIElement (InputField) at runtime. When man click a button "Add InputField", then a UIElement (InputField) is dynamic generated. Man can generate many times UIElement"InputField" with this button.

I have tried the Webdynpro Tutorial 17. But not yet any idea.

Plx give me some suggestions and code.

Thanks in advance

best regards

Yaning

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

hi

for this create 2 int variables <b>act</b> and a <b>counter</b>

//@@begin others

static int act,counter=0;

//@@end

<b>in Doinit set act to 0.</b>

public void wdDoInit()

{

//@@begin wdDoInit()

act=0;

//@@end

}

<b>create an action(onActionCreate_Element) and assign it to the button ui element</b>

public void onActionCreate_Element(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )

{

//@@begin onActionCreate_Element(ServerEvent)

act = 1;

//@@end

}

<b>in wdDoModifyView</b>

public static void wdDoModifyView(IPrivateDynamicView wdThis, IPrivateDynamicView.IContextNode wdContext, com.sap.tc.webdynpro.progmodel.api.IWDView view, boolean firstTime)

{

//@@begin wdDoModifyView

if( act== 1)

{

IWDTransparentContainer con= (IWDTransparentContainer)view.getElement("RootUIElementContainer");

IWDInputField input = (IWDInputField)view.createElement(IWDInputField.class,"input1"+counter);

IWDAttributeInfo test = wdContext.getNodeInfo().addAttribute("name"+counter,"ddic:com.sap.dictionary.string");

input.bindValue(test);

input.setVisible(WDVisibility.VISIBLE);

counter++;

con.addChild(input);

act = 0;

}

//@@end

}

Former Member
0 Kudos

Hello Puneet,

Thx for your answer. This problem already clear.

I have yet a question.

For example, i have created many InputFields, but the last one is superfluous .

Therefore i wanna delete a InputField dynamically ,which i have created.

click a button"Delete" , dynamically delete.

Thx again.

Regards

Yaning

siarhei_pisarenka3
Active Contributor
0 Kudos

Hi Yaning

In order to delete some UI control you can use the following code:

1. Create new attribute 'view' of type IWDView in the context of your view controller.

2. In method wdDoModifyView() store 'view' input parameter in the context:

wdContext.currentContextElement().setView(view);

3. In action handler of your Delete button write something like this:

IWDView view = wdContext.currentContextElement().getView();

IWDInputField if = (IWDInputField) view.getElement(<ID of Input Field to delete>);

IWDUIElementContainer container = if.getContainer();

container.removeChild(container.indexOfChild(if));

Or just set Visibility of the input field to NONE

BR

Sergei

Former Member
0 Kudos

Hi Sergei ,

Thank U very much!!

I am a new for WebDynpro. Thx for your help again!

this is total perfect idea.

Regards

Yaning

Former Member
0 Kudos

Siarhei,

The advise with VISIBILITY is ok, however altering view outside wdDoModifyView (or methods invoked from wdDoModifyView) is a bad idea.

Currently this works, but WD documentation stays explicitly that there are no guarantees for code correctness if view modification is performed in action handler rather in wdDoModifyView. So it's better to do just the reverse:

1. Create context attribute ControlIdToDelete of type string

2. In action handler assign ID of necessary UI control to this context attribute

3. In wdDoModifyView check that ControlIdToDelete is not null, then delete control and reset ControlIdToDelete to null.

Valery Silaev

SaM Solutions

http://www.sam-solutions.net

siarhei_pisarenka3
Active Contributor
0 Kudos

Hi Valery

Thanks for your correction. When this will be done it will make things more complicated then now.

BR

Sergei

PS: Could you get a source of the info? Thanks.

Former Member
0 Kudos

Siarhei,

Unfortunately, I can't find reference in WD Java documentation any longer, however there is still note in WD ABAP docs (I firmly believe it is applicable to both):

<i>To make changes to the structure of a view layout, you must use the method WDDOMODIFYVIEW (or a method called within it).</i>

From http://help.sap.com/saphelp_erp2005/helpdata/en/11/ba74412cba127de10000000a155106/content.htm

As about complexity, it's just the same: one extra attribute of type IWDView that is handled in action handler vs one extra attribute that is processed in wdDoModifyView.

Valery Silaev

SaM Solutions

http://www.sam-solutions.net

Answers (3)

Answers (3)

Former Member
0 Kudos

hello,

another question,

when man click the button "delete field" , the Context attribute must be removed.

how do it?

thx

regards

Yaning

siarhei_pisarenka3
Active Contributor
0 Kudos

It's a problem. API does not allow to do this explicitly. There is only wdContext.getContext().reset() method, but it will remove all such attributes created dynamically.

You can just leave the attribute even if you remove the input field associated. However this solution requires additional check during new attributes creation because it's impossible to create two attributes with the same name:

IWDAttributeInfo test = wdContext.getNodeInfo().getAttribute("name"+counter);

if (test == null) {

test = wdContext.getNodeInfo().addAttribute("name"+counter, "ddic:com.sap.dictionary.string");

}

Former Member
0 Kudos

hi Sergei,

thx again.

now comes another problem. After removed the InputField, I wanna add a InputFiled again.

then error appears.

problem should be, the ID "InputField3" was not removed.

can man remove ID meanwhile, when man delete the UIElement.

******************

com.sap.tc.webdynpro.services.exceptions.WDRuntimeException: View: Cannot add element with duplicate ID "InputField3" of type com.sap.tc.webdynpro.clientserver.uielib.standard.impl.InputField

at com.sap.tc.webdynpro.progmodel.view.View.addElement(View.java:482)

at com.sap.tc.webdynpro.progmodel.view.ViewElement.<init>(ViewElement.java:43)

at com.sap.tc.webdynpro.progmodel.view.UIElement.<init>(UIElement.java:188)

at com.sap.tc.webdynpro.clientserver.uielib.standard.impl.AbstractInputField.<init>(AbstractInputField.java:143)

at com.sap.tc.webdynpro.clientserver.uielib.standard.impl.InputField.<init>(InputField.java:71)

***********************

Thx

Regards

Yaning

siarhei_pisarenka3
Active Contributor
0 Kudos

Try then .destroy() as Fahad suggesed

IWDInputField input=(IWDInputField)view.getElement(input field ID);

input.destroy();

Former Member
0 Kudos

hi Sergei,

in InputField API "IWDInputField" there is not .destroy function.

regards

yaning

siarhei_pisarenka3
Active Contributor
0 Kudos

Each IWDViewElement including IWDInputField has the method. Do you use Netweaver 2004S?

Former Member
0 Kudos

hi,

my NWDS version SAP

NetWeaver Developer Studio

Version: 2.0.17

i think ,it is the same as you say.

+++++

IWDInputField theInput = (IWDInputField) view.getElement("InputField" + counter);

theInput.destroy();----


"compile error"

+++++

regards

yaning

Former Member
0 Kudos

Hi Yaning,

U can destroy the input field as

IWDInputField input=(IWDInputField)view1.getElement(input field ID);

input.destroy();

Regards

Fahad hamsa

Former Member
0 Kudos

Hi Yaning,

Let ur root container id be RootUIElementContainer

Do the following steps

1. Create a static variable say ' myvar ' of type com.sap.tc.webdynpro.progmodel.api.IWDView in last declaration part(ie. inside //@@begin others //@@end.

2. Create a static variable of type int in last declaration part as static int count=0;

2. In wdDoModifyView(), add the following

myVar=view;

3. Inside ur action to create input field, add the following

IWDTransparentContainer root=(IWDTransparentContainer)view1.getElement("RootUIElementContainer");

IWDAttributeInfo attrInfo=wdContext.getNodeInfo().addAttribute("Name"+count,"ddic:com.sap.dictionary.string");

IWDInputField input=(IWDInputField)view1.createElement(IWDInputField.class,"input"+count);

input.bindValue(attrInfo);

input.setVisible(WDVisibility.VISIBLE);

root.addChild(input);

count++;

Regards,

Fahad Hamsa

siarhei_pisarenka3
Active Contributor
0 Kudos

>

> Do the following steps

>

> 1. Create a static variable say ' myvar ' of type

> com.sap.tc.webdynpro.progmodel.api.IWDView in last

> declaration part(ie. inside //@@begin others

> //@@end.

>

> 2. Create a static variable of type int in last

> declaration part as static int count=0;

>

It's not a good idea to create static variables like you proposed. Such application will not work in multiuser (multithreaded) environment.

It's better to store all necessary variables in the context of the view controller which is available in static method 'wdDoModifyView()'.

BR

Sergei