cancel
Showing results for 
Search instead for 
Did you mean: 

how to create a table dynamically in webdynpro

Former Member
0 Kudos

hai everybody

in webdynpro we know how to create at design time

but i want to create it at runtime that is dynmamically i want to set the rows and coloums of the table at run time

Thanks & Regards

sravan

Accepted Solutions (0)

Answers (5)

Answers (5)

Former Member
0 Kudos

I guess this should work,

Suppose you want to dynamically create a Table (say Category) having a column called Category Code, then in the wdDoModifyView() hook method, just add the following code snippet,

if (firstTime) {

//Create the Table UI Element

IWDTable table = (IWDTable) view.createElement(IWDTable.class, null);

//Set the Table Header

IWDCaption tableCaption = (IWDCaption) view.createElement(IWDCaption.class, null);

tableCaption.setText("Category Table Contents");

table.setHeader(tableCaption);

//Create and add a Column UI element for the above table

IWDTableColumn column = (IWDTableColumn) view.createElement(IWDTableColumn.class, null);

table.addColumn(column);

//Set the required column header using the caption (Category Code here)

IWDCaption caption = (IWDCaption) view.createElement(IWDCaption.class, null);

caption.setText("Category Code");

column.setHeader(caption);

//Create a TableCellEditor and bind it to an attribute of the Category Node in the context

IWDTextView editor = (IWDTextView) view.createElement(IWDTextView.class, null);

editor.bindText("tb_Category.cat_code");

column.setTableCellEditor(editor);

//Finally add the Table UI element to the view

IWDUIElementContainer root = (IWDUIElementContainer) view.getRootElement();

root.addChild(table);

}

You need to follow the same procedure for adding multiple columns to the table.

Hope this answers your question!

Regards

Kishan

Former Member
0 Kudos

Hi,

You can use the same code provided by Bharat .

IWDTableColumn colum = (IWDTableColumn)

view.createElement(

IWDTableColumn.class, null);

table.addColumn(column);

// use TextView for displaying

// attribute "Data.City"

IWDTextView editor = (IWDTextView)

view.createElement(IWDTextView.class, null);

editor.bindText("Data.City");

column.setTableCellEditor(editor);

Regards, Anilkumar

Former Member
0 Kudos

Hi,

In the code i have mentioned i have binded it with

Data

--No

--Name

Likewise bind it with some node and bind each table column with a attribute in that node.

At run time just populate the node and it will get populated in the table directly.

Regards

Bharathwaj

Former Member
0 Kudos

hi Bharathwaj thanx for ur code

i did as u said but i got output only dummy table with two columns no data in the table . for getting the data dynamically into the table should i write any java class and one more thing bharath in the view there is only one default Root Container. should i insert any view elements in the view

Thanks in Advance

sravan.

Former Member
0 Kudos

Hi ,

Do you have the context structure as mentioned.

And are you puttin data into that node. If yes check the node size.

If no ..use the code given by Santosh to populate the node.. (change it appropriately to suit ur node names)

//Create table node element

IPrivate<View name>.IDataElement elem = wdContext.nodeData().createDataElement();

// Set data to the column attributes

elem.setNo("1");

elem.setName("Armin");

//Add the node element to the result/table node

wdContext.nodeData().addElement(1, elem);

This is for a node with structure

Data

-- No

-- Name

And this is a sample for adding one set of data.

Regards

Bharathwaj

My Code added ! I am not sure if this is what u have mentioned Armin !

Former Member
0 Kudos

Can you post your code?

Armin

Former Member
0 Kudos

//Add the node element to the result/table node

<i>wdContext.nodeData().addElement(1, elem);</i>

Indexing starts with 0. Simply omit the first argument to add at the end of the node element list.

Armin

Former Member
0 Kudos

I would suggest a small change in the code given by Bharathwaj

Instead of

IWDTextView editor = (IWDTextView)

view.createElement(IWDTextView.class, null);

write the following code

IWDTableCellEditor editor = (IWDTableCellEditor)

view.createElement(IWDTextView.class, null);

If you need some other element as the cell editor you can just change the class name given in the createElement method call.

Former Member
0 Kudos

Noufal,

this is not good advice because you cannot do anything with type IWDTableCellEditor. In most cases there might follow a bind-statement after the create which is not possible with IWDTableCellEditor, only with the exact type.

Armin

Former Member
0 Kudos

how to insert data dynmically in the table and show in the table

Former Member
0 Kudos

Hi,

Let me assume the node which is bound to the table UI element is "vnResultTable" and the columns are bound to the "vaCol1", "vaCol2" attributes.

Refer the following code

// no of iterations

int nOutputCount = 5;

for(int j=0;j<nOutputCount;j++){

//Create table node element

IPrivate<<View name>>.IVnResultTableElement objResultTableElement = wdContext.nodeVnResultTable().createVnResultTableElement();

// Set data to the column attributes

objResultTableElement.setVaCol1("Column 1 data");

objResultTableElement.setVaCol1("Column 2 data");

//Add the node element to the result/table node

wdContext.nodeVnResultTable().addElement(j, objResultTableElement);

}

Hope this is helpful. If you need any further help, do let me know.

Regards,

Santhosh.C

Former Member
0 Kudos

Please be more specific.

Armin

Former Member
0 Kudos

hai santhosh thanks for ur timely help

the table data is showing only five rows at a time is it default

i want to change the no. of rows accoding to the requirement to be displayed at run time

Former Member
0 Kudos

hi,

Go the tables property and set visiblerowcount to the number you want

To do it dynamically

Put this code in the modifyview

final IWDTable tbl= (IWDTable)view.getElement("tblname");

tbl.setVisibleRowCount(<get it from context which stores the value required)

regards

Bharathwaj

Message was edited by: Bharathwaj R

Former Member
0 Kudos

I would suggest either to set the visible row count at design time or to bind it to a context attribute via bindVisibleRowCount().

wdDoModifyView() should only be used to modify the view layout and no to set property values which can be manipulated via context binding.

Armin

Former Member
0 Kudos

hai i need to insert no. of columns at run time

i.e., in the context i create a value node which is a datasource to the table and three value attributes with No. , Name , and Age. i want to insert more columns

should i need to create the value attributes in the context manually or there is any procedure through which we can create value attributes in the context at runtime dynmically

Former Member
0 Kudos

If possible, create the context attributes at design time, it's easier. Adding table columns can then be done in wdDoModifyView() with code like shown before.

Generally, you can do (almost) everything which can be done at design time also via the API.

Can you describe your use case more exactly?

Armin

Former Member
0 Kudos

hi armin

actually cant we add value attributes and to the value node at runtime i.e. programatically so that we can have the no. of columns of our choice at runtime

Former Member
0 Kudos

Hi,

To add a attribute to a node use

IWDNodeInfo node = wdContext.node<name>.getNodeInfo();

node.addAttribute("Attribute",

"ddic:com.sap.dictionary.string or ur type required");

To bind it

<textviewobject).bindValue("Attribute");

To access the getter and setter methods

wdContext.current<node name>Element().

setAttributeValue("Attribute", null);

wdContext.current<node name>Element().

getAttributeValue("Attribute", null);

But i would suggest creating all the attributes in the beginning and do the binding alone at run-time.

Regards

Bharathwaj

Former Member
0 Kudos

hai bharath could you be more precise please

right now i created a table dynamically in this i created one ValueNode <b>Data</b> and 3 value attributes to that node which are the 3 columns in my table.

What i need is i want to create a column at runtime

let us say attribute is <b>city</b>

so where should i write the code

it would be nice if u provide me the code

Thanks & Regards

sravan

Former Member
0 Kudos

Hi,

IWDNodeInfo nodeinfo = wdContext.nodeData.getNodeInfo();

nodeinfo.addAttribute("City",

"ddic:com.sap.dictionary.string");

will create a attribute of type string under the node Data.

You can write this code anywhere.You must be having some action say onAdd after which this new attribute has to be created .Then write it in that part.(onActiononAdd)

Is that sufficient ?

regards

Bharathwaj

Former Member
0 Kudos

hai bharath i'm getting error while using the following code

IWDNodeInfo nodeinfo = wdContext.nodeData.getNodeInfo();

nodeinfo.addAttribute"City","ddic:com.sap.dictionary.string");

wdContext.nodeData cannot be resolved or is not a field

Former Member
0 Kudos

Hi,

Try the following code

IWDNodeInfo nodeinfo = wdContext.nodeData().getNodeInfo();

nodeinfo.addAttribute("City","ddic:com.sap.dictionary.string");

Bharathwaj missed the "()" for "nodeData", also missed a opening brace for "addAttribute".

Regards,

Santhosh.C

Former Member
0 Kudos

It's a method for accessing the node with name "Data", so it should be wdContext.nodeData().

Armin

Former Member
0 Kudos

Hi,

Sorry ! Thats the error.. Or press ctrl+space after wdContext.node.. you will get the proper node name

Rgds

Bharathwaj

Former Member
0 Kudos

bharath i'm trying to add a value attribute City to a ValueNode Data and inserting data into that column using follwoing but im unable it is giving erros at bind value and at getAttribute. please help me

IWDNodeInfo nodeinfo = wdContext.nodeData().getNodeInfo();

nodeinfo.addAttribute("City","ddic:com.sap.dictionary.string");

IWDTextView editor = (IWDTextView)view.createElement(IWDTextView.class, null);

editor.bindValue("City");

//To access the getter and setter methods

wdContext.currentDataElement().setAttributeValue("City", null);

wdContext.currentDataElement().getAttributeValue("City", null);

Former Member
0 Kudos

Hi,

What are the errors.. Do you get the error when u deploy the application or while typing itself.

I think the case u are using is different.Are u using "City" or "city". I have given the code for "City".If u use "city" it will give errors.

And in binding it with a column since it is under a node Data, u have to give "Data.city" or "Data.City" (i.e whatever nodename.attrname)

Rgds

Bharathwaj

Message was edited by: Bharathwaj R

Former Member
0 Kudos

Hi,

The binding should be with the value node name.


IWDTextView editor = (IWDTextView)view.createElement(IWDTextView.class, null);
editor.bindValue("Data.City");

//Create the node element object
 IPrivate<<View name>>.IDataElement objResultTableElement = wdContext.nodeData().createDataElement();

//set data to the attributes
objResultTableElement.setAttributeValue("City", null);

//add the node element to the node
wdContext.nodeData().addElement(objResultTableElement);

Hope this is want you are looking for.

Regards,

Santhosh.C

Former Member
0 Kudos

try out this link

/people/sap.user72/blog/2005/05/23/accessing-dynamic-nodes-in-webdynpro

Former Member
0 Kudos

hai everybody please checy out the following the code

by doing so i can create rows in a table dynamically at ryntime of my choice. But i want to insert columns of my choice at runtime using some forloop.

IWDTable table=null;

if (firstTime)

{

// create table and then bind table data source to context node "Data"

table=(IWDTable)view.createElementIWDTable.class,null);

table.setVisibleRowCount(6);

table.bindDataSource("Data");

}

int rowCount = 12;

for(int j=0;j<rowCount;j++){

//Create table node element

IPrivateDynNodeAttributeView.IDataElement objdataElement = wdContext.nodeData().createDataElement();

wdContext.nodeData().addElement(j, objdataElement);

}

IWDUIElementContainer root =(IWDUIElementContainer) view.getRootElement();

root.addChild(table);

Former Member
0 Kudos

Hi,

You must be having some condition after which you should add the column.If so check that condition and add if it is satisfied.

If their is no condition, there might not be a need to go for dynamic attributes.Anyway in this case u can put it as i have shown.

if (firstTime)

{

// create table and then bind table data source to context node "Data"

table=(IWDTable)view.createElementIWDTable.class,null);

table.setVisibleRowCount(6);

IWDNodeInfo nodeinfo = wdContext.nodeData().getNodeInfo();

nodeinfo.addAttribute("City","ddic:com.sap.dictionary.string");

table.bindDataSource("Data");

}

int rowCount = 12;

for(int j=0;j<rowCount;j++){

//Create table node element

IPrivateDynNodeAttributeView.IDataElement objdataElement = wdContext.nodeData().createDataElement();

wdContext.nodeData().addElement(j, objdataElement);

}

IWDUIElementContainer root =(IWDUIElementContainer) view.getRootElement();

root.addChild(table);

Regards

Bharathwaj

Former Member
0 Kudos

hai bharath its not working i just want to insert some dummy columns of my choice dynamically at runtime. do u hava any code to insert dummy columns in a table at runtime.

Former Member
0 Kudos

Why not reading this thread? In the very first answer you find coding that dynamically adds table columns.

Armin

Former Member
0 Kudos

Hi,

Ya u can find it mentioned in the thread itself.

Anyway i have executed this code,this is working ..Put this in the place where i have mentioned(after setting visible row count.)

IWDNodeInfo nodeinfo = wdContext.nodeData().getNodeInfo();

nodeinfo.addAttribute("City","ddic:com.sap.dictionary.string");

IWDTextView editor = (IWDTextView)view.createElement(IWDTextView.class, null);

editor.bindText("Data.City");

IWDTableColumn c = (IWDTableColumn)view.createElement(IWDTableColumn.class,null);

c.setTableCellEditor(editor);

table.addColumn(c);

table.bindDataSource("Data");

Regards

Bharathwaj

Former Member
0 Kudos

Hi,

Try this ..

The only requirement is u need a Data node with attributes No and Name in it !

if (firstTime)

{

// create table

IWDTable table = (IWDTable)

view.createElement(IWDTable.class, null);

// bind table data source to context node "Data"

table.bindDataSource("Data");

// create and add first table column

{

IWDTableColumn colum = (IWDTableColumn)

view.createElement(

IWDTableColumn.class, null);

table.addColumn(column);

// use TextView for displaying

// attribute "Data.No"

IWDTextView editor = (IWDTextView)

view.createElement(IWDTextView.class, null);

editor.bindText("Data.No");

column.setTableCellEditor(editor);

}

// create and add second table column

{

IWDTableColumn colum = (IWDTableColumn)

view.createElement(

IWDTableColumn.class, null);

table.addColumn(column);

// use TextView for displaying

// attribute "Data.Name"

IWDTextView editor = (IWDTextView)

view.createElement(IWDTextView.class, null);

editor.bindText("Data.Name");

column.setTableCellEditor(editor);

}

// add table into view layout as child of

// root container

IWDUIElementContainer root =

(IWDUIElementContainer) view.getRootElement();

root.addChild(table);

}

(With help from Armin !)

Regards

bharathwaj

Message was edited by: Bharathwaj R

Former Member
0 Kudos

/people/sap.user72/blog/2005/05/09/have-you-played-blindfold-chess

this blog might be helpful

Former Member
0 Kudos

Thanks for citing my earlier posting.

I want to add, that it is also possible to create the table element at design time and add any number of columns programmatically. It depends on the use case.

The only requirement is a view context node with cardinality 0:N or 1:N.

Armin

Former Member
0 Kudos

Thanx Noufal Kareem

thanks for ur weblog

nitin_mahajan2
Contributor
0 Kudos

Thats a fentastic code, thanks,

This way we have to create one column for each attribute.Is it possible to iterate through the datasource node and get the columns created for all the elements by writing code for just one element.

we can internally use if else to create different type of columns for different elements.

PS: i am very new to this world

Regards

Nitin