简体   繁体   中英

How do you add a new attribute to a dataset node in OpenLaszlo platform?

如何在OpenLaszlo XML数据集中的节点上添加全新属性?

The way to do this is to use the lz.datapointer.setNodeAttribute() function. If you use the setNodeAttribute() function with an attribute name that does not already appear on the node, a new one will be created.

In the sample OpenLaszlo application below, if you press the button titled [displayXML] after you compile the program, you will see the XML dataset before any changes are made does not contain any "fav_saying" attribute.

After you click the [updateAttribute] button to add the favorite saying for Homer via the setNodeAttribute() method, you can then click the [displayXML] button again and you will see that an attribute called 'fav_saying' has been added to the XML dataset.

<canvas height="665" width="1000" layout="axis: x" debug="true">

<dataset name="myData">
<myXML>
<person firstname="Homer" lastname="Simpson" />
<person firstname="Marge" lastname="Simpson" />
<person firstname="Montgomery" lastname="Burns" />
</myXML>
</dataset>

<button text="displayXML">

<handler name="onclick">
  Debug.write(canvas.myData.serialize());
</handler>

</button>

<button text="updateAttribute">

<handler name="onclick">  

  var dp = canvas.myData.getPointer(); // get datapointer to XML data
  dp.setXPath('myXML/person[@firstname="Homer"]'); // set xpath to Homer Simpson

  dp.setNodeAttribute('fav_saying', 'DOH!');

</handler>

</button>

</canvas>

You will also see that multiple calls to setNodeAttribute() will not add additional 'fav_saying' attributes. If the program used a different value for the saying every time then the value in the 'fav_saying' attribute would change but there would still only be one 'fav_saying' attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM