这个其实是最基本的实现之一,唯一不爽的是,flex能够自动识别的通常是格式单一整洁的XML,.NET WebService返回的数据集也是XML的格式,不过太过复杂,flex并不能直接从中识别我们真正想要使用的数据,参照网上查找到的做法,我也另外写了一个方法来把数据整理成XML,再以String的方式返回给flex前端,还好,它能看懂。
public
class
WsUtil
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//……
![](/Images/OutliningIndicators/InBlock.gif)
public static string BuildXmlFromData(DataSet ds, AttributeFieldPair[] pairs)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DataTable dt = ds.Tables[0];
XmlDocument xmlDoc = new XmlDocument();
XmlNode root = xmlDoc.CreateElement("node");
XmlAttribute xa = xmlDoc.CreateAttribute("label");
xa.Value = "摘要";
root.Attributes.Append(xa);
xmlDoc.AppendChild(root);
//
foreach(DataRow dr in dt.Rows)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
XmlNode node = xmlDoc.CreateElement("node");
BuildNodeAttributes(ref node, pairs, dr);
root.AppendChild(node);
}
//
return xmlDoc.InnerXml;
}
![](/Images/OutliningIndicators/InBlock.gif)
protected static void BuildNodeAttributes(ref XmlNode node, AttributeFieldPair[] pairs, DataRow dr)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach(AttributeFieldPair pair in pairs)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
XmlAttribute xa = node.OwnerDocument.CreateAttribute(pair.AttributeName);
xa.Value = dr[pair.FieldName].ToString();
node.Attributes.Append(xa);
}
}
}
![](/Images/OutliningIndicators/None.gif)
public
struct
AttributeFieldPair
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public string AttributeName;
public string FieldName;
![](/Images/OutliningIndicators/InBlock.gif)
public AttributeFieldPair(string an, string fn)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.AttributeName = an;
this.FieldName = fn;
}
}
public
class
DefaultService : System.Web.Services.WebService
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//……
![](/Images/OutliningIndicators/InBlock.gif)
[WebMethod]
public string GetStruct()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WsUtil u = new WsUtil();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
DataSet ds = u.ExecuteQuery("Spector_GetAllProductGroups", new string[]
{}, new object[]
{});
AttributeFieldPair[] pairs = new AttributeFieldPair[1];
pairs[0] = new AttributeFieldPair("label", "Name");
string xml = WsUtil.BuildXmlFromData(ds, pairs);
return xml;
}
}
flex前端把数据直接绑定到tree上:
<?
xml version="1.0" encoding="utf-8"
?>
<
mx:Application
xmlns:mx
="http://www.adobe.com/2006/mxml"
layout
="absolute"
viewSourceURL
="srcview/index.html"
height
="100%"
width
="100%"
pageTitle
="TCost Sample"
>
<
mx:Style
source
="CSS/Common.css"
></
mx:Style
>
<
mx:WebService
id
="WS"
wsdl
="http://localhost/TCostWS/DefaultService.asmx?WSDL"
fault
="mx.controls.Alert.show(event.fault.faultString), 'Error'"
>
<
mx:operation
name
="GetStruct"
resultFormat
="object"
result
="BindStruct(event.result.toString())"
>
</
mx:operation
>
</
mx:WebService
>
![](/Images/OutliningIndicators/None.gif)
<
mx:Script
>
<![CDATA[
import mx.controls.Alert;
function BindStruct(result:String):void
{
var doc:XMLDocument = new XMLDocument(result);
tree1.dataProvider = doc;
mx.controls.Alert("done");
}
]]>
</
mx:Script
>
![](/Images/OutliningIndicators/None.gif)
<
mx:VBox
width
="100%"
height
="99%"
paddingLeft
="10"
paddingRight
="10"
>
<
mx:HBox
verticalAlign
="bottom"
width
="100%"
>
<
mx:Label
text
="TCost"
fontSize
="32"
paddingLeft
="20"
/>
<
mx:Label
id
="lblUser"
text
="早上好,Spector"
paddingLeft
="20"
/>
<
mx:HBox
verticalAlign
="bottom"
width
="100%"
horizontalAlign
="right"
>
<
mx:ToggleButtonBar
>
<
mx:dataProvider
>
<
mx:Array
>
<
mx:String
>
按月度汇总
</
mx:String
>
<
mx:String
>
按季度汇总
</
mx:String
>
</
mx:Array
>
</
mx:dataProvider
>
</
mx:ToggleButtonBar
>
</
mx:HBox
>
</
mx:HBox
>
<
mx:HDividedBox
width
="100%"
height
="100%"
dividerThickness
="2"
>
<
mx:Panel
width
="20%"
height
="100%"
title
="用户视角"
>
<
mx:Accordion
width
="100%"
height
="100%"
id
="accordion1"
>
<
mx:VBox
label
="BU/产品族"
width
="100%"
height
="100%"
>
<
mx:Tree
width
="100%"
height
="100%"
enabled
="true"
id
="tree1"
dataProvider
="{WS.GetStruct()}"
labelField
="@label"
borderThickness
="0"
></
mx:Tree
>
</
mx:VBox
>
</
mx:Accordion
>
</
mx:Panel
>
<
mx:TitleWindow
height
="100%"
width
="100%"
title
="数据展现"
>
<
mx:Label
text
="windows"
/>
</
mx:TitleWindow
>
</
mx:HDividedBox
>
</
mx:VBox
>
</
mx:Application
>
具体调用的WebService的方法有点奇怪,不是很理解,不过目前先放一放。
需要注意的是,描述URL使用机器名还是localhost是需要统一的,如果我是使用
http://localhost:8700/……
这个路径的话,那页面里面描述WebService路径的时候也要使用locahost,否则会出现跨域访问的错误。
据说从本机环境迁移到发布环境的话,还是要再次面临跨域访问的问题,不过目前也先放一放。