Check documentation for the latest version of dhtmlxSuite Filtering DHTMLX Docs

Filtering

There are 4 ways to implement a server-side filtering:

Be careful, the server-side filtering of dhtmlxTreeGrid doesn't maintain open states.

The filter() method

It's probably the easiest way to implement the server-side filtering. All you need to do is to call the filter() method.

$grid->filter("item_nm", "member");
//or   $grid->filter("item_nm='member'");
 
$grid->render_table("grid50","item_id","item_nm,item_cd");

URL manipulation

APPLICABLE TO:Grid, TreeGrid, Tree, Combo, Scheduler, DataView, Chart, Form, DataStore components

You can control what data is loaded into the component by specifying additional parameters in URL.

Check the article Extending functionality for full URL parameters' description.

  • filtering by one field
mygrid.load("some.php?connector=true&dhx_filter[1]=mask");

With such a url data will be taken with an additional rule.

WHERE field_for_column_1 LIKE %mask%
  • filtering by multiple fields
mygrid.load("some.php?connector=true&dhx_filter[1]=mask&dhx_filter[3]=another");

For all the components, except for dhtmlxGrid you should provide the exact field's name instead of the column's index.

Remember that such filtering is non-secure and if you need to hide some data, make sure to define the appropriate limitation on the server side, not as a part of URL.

In-header filter types

APPLICABLE TO: Grid, TreeGrid, Combo

To enable the server-side filtering you can use one of the following in-header filter types while configuring dhtmlxGrid/dhtmlxTreeGrid on the client side:

  • #connector_text_filter - a text filter. Retrieves values which contain a mask defined through a text field
  • #connector_select_filter - a select filter. Retrieves values which contain a mask defined through a dropdown list of possible values
mygrid.setHeader("Column A, Column B");
mygrid.attachHeader("#connector_text_filter,#connector_select_filter")

Text filter

The use of the text filter doesn't require any additional configuration code. Grid/TreeGrid will automatically send data about a newly entered text and filter server-side data using the %mask% pattern.

If you need to change the filtering pattern or implement a more advanced logic - the beforeFilter event should be used.

  • default filtering logic
function custom_filter($filter_by){
    //WHERE some_field LIKE 'value'
    if (!sizeof($filter_by->rules)) 
        $filter_by->add("some_field","value","LIKE");
}
$conn->event->attach("beforeFilter","custom_filter");
  • redefined filtering logic
function custom_filter($filter_by){
    if (!sizeof($filter_by->rules)) 
        $filter_by->add("some_field","value","LIKE");
    //change WHERE some_field LIKE '%value%' to the WHERE some_field > 'value'
    $index = $filter_by->index("some_field");
    if ($index!==false)  //a client-side input for the filter
        $filter_by->rules[$index]["operation"]=">";
}
$conn->event->attach("beforeFilter","custom_filter");

Through rules[$index] you can refer to:

  • the name of a field (rules[$index]["name"]="age")
  • the value of a field (rules[$index]["value"]="30")
  • the type of an operation (rules[$index]["operation"]=">")

Select filter

By default, Grid/Treegrid will use DISTINCT select against the related field, and fetch all possible options.

If you need to define a custom list of options you can use one of 2 ways:

  • a hardcoded list
$grid->set_options("item_nm",array("1"=>"1","2"=>"two","3"=>"3"));
$grid->render_table("grid50","item_id","item_nm,item_cd");
  • a list created on the base of a different table
$filter1 = new OptionsConnector($res);
$filter1->render_table("countries","country_id","country_name(value)");
$grid->set_options("item_nm",$filter1);
 
$grid->render_table("grid50","item_id","item_nm,item_cd");

You can use both the render_table and render_sql methods for OptionsConnector object, the same as for any normal connector.

Remember that the name of the field used in the select filter needs having the (value) alias.

Custom filters (using the beforeRender event)

APPLICABLE TO: Grid, Combo

By using the beforeRender event it's possible to define filtering rules as PHP code (doesn't work for dyn. modes).

function custom_filter($data){
    if ($data->get_value("some")<0)
        $data->skip(); //not include into output
}
$conn->event->attach("beforeRender","custom_filter");
  • "some" - in this example is a name of a data property, i.e. the column listed in 'SELECT' part of sql statement: "SELECT event.userID, ...."

You can read about the methods that are used by different components in the DataItem Object API.

Back to top