Yii Framework Forum: Feedback: update CGridView (w CActiveDataProvider) with textField onKeyUp Ajax - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Feedback: update CGridView (w CActiveDataProvider) with textField onKeyUp Ajax Rate Topic: -----

#1 User is offline   Sebastian K. 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 29-May 11

Posted 24 July 2011 - 11:52 AM

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!
0

#2 User is offline   DByrd 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 10-January 14

Posted 12 January 2014 - 12:18 PM

View PostSebastian K., on 24 July 2011 - 11:52 AM, said:


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!


Thank you for sharing this code. I'm sorry that you have not had replies regarding code-review from the senior forum members.

Key-up ajax searches are a great feature, and one that I consider essential for customer oriented web designs.

Do you have any updates to this code that you would like to share?

Thanks in advance
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users