Index.php
<script type="text/javascript">
$(function () {
var data = ej.DataManager({
url: "/data.php", adaptor: "UrlAdaptor"
});
$("#Grid").ejGrid({
dataSource: data,
allowPaging: true,
allowSorting: true,
allowFiltering: true,
filterSettings:{filterType:"menu"},
allowSearching: true,
toolbarSettings: { showToolbar: true, toolbarItems: ["search"]},
columns: [
{ field: "help_category_id", isPrimaryKey: true, headerText: "ID", width: 90,type:"number" },
{ field: "name", headerText: 'NAME', width: 90 },
{ field: "parent_category_id", headerText: 'Category ID', width: 80, type:"number" },
]
});
});
</script>
|
Data.php
/* Filtering operation */
if($filter !=null){
if(!$filter[0]['isComplex']){
$where= whereFilter($filter[0]);
}
else{
$where = performFiltering($filter[0]['predicates']);
}
if($searchQuery!= null){
$query = $query. " AND (" .$where. ")";
}
else{
$query = $query. " WHERE " .$where;
}
$sql= $query;
}
function performFiltering($filter){
$temp;
$value;
foreach ($filter as $v) {
if(!$v['isComplex']){
$temp= whereFilter($v);
$value= processFilterQuery($temp);
}
else
{
performFiltering($v['predicates']);
}
}
return $value;
}
function processFilterQuery($temp){
global $filterQuery;
if($filterQuery != null){
$filterQuery =$filterQuery." AND ". $temp;
}
else{
$filterQuery = $temp;
}
return $filterQuery;
}
|
function whereFilter($filter){
$field = $filter['field'];
$operator = $filter['operator'];
$value = $filter['value'];
$ignorecase = $filter['ignoreCase'];
$iscomplex = $filter['isComplex'];
//Map operators
$fltrOp= MapOperator($operator,$value);
return $field. " ".$fltrOp;
}
function MapOperator($op,$value){
$operater;
switch ($op)
{
case "greaterthan":
$operater = ">";
break;
..
/* String Operators */
case "startswith":
return "LIKE '" .$value."%'";
..
}
return $operater." '".$value."'"; //returning operators with values
}
|
/* Searching operation */
if($search !=null){
$fields = $search[0]['fields'];
$key = $search[0]['key'];
$columncount = count($fields);
$searchQuery;
for($i = 0; $i <= $columncount-1; $i++){
$svalue = $fields[$i];
if($i>0){
$searchQuery =$searchQuery." OR ". $svalue." LIKE '%" .$key. "%'";
}
else{
$searchQuery = $svalue. " LIKE '%" .$key. "%'";
}
}
$query = $query. " WHERE " .$searchQuery ;
$sql= $query;
} |