日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Flex使用彈出窗口為DataGrid添加新數(shù)據(jù)

系統(tǒng) 2795 0

經(jīng)常在Demo中會看到列表,表格等方式來顯示數(shù)據(jù)。當(dāng)然有時候也需要添加數(shù)據(jù)到這些列表或者表格中。有很多方式提交,這里展示一個彈出窗口的方式來添加新的數(shù)據(jù)到DataGrid中。

例子展示:

首先,我們開始建設(shè)一個基本的界面結(jié)構(gòu),一個帶有“Notes"標(biāo)題的Panel,一個DataGrid,以及一個用于提交數(shù)據(jù)的按鈕。

Xml代碼 復(fù)制代碼
  1. <? xml version = "1.0" encoding = "utf-8" ?>
  2. < mx:Application
  3. xmlns:mx = "http://www.adobe.com/2006/mxml"
  4. layout = "absolute"
  5. width = "500" height = "300" >
  6. < mx:Panel title = "Notes"
  7. width = "100%" height = "100%"
  8. layout = "vertical" horizontalAlign = "right"
  9. paddingTop = "3" paddingLeft = "3" paddingRight = "3" paddingBottom = "3" >
  10. < mx:DataGrid width = "100%" height = "100%" >
  11. < mx:columns >
  12. < mx:DataGridColumn headerText = "Author" dataField = "author" width = "80" />
  13. < mx:DataGridColumn headerText = "Topic" dataField = "topic" width = "100" />
  14. < mx:DataGridColumn headerText = "Description" dataField = "description" />
  15. </ mx:columns >
  16. </ mx:DataGrid >
  17. < mx:Button label = "AddNote" />
  18. </ mx:Panel >
  19. </ mx:Application >

這些代碼看起來并不陌生,DataGrid三個列的數(shù)據(jù)對應(yīng)我們Note類的三個屬性,我們定義Note類如下:

Xml代碼 復(fù)制代碼
  1. package
  2. {
  3. publicclassNote
  4. {
  5. publicvarauthor:String;
  6. publicvartopic:String;
  7. publicvardescription:String;
  8. }
  9. }

要真正使得我們的數(shù)據(jù)開始運轉(zhuǎn),我們還需要一個腳本塊:需要一個數(shù)據(jù)結(jié)構(gòu)來保存我們的Note信息。這里我們使用notes:ArrayCollection來記錄我們要添加和已經(jīng)添加的數(shù)據(jù)。這些數(shù)據(jù)能夠在DataGrid中顯示,是因為我們要把它設(shè)置成為DataGrid的provider.接下來我們先定義和初始化這個notes.

Js代碼 復(fù)制代碼
  1. <mx:Script>
  2. <![CDATA[
  3. import mx.collections.ArrayCollection;
  4. [Bindable]
  5. private var notes:ArrayCollection= new ArrayCollection();
  6. ]]>
  7. </mx:Script>

然后在把它設(shè)置成為datagrid的provider.

Xml代碼 復(fù)制代碼
  1. < mx:DataGrid dataProvider = "{notes}" width = "100%" height = "100%" >
  2. < mx:columns >
  3. < mx:DataGridColumn headerText = "Author" dataField = "author" width = "80" />
  4. < mx:DataGridColumn headerText = "Topic" dataField = "topic" width = "100" />
  5. < mx:DataGridColumn headerText = "Description" dataField = "description" />
  6. </ mx:columns >
  7. </ mx:DataGrid >

接下來,我們就要創(chuàng)建一個彈出的窗口,這里使用的是Flex組件TitleWindow.我們起名為AddNote.mxml.它將用于輸入界面,通過它,可以輸入與datagrid三列屬性對應(yīng)的數(shù)據(jù)。它還包含兩個按鈕:cancel和save.

Xml代碼 復(fù)制代碼
  1. <? xml version = "1.0" encoding = "utf-8" ?>
  2. < mx:TitleWindow xmlns:mx = "http://www.adobe.com/2006/mxml"
  3. layout = "absolute" width = "348" height = "218"
  4. title = "AddANote" >
  5. < mx:Label text = "Author" x = "35" y = "10" />
  6. < mx:TextInput id = "author" width = "150" x = "84" y = "8" />
  7. < mx:Label text = "Topic" y = "36" x = "42" />
  8. < mx:TextInput id = "topic" width = "150" x = "84" y = "34" />
  9. < mx:Label text = "Description" y = "62" x = "10" />
  10. < mx:TextArea id = "description" width = "234" height = "77" x = "84" y = "61" />
  11. < mx:Button label = "Cancel" x = "193" y = "146" />
  12. < mx:Button label = "Save" x = "264" y = "146" />
  13. </ mx:TitleWindow >

好了,我們已經(jīng)擁有一個可以作為數(shù)據(jù)輸入的界面,我們還要在我們的主程序中設(shè)定在某個合適的時間初始化并且顯示這個窗口,這個任務(wù)就交給了Application的creation complete事件。即在Application 創(chuàng)建的時候執(zhí)行:

Xml代碼 復(fù)制代碼
  1. < mx:Application
  2. xmlns:mx = "http://www.adobe.com/2006/mxml"
  3. layout = "absolute"
  4. width = "500" height = "300"
  5. creationComplete = "init()" >

在這個init()函數(shù)中,我們創(chuàng)建了AddNote的一個實例,并設(shè)置監(jiān)聽來自save按鈕的saveNote事件

Js代碼 復(fù)制代碼
  1. private var addNoteScreen:AddNote;
  2. private function init(): void
  3. {
  4. addNoteScreen= new AddNote();
  5. addNoteScreen.addEventListener( "SaveNote" ,saveNote);
  6. }

Xml代碼 復(fù)制代碼
  1. < mx:Button label = "AddNote" click = "addNote()" />

當(dāng)用戶點擊addNoe按鈕的時候就要彈出剛才創(chuàng)建的窗口。這里我們使用PopManager來簡單管理窗口的創(chuàng)建和關(guān)閉。

Js代碼 復(fù)制代碼
  1. private function addNote(): void
  2. {
  3. PopUpManager.addPopUp(addNoteScreen, this , true );
  4. PopUpManager.centerPopUp(addNoteScreen);
  5. addNoteScreen.author.text= "" ;
  6. addNoteScreen.topic.text= "" ;
  7. addNoteScreen.description.text= "" ;
  8. }

這里有兩個方法,方法一addPopUp,就是彈出窗口,這里我們傳輸了三個參數(shù),addNoteScreen為AddNote的一個實例,this為當(dāng)前窗口,true為是否設(shè)是否只有彈出的窗口可被點擊,即是否只有彈出的窗口處于Active狀態(tài)。第二個方法,就是設(shè)置彈出窗口的位置。

當(dāng)窗口彈出來的時候,我們可以做兩件事情,一提交保存用戶輸入數(shù)據(jù),二是簡單的關(guān)閉窗口。如果關(guān)閉窗口,我們也使用PopManager管理器:

Js代碼 復(fù)制代碼
  1. <mx:Script>
  2. <![CDATA[
  3. import mx.managers.PopUpManager;
  4. private function close(): void
  5. {
  6. PopUpManager.removePopUp( this );
  7. }
  8. ]]>
  9. </mx:Script>

Xml代碼 復(fù)制代碼
  1. < mx:Button label = "Cancel" click = "close()" x = "193" y = "146" />

若要保存用戶提交的數(shù)據(jù),我們需要調(diào)度一個自定義的事件.我們使用Event metadata tag來創(chuàng)建我們的自定義事件,而這個<metadata>標(biāo)記將在TitleWindow中創(chuàng)建。

Java代碼 復(fù)制代碼
  1. <mx:Metadata>
  2. [Event(name= "SaveNote" )]
  3. </mx:Metadata>

要調(diào)度這個時間,我們必須和按鈕save掛鉤起來:

Xml代碼 復(fù)制代碼
  1. < mx:Button label = "Save" click = "save()" x = "264" y = "146" />

這個方法將添加到腳本中,這個方法就是簡單調(diào)度SaveNoe事件:

Js代碼 復(fù)制代碼
  1. private function save(): void
  2. {
  3. this .dispatchEvent( new Event( "SaveNote" ));
  4. }

下面是TitleWindow所有代碼:

Xml代碼 復(fù)制代碼
  1. <? xml version = "1.0" encoding = "utf-8" ?>
  2. < mx:TitleWindow xmlns:mx = "http://www.adobe.com/2006/mxml"
  3. layout = "absolute" width = "348" height = "218"
  4. title = "AddANote" >
  5. < mx:Metadata >
  6. [Event( name = "SaveNote" )]
  7. </ mx:Metadata >
  8. < mx:Script >
  9. <![CDATA[
  10. importmx.managers.PopUpManager;
  11. privatefunctionclose():void
  12. {
  13. PopUpManager.removePopUp(this);
  14. }
  15. privatefunctionsave():void
  16. {
  17. this.dispatchEvent(newEvent("SaveNote"));
  18. }
  19. ]]>
  20. </ mx:Script >
  21. < mx:Label text = "Author" x = "35" y = "10" />
  22. < mx:TextInput id = "author" width = "150" x = "84" y = "8" />
  23. < mx:Label text = "Topic" y = "36" x = "42" />
  24. < mx:TextInput id = "topic" width = "150" x = "84" y = "34" />
  25. < mx:Label text = "Description" y = "62" x = "10" />
  26. < mx:TextArea id = "description" width = "234" height = "77" x = "84" y = "61" />
  27. < mx:Button label = "Cancel" click = "close()" x = "193" y = "146" />
  28. < mx:Button label = "Save" click = "save()" x = "264" y = "146" />
  29. </ mx:TitleWindow

要把彈出窗口中用戶輸入的數(shù)據(jù)顯示在Application 中的datagrid中,其實也很簡單,就是要數(shù)據(jù)綁定。前面的[Bindable]中的notes:ArrayCollecton就要與我們彈出窗口中的用戶輸入數(shù)據(jù)綁定起來。這個方法由save按鈕觸發(fā)后執(zhí)行:

Js代碼 復(fù)制代碼
  1. private function saveNote(e:Event): void
  2. {
  3. var note:Note= new Note();
  4. note.author=addNoteScreen.author.text;
  5. note.topic=addNoteScreen.topic.text;
  6. note.description=addNoteScreen.description.text;
  7. notes.addItem(note);
  8. PopUpManager.removePopUp(addNoteScreen);
  9. }

在綁定之后,即顯示在Application datagrid中之后,我們要把彈出的窗口關(guān)閉,即removePopUp。這里就是全部的介紹了,下面是Application的代碼:

Xml代碼 復(fù)制代碼
  1. <? xml version = "1.0" encoding = "utf-8" ?>
  2. < mx:Application
  3. xmlns:mx = "http://www.adobe.com/2006/mxml"
  4. layout = "absolute"
  5. width = "500" height = "300"
  6. creationComplete = "init()" >
  7. < mx:Script >
  8. <![CDATA[
  9. importmx.managers.PopUpManager;
  10. importmx.collections.ArrayCollection;
  11. [Bindable]
  12. privatevarnotes:ArrayCollection=newArrayCollection();
  13. privatevaraddNoteScreen:AddNote;
  14. privatefunctioninit():void
  15. {
  16. addNoteScreen=newAddNote();
  17. addNoteScreen.addEventListener("SaveNote",saveNote);
  18. }
  19. privatefunctionaddNote():void
  20. {
  21. PopUpManager.addPopUp(addNoteScreen,this,true);
  22. PopUpManager.centerPopUp(addNoteScreen);
  23. addNoteScreen.author.text="";
  24. addNoteScreen.topic.text="";
  25. addNoteScreen.description.text="";
  26. }
  27. privatefunctionsaveNote(e:Event):void
  28. {
  29. varnote:Note=newNote();
  30. note.author=addNoteScreen.author.text;
  31. note.topic=addNoteScreen.topic.text;
  32. note.description=addNoteScreen.description.text;
  33. notes.addItem(note);
  34. PopUpManager.removePopUp(addNoteScreen);
  35. }
  36. ]]>
  37. </ mx:Script >
  38. < mx:Panel title = "Notes"
  39. width = "100%" height = "100%"
  40. layout = "vertical" horizontalAlign = "right"
  41. paddingTop = "3" paddingLeft = "3" paddingRight = "3" paddingBottom = "3" >
  42. < mx:DataGrid dataProvider = "{notes}" width = "100%" height = "100%" >
  43. < mx:columns >
  44. < mx:DataGridColumn headerText = "Author" dataField = "author" width = "80" />
  45. < mx:DataGridColumn headerText = "Topic" dataField = "topic" width = "100" />
  46. < mx:DataGridColumn headerText = "Description" dataField = "description" />
  47. </ mx:columns >
  48. </ mx:DataGrid >
  49. < mx:Button label = "AddNote" click = "addNote()" />
  50. </ mx:Panel >
  51. </ mx:Application >

參考翻譯: http://www.switchonthecode.com/tutorials/flex-datagrid-adding-rows-using-a-popup

Flex使用彈出窗口為DataGrid添加新數(shù)據(jù)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 锦州市| 博湖县| 徐汇区| 疏附县| 苏尼特左旗| 常山县| 巴青县| 华阴市| 稷山县| 富川| 泰安市| 建德市| 兰州市| 兴仁县| 耒阳市| 通山县| 阿巴嘎旗| 大姚县| 柘城县| 肃北| 潮安县| 鄯善县| 正阳县| 塘沽区| 太湖县| 加查县| 武安市| 枝江市| 蓬莱市| 文山县| 保德县| 平顺县| 舒城县| 永兴县| 资阳市| 宜昌市| 山西省| 舞钢市| 永康市| 元氏县| 略阳县|