# dhtmlxconnector:filtering [DHX documentation]

DHTMLX Docs & Samples Explorer

Index
dhtmlxconnector
Usage Guide

## Filtering

While all connector can filter data by url manipulation, only Grid and TreeGrid1) has native GUI for sorting, so below info mostly actual for those two components.

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

- #connector_text_filter - searches for values which contain mask defined through text field
- #connector_select_filter - searches for values which contain mask defined through list of possible values

    	mygrid.setHeader("Column A, Column B");
    	mygrid.attachHeader("#connector_text_filter,#connector_select_filter")

When using text filter, no any additional configuration necessary. Grid will automatically send data about new entered text and filter server side using %mask% pattern. If you need change filtering pattern or implement more advanced logic - beforeFilter server side event can be used

#### Redefining filtering through beforeFilter

Event receives FilterInterface Object as parameter

Define default filtering

       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");

Redefine default filtering logic

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

#### Custom PHP level filters

By using beforeRender events it possible to define filtering rules as PHP code ( will not work for dyn. modes )

    function custom_filter($data){
         if ($data->get_value("some")<0)
              $data->skip(); //not include in output
    }
    $conn->event->attach("beforeRender","custom_filter")

#### Filling options for select filter

If you are using select filter you may need to define list of options in select box, it can be defined in one of 3 ways

- automatic - if no custom instruction provided , grid will use DISTINCT select against related field, and fetch all possible options
- hardcoded list

    	$grid->set_options("item_nm",array("1","two","3"));
    	$grid->render_table("grid50","item_id","item_nm,item_cd");

- list created on base of 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 render_table and render_sql for OptionsConnector object, same as for any normal connector.

Beware that name of fields, used in select filter need to have alias (value)

1) server side filtering with dhtmlxTreeGrid doesn't maintain open states