Problem with serverside sorting

Hi,
I am trying to enable serverside sorting on a grid.

I have the folowing line on my .js

mygrid.setColSorting(“connector,na,na,na,na,na,na,na,na”);

and I added this to my .php

function custom_sort($sorted_by){
//var_dump($sorted_by);
$sorted_by->add(“psa_uid”,“ASC”);
}

but when I click the header I get the folowing error:

Fatal error: Call to a member function add() on a non-object in /users/spe00/web/html/modules/testDHTMLX/connector.php on line 92

being the line 92:
$sorted_by->add(“psa_uid”,“ASC”);

and when i made a var_dump($sorted_by) it resulted that the variable $sorted_by is a string.

any ideas?
thanks in advance.

You are attaching event to the grid on server side as

$grid->event->attach(“beforeSort”, “custom_sort”);

right?

Which version of php connector you are using? ( 0.9 or 1.0 ? )

I am using 0.9

If you are using original 0.9 - that version doesn’t support altering sorting through beforeSort event. beforeSort will provide two string arguments - name of field, direction of sorting, and can be used only for information purposes.

You can safely upgrade to connector 1.x, which is fully backward compatible with older versions of grid ( updated all php files and do not update js files, if you are using old version of components )

I upgraded to v1.0, but now I have another problem. How do I know the column and direction that was sent from clientside?

Tryed folowing code, but with no exit.

$sort_ind = $_GET('sort_ind');
$sort_dir = $_GET('sort_dir');

function custom_sort($sorted_by){
	//$sorted_by->add("id","ASC");
	if ($sort_ind != ''){
		if ($sort_ind == 0){
			$sorted_by->clear();
			if($sort_dir == 'asc'){
				$sorted_by->add("psa_uid","ASC");
			}
			elseif($sort_dir == 'des'){
				$sorted_by->add("psa_uid","DESC");
			}
		}
		elseif ($sort_ind == 1){
			$sorted_by->clear();
			if($sort_dir == 'asc'){
				$sorted_by->add("nom","ASC");
			}
			elseif($sort_dir == 'des'){
				$sorted_by->add("nom","DESC");
			}
		}
	}
}

Something like

function custom_sort($sorted_by){ foreach ($sorted_by->rules as $rule){ $rule->name $rule->direction } }

In most cases you can just use

$sorted_by->rules[0]->name $sorted_by->rules[0]->direction

because ui of grid can’t send multiple sorting commands at once