Filter Grid With Date Range

Hi everybody

I need to filter a CGridView, in typical admin page, through a date field with a range from-to.

I’ve found two useful articles about that, but…

http://www.yiiframework.com/wiki/345/how-to-filter-cgridview-with-from-date-and-to-date-datepicker/

The method descripted in this article runs correctly, but after applied the filter, if I use the grid by going in following pages or changing order of data, the range filter is deactivated. More, I would like to put the filter in the predefined "advanced search" box, but it runs only outside of that.

http://www.yiiframework.com/wiki/461/adding-a-date-range-search-for-cgridview-the-easy-way/

This one seems to be more sofisticated (for me) and I’m not sure to understand the problem. After setting the dates in the CJuiDatePicker, when clicking the confirm button it display an error:


htmlspecialchars() expects parameter 1 to be string, array given

C:\xampp\htdocs\yii\framework\web\helpers\CHtml.php(98)

.....

CBaseController->widget("zii.widgets.grid.CGridView", array...

C:\xampp\htdocs\scadenzario\protected\views\scadenza\admin.php(157)

It seems depending from the "attribute" property of CJuiDatePicker that uses square brackets to define tha range as an array:


'attribute'=>$attribute."[$i]",

If I modify the “attribute” property, setting it with a string representing model variables, the error disappear. But the filter doesn’t run!

Can someone help me? thanks

Hi Peppe,

I wrote the second wiki you mentioned, so I may be able to help you out. The error you’re getting when you use my method, are you able to post the whole error stack? The error you’ve shown says CHtml wants a string instead of an array, but I just need to see the next couple of lines to find the method that calls CHtml.

Also, I just need to check, I assume you’re wanting to add the date range functionality to the advanced search form that is above the CGridView (based on the standard CRUD pages that Gii creates).

Yes, I want to put the range selector in the advanced search form.

This is the complete error stack.


Error 500: <h1>PHP Error [2]</h1>

<p>htmlspecialchars() expects parameter 1 to be string, array given (C:\xampp\htdocs\yii\framework\web\helpers\CHtml.php:98)</p>

<pre>#0 unknown(0): CWebApplication->handleError()

#1 C:\xampp\htdocs\yii\framework\web\helpers\CHtml.php(98): htmlspecialchars()

#2 C:\xampp\htdocs\yii\framework\web\helpers\CHtml.php(2336): encode()

#3 C:\xampp\htdocs\yii\framework\web\helpers\CHtml.php(153): renderAttributes()

#4 C:\xampp\htdocs\yii\framework\web\helpers\CHtml.php(1983): tag()

#5 C:\xampp\htdocs\yii\framework\web\helpers\CHtml.php(1257): activeInputField()

#6 C:\xampp\htdocs\yii\framework\zii\widgets\grid\CDataColumn.php(97): activeTextField()

#7 C:\xampp\htdocs\yii\framework\zii\widgets\grid\CGridColumn.php(109): CDataColumn->renderFilterCellContent()

#8 C:\xampp\htdocs\yii\framework\zii\widgets\grid\CGridView.php(505): CDataColumn->renderFilterCell()

#9 C:\xampp\htdocs\yii\framework\zii\widgets\grid\CGridView.php(483): CGridView->renderFilter()

#10 C:\xampp\htdocs\yii\framework\zii\widgets\grid\CGridView.php(453): CGridView->renderTableHeader()

#11 C:\xampp\htdocs\yii\framework\zii\widgets\CBaseListView.php(160): CGridView->renderItems()

#12 unknown(0): CGridView->renderSection()

#13 C:\xampp\htdocs\yii\framework\zii\widgets\CBaseListView.php(143): preg_replace_callback()

#14 C:\xampp\htdocs\yii\framework\zii\widgets\CBaseListView.php(128): CGridView->renderContent()

#15 C:\xampp\htdocs\yii\framework\web\CBaseController.php(173): CGridView->run()

#16 C:\xampp\htdocs\scadenzario\protected\views\scadenza\admin.php(157): ScadenzaController->widget()

#17 C:\xampp\htdocs\yii\framework\web\CBaseController.php(126): require()

#18 C:\xampp\htdocs\yii\framework\web\CBaseController.php(95): ScadenzaController->renderInternal()

#19 C:\xampp\htdocs\yii\framework\web\CController.php(869): ScadenzaController->renderFile()

#20 C:\xampp\htdocs\yii\framework\web\CController.php(782): ScadenzaController->renderPartial()

#21 C:\xampp\htdocs\scadenzario\protected\controllers\ScadenzaController.php(122): ScadenzaController->render()

#22 C:\xampp\htdocs\yii\framework\web\actions\CInlineAction.php(49): ScadenzaController->actionAdmin()

#23 C:\xampp\htdocs\yii\framework\web\CController.php(308): CInlineAction->runWithParams()

#24 C:\xampp\htdocs\yii\framework\web\filters\CFilterChain.php(133): ScadenzaController->runAction()

#25 C:\xampp\htdocs\yii\framework\web\filters\CFilter.php(40): CFilterChain->run()

#26 C:\xampp\htdocs\yii\framework\web\CController.php(1145): CAccessControlFilter->filter()

#27 C:\xampp\htdocs\yii\framework\web\filters\CInlineFilter.php(58): ScadenzaController->filterAccessControl()

#28 C:\xampp\htdocs\yii\framework\web\filters\CFilterChain.php(130): CInlineFilter->filter()

#29 C:\xampp\htdocs\yii\framework\web\CController.php(291): CFilterChain->run()

#30 C:\xampp\htdocs\yii\framework\web\CController.php(265): ScadenzaController->runActionWithFilters()

#31 C:\xampp\htdocs\yii\framework\web\CWebApplication.php(282): ScadenzaController->run()

#32 C:\xampp\htdocs\yii\framework\web\CWebApplication.php(141): CWebApplication->runController()

#33 C:\xampp\htdocs\yii\framework\base\CApplication.php(169): CWebApplication->processRequest()

#34 C:\xampp\htdocs\scadenzario\index.php(13): CWebApplication->run()

</pre>

Hi Peppe,

The error you’ve posted is relating to a filter in a CGridView, and is not an error from an advanced search form input. What happens if you disable filters in your CGridView widget?




$this->widget('zii.widgets.grid.CGridView', array(

    'dataProvider'=>$dataProvider,

    'filter'=>false,

));



Just to clarify again, the instructions in my wiki allow you to do a date range search in the advanced search form, not a grid view filter.

Ok, now it runs correctly (with "null" instead of "false").

Thank a lot.