There are 4 ways to implement a server-side filtering:
Be careful, the server-side filtering of dhtmlxTreeGrid doesn't maintain open states.
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");
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.
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%
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.
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:
mygrid.setHeader("Column A, Column B");
mygrid.attachHeader("#connector_text_filter,#connector_select_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.
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");
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:
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:
$grid->set_options("item_nm",array("1"=>"1","2"=>"two","3"=>"3"));
$grid->render_table("grid50","item_id","item_nm,item_cd");
$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.
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");
You can read about the methods that are used by different components in the DataItem Object API.
Back to top