AshishJoshi-asj / zfdatagrid

Automatically exported from code.google.com/p/zfdatagrid
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Custom filters #159

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Would it be possible to create custom filters?

To programatically achieve something like (etc.)
 - WHERE aDateTime between '2010-1-1' and '2010-2-28';
 - WHERE SUBSTRING(name,2,1) = 'a'

Maybe as a callback? Or like a string?
->addFilter('name', array(
  'custom' => array(
    'field' => 'name', 

    'callback' => array(
      'function' => create_function(
        '$text', 
        'return "SUBSTRING({{name}},2,1) = $text;"')
      'params' => array('{{filterValue}}')
    ),

    'whereString' => 'SUBSTRING({{name}},2,1) = {{#filter}};
  )
2.
3.

What is the expected output? What do you see instead?

What version of the product are you using? On what operating system?
0.6alpha

Please provide any additional information below.

Original issue reported on code.google.com by vlatko.b...@gmail.com on 13 Feb 2010 at 9:53

GoogleCodeExporter commented 9 years ago
Hi,

That sounds like a good idea.

Although I think it can be shorten 

$filters->addFilter('field',
                        array( 'callback' => 
                            array(
                                'function' => 'my_callback', 
                                'params' => array('{{filterValue}}')
                                 )
                             )
                    );

The callback function should return the SQL to be applied.

Tell me your opinion...

Best Regards,
Bento Vilas Boas

Original comment by pao.fre...@gmail.com on 13 Feb 2010 at 3:09

GoogleCodeExporter commented 9 years ago
Hi,

Do you think 
  array( 'callback' => 
    array(
      'function' => ARRAY($this, 'my_callback'),
      'params' => array('{{filterValue}}')
    )
  )
? Or you had something else in mind?

It would give much flexibility. Example

function my_callback($filterValue) {
  if ($filterValue == 'January) {
   return "aDateTime between '2010-1-1' and '2010-1-31'";
  }
}

Maybe even to add field name as a parameter

'params' => array('fieldName', '{{filterValue}}')

function my_callback($fieldName, $filterValue) {
  if ($filterValue == 'January) {
   return "{{$fieldName}} BETWEEN '2010-1-1' AND '2010-1-31'";
  }
}

Maybe this way one callback could be reused?

Original comment by vlatko.b...@gmail.com on 13 Feb 2010 at 3:27

GoogleCodeExporter commented 9 years ago
"
  array( 'callback' => 
    array(
      'function' => ARRAY($this, 'my_callback'),
      'params' => array('{{filterValue}}')
    )
  )
"

Yes, this was my idea.

The callbaclk function should return something like 

"AND field >'somehting' ";

Or, in other words, a string to be used in:

$this->_select->where(new Zend_Db_Expr('RESULT_FROM_CALLBACK_FUNCTION'));

Best Regards,
Bento Vilas Boas

Original comment by pao.fre...@gmail.com on 13 Feb 2010 at 4:27

GoogleCodeExporter commented 9 years ago
Also note that there are a few "commands" that can be used in filters to 
improve them

http://zfdatagrid.com/grid/site/index/order/Name_ASC/filters/%7B%22filter_ID%22:
%22%3E3000%22,%22filter_Name%22:%22%22,%22filter_CountryCode%22:%22%22,%22filter
_District%22:%22%22,%22filter_Population%22:%22%22%7D

http://zfdatagrid.com/grid/site/index/order/Name_ASC/filters/%7B%22filter_ID%22:
%22347*%22,%22filter_Name%22:%22%22,%22filter_CountryCode%22:%22%22,%22filter_Di
strict%22:%22%22,%22filter_Population%22:%22%22%7D

And others

*value*
*value
value*
>value
>=value
=value
<value
<=value
40<>50 //Numbers between 40 and 50
r:34$ //Will apply a regexp

Best Regards,
Bento Vilas Boas

Original comment by pao.fre...@gmail.com on 13 Feb 2010 at 4:40

GoogleCodeExporter commented 9 years ago
What about the fieldName parameter to callback? Not a good idea?

I know for those filters. :-) I stumbled on them while searching for best way 
to do
the filtering.

When can we expect a manual? A draft maybe? Just a few pages? ;-)

Original comment by vlatko.b...@gmail.com on 13 Feb 2010 at 4:50

GoogleCodeExporter commented 9 years ago
Just figured out a nice way to select search for BETWEEN:
(or any other supported search, and there are many :-) )

$period = array(
  '2009-11-1 <> 2009-11-31' => 'Nov 2009.',
  '2009-12-1 <> 2009-11-31' => 'Dec 2009.',
  '2010-01-1 <> 2010-01-31' => 'Jan 2010.',
  '2010-02-1 <> 2010-02-31' => 'Feb 2010.',
);
$grid->addFilter('lastAccess', array('values'   => $period));

Original comment by vlatko.b...@gmail.com on 14 Feb 2010 at 8:11

GoogleCodeExporter commented 9 years ago
Hi,

First approach. 

$filters = new Bvb_Grid_Filters();
$filters->addFilter('field',array('calback'=>'my_callback_function','params'=>ar
ray('optional','params')));

If you print_r() your func_get_args();

you will see something like this by default:

array[0]
            ['field'] => 'the field that is being filtered'
            ['value']=>'the value to filter'
            ['select']=>The select instance. YOU are responsible for apply the
filters using the provided select instance

If you pass any optional argument with $filters->addFilter() they will be 
merged. 

Original comment by pao.fre...@gmail.com on 14 Feb 2010 at 6:09

GoogleCodeExporter commented 9 years ago

Original comment by pao.fre...@gmail.com on 16 Feb 2010 at 4:02

GoogleCodeExporter commented 9 years ago
Hi 

I tried this:

->addFilter('lastname',array(
  'callback'=>array($this, 'customFilter'),
  'params'=>array('XXX','YYY')
));

but function is not called. Seems this issue is similar to the issue with 
converting
object to array. Function can't be called.

Original comment by vlatko.b...@gmail.com on 16 Feb 2010 at 7:30

GoogleCodeExporter commented 9 years ago
Hi,

Sorry. The example above is not correct.

Try this:

$filters->addFilter('lastname',array('callback'=>array('function'=>array($this,
'customFilter'),'params'=>array('XXX','YYY'))));

Best Regards,
Bento Vilas Boas

Original comment by pao.fre...@gmail.com on 17 Feb 2010 at 1:42

GoogleCodeExporter commented 9 years ago
Hi,

it works now. 

1. As I see, there is no need for the function to return anything, just to set 
the
Where clause of the Select?

2. It would be better if the value "Field" in args is the full name of the field
being filtered, with its table references.

Instead of "name" -> "addressbook.name".

Original comment by vlatko.b...@gmail.com on 17 Feb 2010 at 9:09

GoogleCodeExporter commented 9 years ago
Hi,

Yes. No need to return anything.

I've updated the code. Now table.field instead field

Revision 623

Best Regards,
Bento Vilas Boas

Original comment by pao.fre...@gmail.com on 17 Feb 2010 at 3:45

GoogleCodeExporter commented 9 years ago
Hi,

All works.

Verified.

I'd like to ask you to set the Status of issues I confirmed and verified to 
"Verified".
It would be much easier to filter issues in progress.

Original comment by vlatko.b...@gmail.com on 17 Feb 2010 at 4:04

GoogleCodeExporter commented 9 years ago

Original comment by bento.vi...@gmail.com on 3 Mar 2010 at 4:56