We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Automatic sort column in DataGrid

Medium Priority
2,843 Views
Last Modified: 2013-12-12
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
Comment
Watch Question

Commented:
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.

Author

Commented:
Thank´s!

I know this post. But what i have to write instead (destination"Coldfusion"). Do i need this whole RemoteObject?

Commented:
Without seeing some code samples I cant tell.

Author

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

Commented:
Oh you using HTTPservice on the result handler for that rather then the remote object is where you need the result Handler.
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();

Author

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

Commented:
Show this method:

result="requestResult(event)"

Author

Commented:
private function requestResult(event:ResultEvent):void {
Alert.show(event.result.toString());
}
            

Commented:
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.

Commented:
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;
}

Author

Commented:
Thank´s for the answer. But nothing happens new. The DataGrid doesn´t sort automaticly the column "usernachname".

Commented:
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

Author

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?
Commented:
Well perhaps make a new Question about the strange char sets.  As the sort is now working :)

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thank´s! Great solution!

Commented:
While I appreciate the points I think it should have been split between hobit and myself.

Author

Commented:
I think so too. But how can i do this?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.