PDA

View Full Version : Create dataset onclick


paula_mz
03-22-2004, 03:40 AM
hello,

I need to create an xml on the client, every time I clicked the button, as follows:
<myrootnode>
<sub1>
<a_sub2>1</a_sub2>
<b_sub2>2</b_sub2>
<c_sub2>3</c_sub2>
</sub1>
<sub1>
<a_sub2>1</a_sub2>
<b_sub2>2</b_sub2>
<c_sub2>3</c_sub2>
</sub1>
...
<sub1>
<a_sub2>1</a_sub2>
<b_sub2>2</b_sub2>
<c_sub2>3</c_sub2>
</sub1>
</myrootnode>

I used your code for example and I add some code, but is a mistake here...
What I do wrong?

Here is the code:
-----------------------------------
<canvas debug="true">
<dataset name="myds">
<myrootnode/>
</dataset>
<simplelayout axis="y" spacing="10" />
<button>Add new node
<method event="onclick">
var dp=canvas.datasets.myds.getPointer();
dp.selectChild();
var newNodeName = 'sub1';
dp.addNode(newNodeName);
dp.setXPath(newNodeName);
dp.addNode( "a_sub2", "1", {} )
dp.addNode( "b_sub2", "2", {} )
dp.addNode( "c_sub2", "3", {} )

canvas.serializeDP( dp );
</method>
</button>
<button>Serialize entire dataset
<method event="onclick">
canvas.serializeDP( canvas.datasets.myds.getPointer() )
</method>
</button>
<method name="serializeDP" args="dp">
Debug.write( dp.serialize() );
</method>
</canvas>

thank you
paula

antun
03-22-2004, 01:09 PM
Here's what's going on:


1. Obtain a pointer to the root of the dataset:
var dp=canvas.datasets.myds.getPointer();

2. Select the first child, i.e. <myrootnode>:
dp.selectChild();

3. Create a new node in <myrootnode> called "sub1":
var newNodeName = 'sub1';
dp.addNode(newNodeName);

4. Now move the pointer into the node named "sub1":
dp.setXPath(newNodeName);

5. Now that the pointer is pointing to "sub1", create subnodes inside of it:
dp.addNode( "a_sub2", "1", {} )
dp.addNode( "b_sub2", "2", {} )
dp.addNode( "c_sub2", "3", {} )


The first time that you click the button, there is no <sub1> node, and it gets created in step 3. So in step 4, the datapointer can be moved into the <sub1> node, because there is only one <sub1> node at that time.

However the second time you click the button, step 4 fails, because there are two <sub1> nodes there.

What you have to do is tell it which <sub1> node to move into. Obviously you want the last one created, so you want:


dp.setXPath(newNodeName + "[n]");


... where n is the 1-based number of the last node created. You could use dp.getNodeCount() to find how many nodes there are and use that:

http://www.laszlosystems.com/lps-2.1/docs/lzx-reference/?datapointer.html#meth-getNodeCount

-Antun