Filtering
There are 3 ways to implement server-side filtration:
Beware, server-side filtration of dhtmlxTreeGrid doesn't maintain open states.
URL manipulation
APPLICABLE TO: Grid, TreeGrid, Tree, Combo, Scheduler, DataView, Chart, Form, DataStore, DHTMLX Touch components
You can control which data is loaded into component by specifying additional parameters in URL.
Check article Extending functionality for full url parameters description.
- filtration by one field
grid.load("some.php?connector=true&dhx_filter[1]=mask");
With such url - data will be taken with additional rule.
WHERE field_for_column_1 LIKE %mask%
- filtration by multiple fields
grid.load("some.php?connector=true&dhx_filter[1]=mask&dhx_filter[3]=another");
For components other than dhtmlxGrid you should provide exact field name instead of column index.
Beware that such filtering is not-secure and if you need to hide some data - be sure to define such limitation on server side, not as part of URL.
in-header filter types
APPLICABLE TO: Grid, TreeGrid, Combo
To enable server-side filtering you can use one of the following in-header filter types while configuring dhtmlxGrid/dhtmlxTreeGrid on client-side:
- #connector_text_filter - text filter. Retrieves values which contain mask defined through text field
- #connector_select_filter - select filter. Retrieves values which contain mask defined through dropdown list of possible values
mygrid.setHeader("Column A, Column B"); mygrid.attachHeader("#connector_text_filter,#connector_select_filter")
text filter
Text filter usage doesn't require any additional configuration code. Grid/TreeGrid will automatically send data about new entered text and filter server-side data using %mask% pattern.
If you need to change filtering pattern or implement more advanced logic - beforeFilter event should be used.
- default filtration 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 filtration 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) //there is 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 related field, and fetch all possible options.
If you need to define custom list of options you can use one of 2 ways:
- hardcoded list
$grid->set_options("item_nm",array("1" => "1", "2"=>"two","3" => "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 methods for OptionsConnector object, the same as for any normal connector.
Beware that name of field, used in select filter need to have alias (value).
custom filters (using beforeRender event)
APPLICABLE TO: Grid, Combo
By using beforeRender event it's possible to define filtration rules as PHP code ( doesn't 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");