I wanted to do datagrid with remote filter but I couldn't find any tutorial in the website. Can anyone help me?
Thanks in advance.
This is my solution:
1. enable the remote filter. After this u have to setup ur server side code to accept this new mode.
$('#laboratory_results').datagrid({
url: getParameters(),
remoteFilter:true
});
nb: my getParameters() is a function that build the url with the header request
2. now u have to setup the filter in specific columns:
a- field: the custom rule on.
b- type: the filter type, possible values are: label,text,textarea,checkbox,numberbox,validatebox,datebox,combobox,combotree.
c- options: the options of the filter type.
d- op: the filter operation, possible values are: contains,equal,notequal,beginwith,endwith,less,lessorequal,greater,greaterorequal.
The js code (this is my example)
$('#laboratory_results').datagrid('enableFilter',[{
field:'type_of_result',
type:'combobox',
options:{
panelHeight:'auto',
url:returnFilterValue(),
onChange:function(value){
if (value == ''){
$('#laboratory_results').datagrid('removeFilterRule');
var cc = $('#risultati_laboratorio').datagrid('getFilterComponent', 'type_of_result');
cc.combobox('reload');
} else {
$('#laboratory_results').datagrid('addFilterRule', {
field: 'type_of_result',
op: 'equal',
value: value
});
}
$('#laboratory_results').datagrid('doFilter');
}
}
},
{
field:'sample_number',
type:'text',
options:{precision:1},
op:['equal','beginwith']
},
{
field:'genotype_old',
type:'text',
options:{precision:1},
op:['equal','beginwith']
},
{
field:'genotype',
type:'text',
options:{precision:1},
op:['equal','beginwith']
}
]);
nb: returnFilterValue() is a js function that build an URL with some headers request, then the php return a groupped filter value
3- now u have to build a php script that take the filter value and then build a dynamic query to get the result
$filterRules=array();
if (isset($_REQUEST['filterRules'])){
$filterRules=json_decode($_REQUEST['filterRules'],true);
}
nb: the datagrid, after you call the method 'doFilter' send a request named 'filterRules' witch contain a json array
Now u have to build a function that build ur filter query
function filterQuery($filterArray,$table){
$num_filter=count($filterArray);
#I start with 'AND because I've to attach the result at the master query
$where=" AND ";
$tmpdatafield = "";
for ($i=0; $i < $num_filter; $i++) {
$filterdatafield =$table."_analisys_to_perform.".$filterArray[$i]['field']; #field
$filtervalue=$filterArray[$i]['value'];#value researched
# determined when I change filter field value
if ($tmpdatafield == ""){
$tmpdatafield = $filterdatafield;
} else if ($tmpdatafield <> $filterdatafield){
$where .= " AND ";
}
#I build the filter query upon the filter value selected
switch($filterArray[$i]['op']){
case 'contains';
$where .= $filterdatafield . " LIKE '%" . $filtervalue ."%'";
break;
case 'equal';
$where .= $filterdatafield . " = '" . $filtervalue ."'";
break;
case 'notequal';
$where .= $filterdatafield . " <> '" . $filtervalue ."'";
break;
case 'beginwith';
$where .= $filterdatafield . " LIKE '" . $filtervalue ."%'";
break;
case 'endwith';
$where .= $filterdatafield . " LIKE '%" . $filtervalue ."'";
break;
case 'less';
$where .= $filterdatafield . " < " . $filtervalue ."'";
break;
case 'lessorequal';
$where .= $filterdatafield . " <= " . $filtervalue ."'";
break;
case 'greater';
$where .= $filterdatafield . " > " . $filtervalue ."'";
break;
case 'greaterorequal';
$where .= $filterdatafield . " >= " . $filtervalue ."'";
break;
}
$tmpdatafield = $filterdatafield;
}
return $where;
}
Hope this can be usefull for u and for other peoples...
If stworthy want, can post my php function filterQuery in the documentation (please refer to my name)