问题
I am trying to display a list of items in a datagrid from an XMLList.
<Series no="1">
<file>
<filenum>1</epnum>
<prodnum>4V01</prodnum>
<title>Series #1 - File #1</title>
</file>
<file>
<filenum>2</epnum>
<prodnum>4V02</prodnum>
<title>Series #1 - File #2</title>
</file>
</Series>
<Series no="2">
<file>
<filenum>1</epnum>
<prodnum>4V01</prodnum>
<title>Series #2 - File #1</title>
</file>
<file>
<filenum>2</epnum>
<prodnum>4V02</prodnum>
<title>Series #2 - File #2</title>
</file>
</Series>
My current code allows me to retrieve every Series into an XMLList and then i have a nesteddatagrid class that allows me to do things like.
<classes:NestedDataGrid width="100%" height="100%" id="gridFiles" dataProvider="{filesList}" >
<classes:columns>
<mx:DataGridColumn headerText="Season" dataField="@no" width="60"/>
<mx:DataGridColumn headerText="Episode" dataField="file.filenum" width="60"/>
<mx:DataGridColumn headerText="Title" dataField="file.title"/>
</classes:columns>
</classes:NestedDataGrid>
However this displays the datagrid with two rows, the first row has 1 in the Series column and then the two files crammed into the second cell in the same row. The second row is the same but has the number 2 in the Series column and the two series #2 files crammed into the cell next to it.
If i do not use the nested data class i can pull the files using Series.file instead and all 4 of the files list correctly, however i do not get the Series number for each...
回答1:
With the current structure of the xml, it's easier to represent it with a two column grid - first column being the series number, and second column being another 2 or 3 column DataGrid that displays file details. But if you don't wanna change the structure, the following code is what you need. Note that since dataField
property is not set, you have to specify a sortCompareFunction
for sorting the grid based on series number - otherwise it might throw exceptions while trying to sort.
<classes:NestedDataGrid width="100%" height="100%" id="gridFiles"
dataProvider="{filesList.Series.file}" >
<classes:columns><!-- classes copy pasted from OP's code. Whats that? -->
<mx:DataGridColumn headerText="Season" labelFunction="getSeries" width="60"/>
<mx:DataGridColumn headerText="Episode" dataField="filenum" width="60"/>
<mx:DataGridColumn headerText="Title" dataField="title"/>
</classes:columns>
</classes:NestedDataGrid>
private function getSeries(item:Object, col:DataGridColumn):String
{
return XML(item).parent().@no;
}
UPDATE:
<mx:DataGrid width="100%" height="100%" id="gridFiles" >
<mx:columns>
<mx:DataGridColumn headerText="Season" labelFunction="getSeries" width="60"/>
<mx:DataGridColumn headerText="Episode" dataField="epnum" width="60"/>
<mx:DataGridColumn headerText="Title" dataField="title"/>
</mx:columns>
</mx:DataGrid>
gridFiles.dataProvider = XML(event.result).descendants('episode');
//use the same getSeries function as above
来源:https://stackoverflow.com/questions/1591467/deep-nested-xml