PDA

View Full Version : access subviews during ondata event


Peter_Chea
06-11-2003, 12:57 PM
I am trying to access a subview during the ondata event. Is possible?

<text name="price" multiline="true"/>
<method name="processData">
//debug.write(this.datapath.getNodeName());
this.price.setText(this.datapath.getXPath("price/text()") * 1.10);
</method>






<canvas debug="true">

<dataset name="hotelresult">
<item>
<name>BUENA VISTA SUITES</name>
<price>$20</price>
</item>
<item>
<name>QUALITY SUITES MAINGATE EAST</name>
<price>$20</price>
</item>
<item>
<name>HOLIDAY INN MAINGATE WEST</name>
<price>$20</price>
</item>
<item>
<name>RAMADA INN RESORT</name>
<price>$20</price>
</item>
<item>
<name>BDOUBLETREE RESORT VILLAS</name>
<price>$20</price>
</item>

<item>
<name>DAYS SUITES MAINGATE EAST</name>
<price>$20</price>
</item>
<item>
<name>HILTON WALT DISNEY WORLD</name>
<price>$20</price>
</item>
<item>
<name>HOWARD JOHNSON MAINGATE EAST</name>
<price>$20</price>
</item>
</dataset>

<class name="simplelayout2" extends="layout" >
<attribute name="axis" value="y" onset="this.setAxis( axis );"
type="string" />
<attribute name="spacing" value="0"
onset="this.spacing = spacing;
if( this.subviews.length ) this.update()"/>
<attribute name="altbgcolor" type="number"
onset="this.altbgcolor = altbgcolor;"/>
<method name="setAxis" args="a" >
this.axis = a;
this.sizeAxis = a == "x" ? "width" : "height"
</method>

<method name="addSubview" args="newsub">
this.updateDelegate.register( newsub, "on" + this.sizeAxis);
if ( ! this.locked &amp;&amp; this.subviews.length ){
var s= this.subviews[ this.subviews.length-1 ];
var p = s.getAttribute( this.axis ) +
s.getAttribute( this.sizeAxis );
if ( this.subviews.length ){
p += this.spacing;
}
newsub.setAttribute( this.axis , p ) ;

}
super.addSubview( newsub );
</method>
<method name="update">
<![CDATA[
if ( this.locked ) return;
var l = this.subviews.length;
var c = 0;

for(var i=0; i < l; i++) {
var s = this.subviews[i];
if(i % 2)
{
//debug.write("altcolor:" + this.altbgcolor);
s.setAttribute("bgcolor", this.altbgcolor);
//s.setBGColor(this.altbgcolor);
}
else
{
//debug.write("normalcolor:" +this.bgcolor);
s.setAttribute("bgcolor", this.bgcolor);
}
s.setAttribute( this.axis , c );
//trace("setAttribute after= " + this.axis);
c += this.spacing + s.getAttribute( this.sizeAxis ) ;
}

]]>
</method>


</class>
<class name="copylayout" extends="layout" >
<attribute name="axis" value="y" onset="this.setAxis( axis )"
type="string" />
<attribute name="spacing" value="0"
onset="this.spacing = spacing;
if( this.subviews.length ) this.update()"/>
<attribute name="copyFrom" type="expression"/>

<method name="setAxis" args="a" >
this.axis = a;
this.sizeAxis = a == "x" ? "width" : "height"
</method>

<method name="addSubview" args="newsub">
this.updateDelegate.register( newsub, "on" + this.sizeAxis);
super.addSubview( newsub );
</method>
<method name="update">
<![CDATA[
if ( this.locked ) return;
var l = Math.min(this.subviews.length, copyFrom.subviews.length);

for(var i=0; i < l; i++) {
var src = copyFrom.subviews[ i ];
var dst = this.subviews[ i ];
dst.setAttribute(this.axis, src.getAttribute(this.axis));
dst.setAttribute(this.sizeAxis, src.getAttribute(this.sizeAxis));
}

]]>
</method>
</class>



<view name="headergroup">
<view name="header" initstage="early" bgcolor="#aaaaaa">
<text width="80" name="name">Name</text>
<text width="80" name="price">Price</text>
<simplelayout axis="x" spacing="20"/>
</view>
</view>

<view name="data" datapath="hotelresult:/item" ondata="this.processData();" >
<text name="name" multiline="true" datapath="name/text()"/>
<text name="price" multiline="true"/>
<copylayout axis="x">
<attribute name="copyFrom" init="parent.parent.headergroup.header" />
</copylayout>
<method name="processData">
//debug.write(this.datapath.getNodeName());
this.price.setText(this.datapath.getXPath("price/text()") * 1.10);
</method>
</view>

<simplelayout2 axis="y">
<attribute name="bgcolor" value="0xcccccc"/>
<attribute name="altbgcolor" value="0xf0f0f0"/>

</simplelayout2>
</canvas>

antun
06-11-2003, 01:39 PM
Hey Peter_Chea

Accessing subviews during an ondata event is undefined. The reason for this is that you might have replicated views in there too.

You might want to try this:


<view name="data" datapath="hotelresult:/item">
<text name="name" multiline="true" datapath="name/text()"/>
<text name="price" datapath="price/text()" width="50" multiline="true"/>
<copylayout axis="x">
<attribute name="copyFrom"
init="parent.parent.headergroup.header" />
</copylayout>
</view>


-Antun

Peter_Chea
06-11-2003, 04:28 PM
if I have a column that combine the content of two nodes such as the discount column, How would I do it?
<view name="data" datapath="hotelresult:/item">
<text name="name" datapath="name/text()"/>
<text name="price" datapath="price/text()" width="50"/>
<text name="discount" datapath="price/text() * discount/text()" width="50"/>
</view>

antun
06-11-2003, 07:13 PM
You could try something like:


<view name="data" datapath="hotelresult:/item">
<text name="name" datapath="name/text()"/>
<text name="price" datapath="price/text()" width="50"/>
<text name="discount"
ondata="this.setText( parent.datapath.getXPath('price/text()')
* parent.datapath.getXPath('discount/text()'))"
width="50"/>
</view>


Let me know if that works...

-Antun

Peter_Chea
06-12-2003, 09:43 AM
Without the datapath attribute, the ondata event was never called. I end up putting datapath="" in the code.

<view name="data" datapath="hotelresult:/item">
<text name="name" datapath="name/text()"/>
<text name="price" datapath="price/text()" width="50"/>
<text name="discount" datapath=""
ondata="this.setText( parent.datapath.getXPath('price/text()')
* parent.datapath.getXPath('discount/text()'))"
width="50"/>
</view>

rdbetz
06-17-2003, 12:42 PM
For what it's worth, this should work as well:

<view name="data" datapath="hotelresult:">
<text name="name" datapath="item/name/text()"/>
<text name="price" datapath="item/price/text()" width="50"/>
<text name="discount" datapath="item"
ondata="this.setText( this.datapath.getXPath('price/text()')
* this.datapath.getXPath('discount/text()'))"
width="50"/>
</view>