Using Datapointer and Dataelement

A Dataset object internally employs a pointer to reference the current node of the XML data. This data pointer is represented by the LzDataPointer class, a direct child of LzNode. Table 8.4 shows the attributes defined in the Datapointer class.

Table 8.4. The attributes in the LzDataPointer class
NameUsageTypeDefaultAccessibility
pTag and JSstring read-write
 Description. The LzDataNode object the pointer is referencing.
rerunxpathTag and JSboolean read-write
 Description. Indicates whether or not to reevaluate the XPath expression after the dataset is edited.
spacingTag and JSnumber0read-write
 Description. The spacing between elements in pixels.

The LzDataPointer class defines the following methods:

addNode(name, text, attributes)

Adds a new child node below the current context.

addNodeFromPointer(pointer)

Copies the node pointed by the specified pointer to the current node.

comparePointer(pointer)

Indicates whether or not the specified pointer references the same node as this pointer.

deleteNode()

Deletes the node pointed by the pointer.

deleteNodeAttribute(attribute)

Deletes the specified attribute from the current node.

dupePointer()

Clones this pointer and returns the new pointer.

getDataset()

Returns the data set associated with this pointer.

getNodeAttribute(attribute)

Returns the value of the specified attribute in the current node.

getNodeAttributes()

Returns an object containing all the attributes in the current node.

getNodeCount()

Returns the number of element nodes that are children of the current node.

getNodeName()

Returns the name of the current node.

getNodeText()

Returns the text of the current node.

isValid()

Tests if this pointer is referencing a valid node.

selectChild(howMany)

Moves down the data hierarchy by howMany.

selectNext(howMany)

Moves to the next sibling.

selectParent(howMany)

Moves up the data hierarchy by howMany.

selectPrev(howMany)

Moves to the previous sibling.

serialize()

Serializes the current node and its children as a string

setFromPointer(pointer)

Sets this data pointer to the location referenced by the specified pointer.

setNodeAttribute(name, value)

Sets the specified attribute in the current node.

setNodeName(name)

Changes the name of the current node.

setNodeText(text)

Changes the text of the current node.

setPointer(node)

Points this pointer to the specified node.

setXPath(path)

Sets the XPath of this pointer.

xpathQuery(path)

Returns the result of querying the path without changing the pointer.

As an example, the code in Listing 8.8 shows how to use data pointers.

Listing 8.8. Using data pointers
<canvas height="300">
    <simplelayout axis="y"/>
    <dataset name="myData">
        <phoneBook>
        <contact>
            <firstName>Linda</firstName>
            <lastName>Carter</lastName>
            <phone>999-789-8998</phone>
        </contact>
        <contact>
            <firstName>Amy</firstName>
            <lastName>Grant</lastName>
            <phone>992-123-1231</phone>
        </contact>
    </phoneBook>
</dataset>

<datapointer id="dataPointer" xpath="myData:/phoneBook"/>

<grid datapath="myData:/phoneBook" contentdatapath="contact">
    <gridtext width="100" editable="false"
   datapath="firstName/text()">
        First Name
    </gridtext>
    <gridtext width="100" datapath="lastName/text()">
        Last Name
    </gridtext>
    <gridtext width="100" datapath="phone/text()">
        Last Name
    </gridtext>
    <gridcolumn width="70">
        <view datapath="position()">
            <button text="delete">
                <method event="onclick">
                    parent.datapath.deleteNode();
                </method>
            </button>
        </view>
    </gridcolumn>
</grid>
<button>Add New Contact
    <method event="onclick">
        parent.addContactWindow.setVisible(true);
        LzFocus.setFocus(firstName);
    </method>
</button>
<window name="addContactWindow" title="Add Contact"
        width="180" visible="false">
    <simplelayout axis="y"/>
    <view>
            <simplelayout/>
            <text>First Name:</text>
            <edittext id="firstName" x="30"/>
        </view>
        <view>
            <simplelayout/>
            <text>Last Name:</text>
            <edittext id="lastName" x="30"/>
        </view>
        <view>
            <simplelayout/>
            <text>Phone:</text>
            <edittext id="phone" x="30"/>
        </view>
        <view>
            <button text="Add" x="85">
                <method event="onclick">
                    var newNode = new LzDataElement("contact");
                    dataPointer.p.insertBefore(newNode,
                            dataPointer.p.getFirstChild())
                    var childNode1 = new LzDataElement("firstName");
                    childNode1.appendChild(
                            new LzDataText(firstName.getText()));
                    var childNode2 = new LzDataElement("lastName");
                    childNode2.appendChild(
                            new LzDataText(lastName.getText()));
                    var childNode3 = new LzDataElement("phone");
                    childNode3.appendChild(
                            new LzDataText(phone.getText()));
                    newNode.appendChild(childNode1);
                    newNode.appendChild(childNode2);
                    newNode.appendChild(childNode3);
                    parent.parent.setVisible(false);
                    firstName.clearText();
                    lastName.clearText();
                    phone.clearText();
                </method>
            </button>
        </view>
    </window>
</canvas>

The example is an application that can take user input and use the input as values for the next node it will add to the current dataset.

To try this example, use this URL:

http://localhost:8080/lps-4.0.x/app08/gridPointerTest1.lzx

Figures 8.4 and 8.5 show the generated application.

Figure 8.4. Using data pointers


Figure 8.5. Adding a contact


If you click the Add New Contact button, you will see a window you can use to enter the details of a new contact. This window is shown in Figure 8.5.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset