问题
My webapplication is giving XML in following format:
<Vendor name="Infosys Limited" adopTotal="120" totalstrength="775" FY10Q1="162" FY10Q2="132" FY10Q3="229" FY10Q4="254" FY11Q1="282" FY11Q2="243" FY11Q3="238" FY11Q4="233" FY12Q1="237" FY12Q2="136" FY12Q3="101" FY12Q4="120"/>
<Vendor name="Wipro" adopTotal="42" totalstrength="1181" FY10Q1="9" FY10Q2="12" FY10Q3="53" FY10Q4="67" FY11Q1="85" FY11Q2="76" FY11Q3="84" FY11Q4="82" FY12Q1="98" FY12Q2="51" FY12Q3="39" FY12Q4="42"/>
<Vendor name="Zensar Technologies Inc" adopTotal="187" totalstrength="1408" FY10Q1="207" FY10Q2="197" FY10Q3="428" FY10Q4="490" FY11Q1="511" FY11Q2="479" FY11Q3="471" FY11Q4="440" FY12Q1="440" FY12Q2="308" FY12Q3="137" FY12Q4="187"/>
<Vendor name="Hcl Technologies Limited" adopTotal="0" totalstrength="342" FY10Q1="0" FY10Q2="0" FY10Q3="0" FY10Q4="0" FY11Q1="3" FY11Q2="0" FY11Q3="9" FY11Q4="0" FY12Q1="0" FY12Q2="1" FY12Q3="1" FY12Q4="0"/>
<Vendor name="Mahindra Satyam" adopTotal="73" totalstrength="12" FY10Q1="106" FY10Q2="69" FY10Q3="50" FY10Q4="63" FY11Q1="117" FY11Q2="203" FY11Q3="192" FY11Q4="195" FY12Q1="208" FY12Q2="139" FY12Q3="53" FY12Q4="73"/>
<Vendor name="TCS" adopTotal="48" totalstrength="106" FY10Q1="54" FY10Q2="75" FY10Q3="140" FY10Q4="164" FY11Q1="164" FY11Q2="140" FY11Q3="147" FY11Q4="134" FY12Q1="124" FY12Q2="83" FY12Q3="49" FY12Q4="48"/>
But Datagrid is not following the format. Column should come according to xml variables. The order of the columns are not like xml.
I have simply added dataprovider to datagrid. Please help.
Thanks Rimi
回答1:
Datagrid definition:
<s:DataGrid id="datagrid" dataProvider="{dp}"/>
When you receive data from backend, call the following function for each attribute in your xml:
private function addDataGridColumn(dataField:String):void
{
var dgc:DataGridColumn = new DataGridColumn(dataField);
var cols:Array = datagrid.columns;
cols.push(dgc);
datagrid.columns = cols;
}
Hope that helps.
回答2:
You'll have to define your columns explicitly, like this:
<s:DataGrid dataProvider="{dp}">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="@name" headerText="Name" />
<s:GridColumn dataField="@adopTotal" headerText="Adop" />
<s:GridColumn dataField="@totalstrength" headerText="Strength" />
</s:ArrayList>
</s:columns>
</s:DataGrid>
And it's very similar with the old mx DataGrid:
<mx:DataGrid dataProvider="{dp}">
<mx:columns>
<mx:DataGridColumn dataField="..." headerText="..." />
</mx:columns>
</mx:DataGrid>
The dataField
property determines which property you want to display in that column.
The headerText
property determines the label that will be displayed in the column header.
The @
sign refers to an XML attribute; you don't need it with regular collections.
回答3:
The way you declare the properties does not reflect the order of the properties are read internal. To ensure the right order of the columns manually create them in your datagrid.
For example
<mx:DataGrid id="dg" width="100%" height="100%" rowCount="5" dataProvider="{employees}">
<mx:columns>
<mx:DataGridColumn dataField="@name" headerText="Name"/>
<mx:DataGridColumn dataField="@adopTotal" headerText="Total"/>
<....
</mx:columns>
</mx:DataGrid>
来源:https://stackoverflow.com/questions/11119962/columns-are-not-coming-in-order-like-in-xml-to-datagrid