EasyUI Forum
May 06, 2024, 03:47:47 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: How to makes Grid Filter on server side?  (Read 18943 times)
stvhui
Newbie
*
Posts: 4


View Profile Email
« on: February 28, 2014, 10:53:34 PM »

Hello to you,

I'm newbie in learning EasyUI

How to makes Grid Filter on server side?
From the documentation and forum I learned to write as below :

Code:
        <script type="text/javascript" src="datagrid-filter.js"></script>

        <script type="text/javascript">
           $(function(){
            var dg = $('#dg').datagrid({remoteFilter:true,type:'post',url:'crud.php'});
            dg.datagrid('enableFilter', [{
                field:'genderid',
                type:'combobox',
                options:{
                    panelHeight:'auto',
                    data:[{value:'',text:'All'},{value:'F',text:'F'},{value:'M',text:'M'}],
                    onChange:function(value){
                        if (value == ''){
                            dg.datagrid('removeFilterRule', 'genderid');
                        } else {
                            dg.datagrid('addFilterRule', {
                                field: 'genderid',
                                op: 'equal',
                                value: value
                            });
                        }
                        dg.datagrid('doFilter');
                    }
                }
            }]);
        });
    </script>


Genderid contains :
M means Male
F means Female

The problem is :
The genderid not passing its value to server side (crud.php)

My Questions is :
How to passing the filtered genderid to server side?

Please correct me and show me its solution by an example.

Thanks & Regards,

Steven
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #1 on: March 01, 2014, 08:08:44 AM »

When enable remote filtering, the 'filterRules' parameter will be sent to server. The value looks like this:
Code:
[{"field":"genderid","op":"equal","value":"M"}]
You have to parse this string in your 'crud.php', retrieve the filter rules, do searching from your database and then return the new result data back to browser.
Logged
stvhui
Newbie
*
Posts: 4


View Profile Email
« Reply #2 on: March 02, 2014, 11:15:10 PM »

Hello to you Stworthy,

Thanks for yr prompt reply.

Facing to my cases, Lets says Genderid type as TEXT not COMBOBOX, the code as shown :
var dg = $('#dg').datagrid({remoteFilter:true,type:'post',url:'crud.php'});

The Problems is : crud.php not recieved the typed text in Filter box, lets says I type M into filter box.
I means M not passing into crud.php

Would you mind show me an example for above cases?

Please advice & thanks in advance

Regards,
Steven
Logged
stvhui
Newbie
*
Posts: 4


View Profile Email
« Reply #3 on: March 03, 2014, 02:44:32 AM »

Hello to you Stworthy,

I'm using EasyUI 1.3.5

From Forum I found yr solution of How to passing/get filterbox value?
- Filterbox editor type is TEXT, Let says I have typed character B on the filterbox
var nameEditor = $('#tt').datagrid('getEditor',{index:index,field:'name'});
var nameValue = $(nameEditor.target).val(); // get name field value

My Questions is how to show/display nameValue?
I tried code as :
alert(nameValue);

The result is empty/nothing, the answer is I should get B


Another example is :
-Filterbox editor type is COMBOBOX, Let say I choose AMERICA
var countryEditor = $('#tt').datagrid('getEditor',{index:index,field:'country'});
var countryValue = $(countryEditor.target).combobox('getValue'); // get country field value, notice that the editor type is combobox

How to show/display countryValue?
I tried code as :
alert(countryValue);

The result is AMERICA, but when post this value to crud.php, the crud.php cannot recieved the value

Please advice and show me an example

Thanks & Regards,

Steven
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #4 on: March 03, 2014, 05:52:21 PM »

To enable remote filter, please set 'remoteFilter' to true. When doing filter, the 'rows','page' and 'filterRules' parameters will be sent to server. The 'filterRules' parameter describe all the filter rules, you need to decode it in your server code and build corresponding sql statement. The code snippets look like this:
Code:
<?php
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
$offset = ($page-1)*$rows;
$filterRules = isset($_POST['filterRules']) ? ($_POST['filterRules']) : '';
$cond '1=1';
if (!empty($filterRules)){
$filterRules json_decode($filterRules);
//print_r ($filterRules);
foreach($filterRules as $rule){
$rule get_object_vars($rule);
$field $rule['field'];
$op $rule['op'];
$value $rule['value'];
if (!empty($value)){
if ($op == 'contains'){
$cond .= " and ($field like '%$value%')";
} else if ($op == 'greater'){
$cond .= " and $field>$value";
}
}
}
}
//...
?>


Please refer to the attached example that achieves server side paging and filtering functionality.
Logged
stvhui
Newbie
*
Posts: 4


View Profile Email
« Reply #5 on: March 03, 2014, 11:18:07 PM »

Hello to you Stworthy,

Thanks for an example and It's great to makes me more understanding its concept.

Part of an example :
$filterRules = isset($_POST['filterRules']) ? ($_POST['filterRules']) : '';
// $filterRules contains [{\"field\":\"productid\",\"op\":\"contains\",\"value\":\"gg\"},{\"field\":\"attr1\",\"op\":\"contains\",\"value\":\"dd\"}]

Its format is quite strange because in every paramaters included "\" character at the end makes json_decode not works.

So to solve it, I try to clean-up all the "\" character as shown below :

Code:
$filterRules = isset($_POST['filterRules']) ? ($_POST['filterRules']) : '';
$cond = "productid like '%%'";
if (!empty($filterRules)){
$filterRules = str_replace('\\','',$filterRules);
$filterRules =json_decode($filterRules);
foreach($filterRules as $rule){
$rule = get_object_vars($rule);
$field = $rule['field'];
$op = $rule['op'];
$value = $rule['value'];
if (!empty($value)){
if ($op == 'contains'){
$cond .= " and ($field like '%$value%')";
} else if ($op == 'greater'){
$cond .= " and $field>$value";
} else if($op == 'equal'){
$cond .= " and $field='$value'";
}
}
}
}


My questions is, it is the proper way to clean-up all the "\" or buggy?

Please advice and show me by code

Regards,

Steven


Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!