• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2811
  • Last Modified:

Automatic sort column in DataGrid

Hello!

Please can somebody show me, how i can sort a specific column in a datagrid?
Hers an example: The datagrid has 4 columns (id, user_firstname, user_lastname, emailaddress). Now when i add a new user and the datagrid refreshs, the new added user is shown as last, because the id is sorting.

Please i need a solution where the column (user_lastname) is sorting automatic.

Thank´s in advance!

Regards,
Mario
0
20one
Asked:
20one
  • 9
  • 8
1 Solution
 
Jones911Commented:
Check out this post:

http://www.bealearts.co.uk/blog/2007/06/15/

You can dispatch and event in you resultHndeler to resort the datagrid on the column you specify.
0
 
20oneAuthor Commented:
Thank´s!

I know this post. But what i have to write instead (destination"Coldfusion"). Do i need this whole RemoteObject?
0
 
Jones911Commented:
Without seeing some code samples I cant tell.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
20oneAuthor Commented:
It´s a simple DataGrid with populating data from mysql with php. I´m not at home and i can´t show you now a code sample. I will be in about 3 hours at home, then i will poste the code.

Thank´s!

Regards,
Mario
0
 
Jones911Commented:
Oh you using HTTPservice on the result handler for that rather then the remote object is where you need the result Handler.
0
 
Gary BenadeCommented:
Assuming the dataprovider is an ArrayCollection called yourArrayCollection, in the remote service result handler:

yourArrayCollection.source = event.result; // assign the new data to your arraycollection
yourArrayCollection.sort = new Sort();
yourArrayCollection.sort.fields = [new SortField("user_lastname", true)];
yourArrayCollection.refresh();
0
 
20oneAuthor Commented:
Hello,

thank´s for the answer. I´m not sure if it´s aan ArrayCOllection. I don´t think so. Here´s the code. Please can you show it to me?

Thank´s in advance!
<mx:Script> 
private function init():void {
userRequest.send();
}
</mx:Script>
 
<mx:DataGrid change="eventListener(event)" id="dgUserRequest" width="100%" height="100%" x="0" y="50" dataProvider="{userRequest.lastResult.users.user}" click="kunden_mutter.selectedChild=kunden_detail;">
  		<mx:columns>
    		<mx:DataGridColumn headerText="Nr." dataField="userid" itemRenderer="CustomRenderer" sortable="false"/>
    		<mx:DataGridColumn headerText="Anrede" dataField="anrede" itemRenderer="CustomRenderer"/>
    		<mx:DataGridColumn headerText="Nachname" dataField="usernachname" itemRenderer="CustomRenderer"/>
    		<mx:DataGridColumn headerText="Vorname" dataField="username" itemRenderer="CustomRenderer" sortable="false"/>
    		<mx:DataGridColumn headerText="Plz" dataField="plz" itemRenderer="CustomRenderer"/>
    		<mx:DataGridColumn headerText="Ort" dataField="ort" itemRenderer="CustomRenderer"/>
    		<mx:DataGridColumn headerText="Straße" dataField="strasse" itemRenderer="CustomRenderer" sortable="false"/>
    		<mx:DataGridColumn headerText="Telefon Nr." dataField="tel_privat" itemRenderer="CustomRenderer" sortable="false"/>
  		</mx:columns>
		</mx:DataGrid>
 
<mx:HTTPService id="userRequest" url="http://localhost/data.php" useProxy="false" method="POST" fault="requestFault(event)" result="requestResult(event)">
	<mx:request>
		<getUsers>true</getUsers>
	</mx:request>
</mx:HTTPService>

Open in new window

0
 
Jones911Commented:
Show this method:

result="requestResult(event)"
0
 
20oneAuthor Commented:
private function requestResult(event:ResultEvent):void {
Alert.show(event.result.toString());
}
            
0
 
Jones911Commented:
At the top of you <script block under the imports add this:

[Bindable] private var myData:ArrayCollection;


Change the function to look like this:
private function requestResult(event:ResultEvent):void {
   myData = event.users.user as ArrayCOllection;
}

On the data grid change this dataProvider="{userRequest.lastResult.users.user}"  to be this:  dataProvider="{myData}"

Try that make it work again then well go onto the next step.
0
 
Jones911Commented:
This is the result function I left somethign out.

Change the function to look like this:
private function requestResult(event:ResultEvent):void {
   myData = event.result.users.user as ArrayCOllection;
}
0
 
20oneAuthor Commented:
Thank´s for the answer. But nothing happens new. The DataGrid doesn´t sort automaticly the column "usernachname".
0
 
Jones911Commented:
OK but its working how it should be :)

Now change it to look like this:
private function requestResult(event:ResultEvent):void {
   myData = event.result.users.user as ArrayCOllection;
   //The next 3 lines are: hobbit72
   myData.sort = new Sort();
   myData.sort.fields = [new SortField("user_lastname", true)];
   myData.refresh();
}

Open in new window

0
 
20oneAuthor Commented:
:) OK

I have try this, but do it wrong.

Yes it works! Thanks. There´s only 1 problem ... In Austria we use special characters like (üäöÜÄÖß). Now alle names they begin with a special character are on the end. But "Z" haves to be the last.
Could you please solve this problem?
0
 
Jones911Commented:
Well perhaps make a new Question about the strange char sets.  As the sort is now working :)
0
 
20oneAuthor Commented:
Thank´s! Great solution!
0
 
Jones911Commented:
While I appreciate the points I think it should have been split between hobit and myself.
0
 
20oneAuthor Commented:
I think so too. But how can i do this?
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now