フォーラム一覧 - トピック一覧 Flex Builder 3
DataGridのソートについて質問 | 投稿するにはまず登録を |
| スレッド表示 | 新しいものから | 前のトピック | 次のトピック | 下へ |
| 投稿者 | トピック |
|---|---|
| gakugun | 投稿日時: 2008-6-25 16:12 |
やや お馴染みさん ![]() ![]() 登録日: 2008-6-25 居住地: 投稿: 8 |
DataGridのソートについて質問 XMLListのデータを利用するDataGridに「数値データ」をソートするとき、「文字列」としてソートされました。
どうすれば宜しいでしょうか? <?xml version="1.0"?> <!-- DataGrid control example. --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:XMLList id="employees"> <employee> <name>Christina Coenraets</name> <price>12345</price> <email>ccoenraets@fictitious.com</email> <active>true</active> </employee> <employee> <name>Joanne Wall</name> <price>234569</price> <email>jwall@fictitious.com</email> <active>true</active> </employee> <employee> <name>Maurice Smith</name> <price>10320</price> <email>maurice@fictitious.com</email> <active>false</active> </employee> <employee> <name>Mary Jones</name> <price>9879</price> <email>mjones@fictitious.com</email> <active>true</active> </employee> </mx:XMLList> <mx:Panel title="DataGrid Control Example" height="100%" width="100%" paddingTop="10" paddingLeft="10" paddingRight="10"> <mx:Label width="100%" color="blue" text="Select a row in the DataGrid control."/> <mx:DataGrid id="dg" width="100%" height="100%" rowCount="5" dataProvider="{employees}"> <mx:columns> <mx:DataGridColumn dataField="name" headerText="Name"/> <mx:DataGridColumn dataField="price" headerText="price"/> <mx:DataGridColumn dataField="email" headerText="Email"/> </mx:columns> </mx:DataGrid> </mx:Panel> </mx:Application> |
| T-mac | 投稿日時: 2008-6-25 16:33 |
ご主人様 ![]() ![]() 登録日: 2008-2-21 居住地: 松戸 投稿: 60 |
Re: DataGridのソートについて質問 gakugunさん、こんにちは>
DataGridColumnにsortCompareFunctionを追加すればいいと思います。 <?xml version="1.0"?> <!-- DataGrid control example. --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:Script> <![CDATA[ import mx.utils.ObjectUtil; private function sortPirces(obj1:Object, obj2:Object):int{ var value1:Number = obj1.price; var value2:Number = obj2.price; if(value1 < value2) { return -1; } else if(value1 > value2){ return 1; } else { return 0; } } ]]> </mx:Script> <mx:XMLList id="employees"> <employee> <name>Christina Coenraets</name> <price>12345</price> <email>ccoenraets@fictitious.com</email> <active>true</active> </employee> <employee> <name>Joanne Wall</name> <price>234569</price> <email>jwall@fictitious.com</email> <active>true</active> </employee> <employee> <name>Maurice Smith</name> <price>10320</price> <email>maurice@fictitious.com</email> <active>false</active> </employee> <employee> <name>Mary Jones</name> <price>9879</price> <email>mjones@fictitious.com</email> <active>true</active> </employee> </mx:XMLList> <mx:Panel title="DataGrid Control Example" height="100%" width="100%" paddingTop="10" paddingLeft="10" paddingRight="10"> <mx:Label width="100%" color="blue" text="Select a row in the DataGrid control."/> <mx:DataGrid id="dg" width="100%" height="100%" rowCount="5" dataProvider="{employees}"> <mx:columns> <mx:DataGridColumn dataField="name" headerText="Name"/> <mx:DataGridColumn dataField="price" headerText="price" sortCompareFunction="sortPirces"/> <mx:DataGridColumn dataField="email" headerText="Email"/> </mx:columns> </mx:DataGrid> </mx:Panel> </mx:Application> |
| gakugun | 投稿日時: 2008-6-25 16:48 |
やや お馴染みさん ![]() ![]() 登録日: 2008-6-25 居住地: 投稿: 8 |
Re: DataGridのソートについて質問 T-macさん
ご回答ありがとうございます。ソートできました。 DataGridに初期表示時 データをソートし「昇順」の矢印を表示したいが、できますか? |
| goki | 投稿日時: 2008-6-25 17:08 |
ご主人様 ![]() ![]() 登録日: 2007-8-27 居住地: おかのよこはま 投稿: 735 |
Re: DataGridのソートについて質問 dataProviderに設定するArrayCollectionにSort/SortFieldを設定すれば可能です。
|
| T-mac | 投稿日時: 2008-6-25 18:29 |
ご主人様 ![]() ![]() 登録日: 2008-2-21 居住地: 松戸 投稿: 60 |
Re: DataGridのソートについて質問 gakugunさん、gokiさんの言うとおりです。
gakugunさんの例にXMLListを使っていますね、ちょっとめんどうくさいですけど、XMLListをArrayCollectionに転換して実現しました。 <?xml version="1.0"?> <!-- DataGrid control example. --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="changeXML()"> <mx:Script> <![CDATA[ import mx.utils.ArrayUtil; import mx.controls.Alert; import mx.collections.ArrayCollection; import mx.utils.ObjectUtil; import mx.collections.Sort; import mx.collections.SortField; private function sortPirces(obj1:Object, obj2:Object):int{ var value1:Number = obj1.price; var value2:Number = obj2.price; if(value1 < value2) { return -1; } else if(value1 > value2){ return 1; } else { return 0; } } [Bindable] private var employees1:ArrayCollection=new ArrayCollection(); private function changeXML():void{ var tempList:XMLList=new XMLList(employees); var itemLen:Number=tempList.length(); for(var i:int=0;i<itemLen;i++){ var obj:Object={"name":tempList[i].name,"price":tempList[i].price ,"email":tempList[i].email,"active":tempList[i].active} employees1.addItem(obj); } employees1.sort = new Sort(); employees1.sort.fields = [new SortField("price", false, false)]; employees1.refresh(); } ]]> </mx:Script> <mx:XMLList id="employees"> <employee> <name>Christina Coenraets</name> <price>12345</price> <email>ccoenraets@fictitious.com</email> <active>true</active> </employee> <employee> <name>Joanne Wall</name> <price>234569</price> <email>jwall@fictitious.com</email> <active>true</active> </employee> <employee> <name>Maurice Smith</name> <price>10320</price> <email>maurice@fictitious.com</email> <active>false</active> </employee> <employee> <name>Mary Jones</name> <price>9879</price> <email>mjones@fictitious.com</email> <active>true</active> </employee> </mx:XMLList> <mx:Panel title="DataGrid Control Example" height="100%" width="100%" paddingTop="10" paddingLeft="10" paddingRight="10"> <mx:Label width="100%" color="blue" text="Select a row in the DataGrid control."/> <mx:DataGrid id="dg" width="100%" height="100%" rowCount="5" dataProvider="{employees1}"> <mx:columns> <mx:DataGridColumn dataField="name" headerText="Name"/> <mx:DataGridColumn dataField="price" headerText="price" sortCompareFunction="sortPirces"/> <mx:DataGridColumn dataField="email" headerText="Email"/> </mx:columns> </mx:DataGrid> </mx:Panel> </mx:Application> |
| gakugun | 投稿日時: 2008-6-26 11:49 |
やや お馴染みさん ![]() ![]() 登録日: 2008-6-25 居住地: 投稿: 8 |
Re: DataGridのソートについて質問 T-macさん: ソートの矢印が表示されたが、データがまたテキストとしてソートされました。 引用:
|
| T-mac | 投稿日時: 2008-6-26 15:35 |
ご主人様 ![]() ![]() 登録日: 2008-2-21 居住地: 松戸 投稿: 60 |
Re: DataGridのソートについて質問 gakugunさん、以下
employees1.sort.fields = [new SortField("price", false, false)];→ employees1.sort.fields = [new SortField("price", false, false,true)];修正してください。 |
| gakugun | 投稿日時: 2008-6-27 14:58 |
やや お馴染みさん ![]() ![]() 登録日: 2008-6-25 居住地: 投稿: 8 |
Re: DataGridのソートについて質問 T-macさん:
ご回答有難うございます。思ったとおり表示されました。 これからもよろしく。 |
| スレッド表示 | 新しいものから | 前のトピック | 次のトピック | トップ |
| 投稿するにはまず登録を | |