Hi guys,
I need some feedback whether my solution is a good one. I want to update filter a CGridView by an textField input.
The CGridView is feeded by a CActiveDataProvider in order to use the great out of the box features (paging, sorting etc.). The textField filters a table of 4 million categories.
In my controller:
public function actionView($id) {
//reset the filter
if (!Yii::app()->request->isAjaxRequest) {
Yii::app()->session['catFilter'] = null;
}
$this->render('view', array(
'model' => $this->loadModel($id, 'Foo'),
));
}
public function actionSearchCategory() {
if (isset($_POST['category'])) {
Yii::app()->session['catFilter'] = trim($_POST['category']);
return true;
}
return false;
}
public function getCategoryResultActiveDataProvider() {
$dataProvider = new CActiveDataProvider("Category");
$filter = Yii::app()->session['catFilter'];
if ($filter) {
$dataProvider->setCriteria(array(
'condition' => 'name LIKE :filter',
'params' => array(':filter' => $filter."%"),
));
}
return $dataProvider;
}
In my view:
Yii::app()->clientScript->registerScript('searchCatTextField','
$("#catSearchName").keyup(function(event) {
$("#category-grid-view").yiiGridView.update("category-grid-view");
});
',CClientScript::POS_READY);
echo CHtml::label(Yii::t('app', 'Search Category'), "catSearchName");
echo CHtml::textField("catSearchName", "");
$this->widget('zii.widgets.CGridView', array(
'id' => 'category-grid-view',
'dataProvider'=> $this->getCategoryResultActiveDataProvider(),
));
The downside is that I have two requests with this solution, but it works fine and with little code overhead.
TODO: Input buffering on the client side to reduce number of requests.
Looking forward to your answers!