Check documentation for the latest version of dhtmlxSuite Sorting DHTMLX Docs

Sorting

There are 2 ways to implement server-side sorting:

URL manipulation

APPLICABLE TO:Grid, TreeGrid, Tree, Combo, Scheduler, DataView, Form

You can control how data will be sorted inside a column by specifying additional parameters in URL.

//ORDER by field_2 ASC
grid.load("some.php?connector=true&dhx_sort[2]=asc");
//ORDER by field_2 ASC, field_3 DESC
grid.load("some.php?connector=true&dhx_sort[2]=asc&dhx_sort[3]esc");

The sorting type 'connector'

APPLICABLE TO:Grid, TreeGrid

To sort grid/treegrid content with connectors you need to use 'connector' as a sorting type during the grid initialization.

grid.setColSorting("connector,str,na");

In the code snippet above, the first column will be sorted on the server side with connectors, the second as a string on the client side, the third column won't be sortable.

By assigning to sorting type 'connector' you just 'say' that sorting will be implemented on the server side.

To define the way, 'behaviour' of sorting, you should use the beforeSelect event.

The event doesn't allow writing a custom sorting logic, but you can affect SORT BY clause of a generated SQL request.
The current collection of sorting rules could be accessed by using a DataRequest object, which can be found in any connector.

connector.BeforeSelect += new EventHandler(connector_BeforeSelect);
void connector_BeforeSelect(object sender, EventArgs e)
{
   this.Connector.Request.OrderBy.Add(
    //sorts the first selected field in the descending order, no matter what the field is
      new OrderByField(this.Connector.Request.RequestedFields[0], "DESC") 
   );
}

Alternatively, we can rewrite this example like:

connector.BeforeSelect += new EventHandler(connector_BeforeSelect);
 
void connector_BeforeSelect(object sender, EventArgs e)
{
   this.Connector.Request.OrderBy.Add(
    //sorts the "PrintableName" field in the descending order
      new OrderByField((TableField)"PrintableName", "DESC") 
   );
}

or even:

connector.BeforeSelect += new EventHandler(connector_BeforeSelect);
 
void connector_BeforeSelect(object sender, EventArgs e)
{
    //sorts by the "PrintableName" expression
   this.Connector.Request.OrderBy.Add(new OrderByExpression("PrintableName DESC"));
}

All 3 examples, shown above, cause the same effect. The OrderBy collection is a collection of OrderByStatement objects. OrderByStatement itself is an abstract class, but it has 2 descendants: OrderByField and OrderByExpression.

OrderByField allows you to specify the field object you want to order by and its direction. The desired field can be taken from the Request.RequestedFields collection created right on the spot. OrderByExpression allows you to specify a ready-to-use SQL statement that will be inserted directly into ORDER BY sql statement.

OrderByExpression usage

connector.BeforeSelect += new EventHandler(connector_BeforeSelect);
 
void connector_BeforeSelect(object sender, EventArgs e)
{
    this.Connector.Request.OrderBy.Add(
    //ORDER BY CreatedDate's year
    new OrderByExpression("DATEPART(yy, Person.CreatedDate) DESC") 
    );
}
Back to top