2006-12-22

Tree.dataProvider com XML complexo



Essa vai para um novo amigo.

Para construir a Tree foi usado o seguinte XML, para simular a saída produzida pela framework usada.
<?xml version="1.0" encoding="utf-8"?>
<tabela>
<faturamento>
<diafatura><![CDATA[00-00-0000]]></diafatura>
<diafatura><![CDATA[00-00-0000]]></diafatura>
<diafatura><![CDATA[00-00-0000]]></diafatura>
<diafatura><![CDATA[00-00-0000]]></diafatura>
<empresa><![CDATA[1]]></empresa>
<empresa><![CDATA[1]]></empresa>
<empresa><![CDATA[1]]></empresa>
<cliente><![CDATA[1]]></cliente>
<cliente><![CDATA[1]]></cliente>
<cliente><![CDATA[1]]></cliente>
<valor><![CDATA[15.00]]></valor>
<valor><![CDATA[15.00]]></valor>
<valor><![CDATA[15.00]]></valor>
<id><![CDATA[1]]></id>
<id><![CDATA[2]]></id>
<id><![CDATA[3]]></id>
</faturamento>
<empresas>
<id><![CDATA[1]]></id>
<id><![CDATA[2]]></id>
<codigo><![CDATA[1]]></codigo>
<codigo><![CDATA[1]]></codigo>
<razaoSocial><![CDATA[tal]]></razaoSocial>
<razaoSocial><![CDATA[tal]]></razaoSocial>
<cnpj><![CDATA[545]]></cnpj>
<cnpj><![CDATA[545]]></cnpj>
<observacao><![CDATA[obs]]></observacao>
<observacao><![CDATA[obs]]></observacao>
<created_at><![CDATA[00-00-000]]></created_at>
<created_at><![CDATA[00-00-000]]></created_at>
<updated_at><![CDATA[00-00-000]]></updated_at>
<updated_at><![CDATA[00-00-000]]></updated_at>
<lock_version><![CDATA[00-00-000]]></lock_version>
<lock_version><![CDATA[00-00-000]]></lock_version>
</empresas>
</tabela>


E o script Flex fica da seguinte forma.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" creationComplete="myload.send()">
<mx:Panel layout="absolute" id="panel1" title="Tree" cornerRadius="13"
width="321" height="236">
<mx:Tree x="10" y="10" width="282" height="146" id="myTree"
dataProvider="{listDP}" labelField="@id"></mx:Tree>
</mx:Panel>
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.collections.XMLListCollection;
import mx.controls.Alert;
[]
private var listDP:XMLListCollection = new XMLListCollection(new XMLList());

private function loadResult(event:ResultEvent):void{

var keys:Object = {
faturamento:["id","diafatura","empresa","cliente","valor"],
empresas:["id","codigo","razaoSocial","cnpj","observacao","created_at","updated_at","lock_version"]
};
var i:Number = 0;
var strNode:String = "";
var newNode:XML;
for(var x:String in myload.lastResult.tabela){
newNode = XML("<tabela id='"+x+"' data='"+x+"' />");
listDP.addItem(newNode);

for(var y:Number = 0; y < event.result.tabela[x][keys[x][0]].length;y++){
strNode = "<registro ";
for(var z:Number = 0 ; z < keys[x].length ;z++){
strNode += keys[x][z]+"='"+event.result.tabela[x][keys[x][z]][y]+"' ";
}
strNode += " />";
newNode = XML(strNode);
listDP[i].appendChild(newNode);

}
i++;
}
}

]]>
</mx:Script>
<mx:HTTPService id="myload" url="http://localhost:8080/cfusion/file.xml" result="loadResult(event)" />
</mx:Application>

2 comentários:

matias delgado disse...

Mas percebi que nenhuma demonstra o uso de DragDrop, então implementei um pequeno script de exemplo para vocês, e o resultado pode ser observado no filme a seguir.

data logging disse...

Thanks bro very good article