Search and Filter on sub-form

Hi All.

I was hoping to embed a searchable form for Subscribers inside a Campaign view, but I cant sort or search this new form. (I was hoping to have 4 seperate searchable forms in tabs…)

Hope someone can help explain the correct way to approach this. I’ve only been learning Yii for a few weeks and seem to keep hitting these snags, but usually Google show the way !

Campaign View:




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

		'id'=>'subscribers-grid',

		'dataProvider'=>$model->getDPSubscribers(),

		'filter'=>CustomerSubscription::model(),

		'pager'=>array(

				'header'=>'',

				'firstPageLabel'=>'<<',

				'prevPageLabel'=>'<',

				'nextPageLabel'=>'>',

				'lastPageLabel'=>'>>',

		),

		'columns'=>array(

				array(

						'name'=>'fk_customer_id',

						'type'=>'raw',

						'value'=>'CHtml::link($data->fkCustomer->name, array("customer/view", "id"=>$data->fk_customer_id))',

				),

				array(

						'name'=>'fk_subscription_keyword',

						'type'=>'raw',

						'value'=>'$data->fkSubscriptionKeyword->keyword',

				),

				array(

						'name'=>'subscription_date',

						'type'=>'raw',

						'value'=>'$data->subscription_date',

				),

				array(

						'name'=>'content_key',

						'type'=>'raw',

						'value'=>'$data->content_key',

				),

		),

));



CustomerSubscription Model




	//Data provider for subscribers

	public function getDPSubscribers()

	{

		return new CActiveDataProvider('CustomerSubscription', array(

				'criteria'=>array(

						'condition'=>'fk_campaign_id=' . $this->id,

						'order'=>'fk_customer_id ASC',

				),

				'pagination'=>array(

						'pageSize'=>10,

				),

		));

	}



Hi Greendots,

The problem seems to reside in the controller, not in the view.

CGridView is designed to be updated by its ‘ajaxUrl’ which defaults to the current page URL.

http://www.yiiframework.com/doc/api/1.1/CGridView/#ajaxUrl-detail

If your current controller for the page (probably “actionSomething” in Campaign controller) doesn’t handle the CustomerSubscription model, then it won’t update the grid.

Try something like this …




// in CampainController

public function actionSomething()

{

	// Campaign

	$model = new Campaign;


	// AJAX request

	if (isset($_POST['ajax']) && $_POST['ajax'] === 'campaign-form')

	{

		echo CActiveForm::validate($model);

		Yii::app()->end();

	}


	// POST from the form

	if(isset($_POST['Campaign']))

	{

		$model->attributes = $_POST['Campaign'];

		if($model->save())

		{

			...

		}

	}


	// CustomerSubscription grid

	$model2 = new CustomerSubscription('search');

	$model2->unsetAttributes();  // clear any default values

	if (isset($_GET['CustomerSubscription']))

	{

		$model2->attributes = $_GET['CustomerSubscription'];

	}


	$this->render('something', array('model' => $model, 'model2' => $model2));

}

...

// You have to change your view code just a little ...



In short, it’s a mixture of actionCreate() in CampaignController and actionAdmin() in CustomerSubscriptionController. You can easily construct it from your gii-generated CRUDs.

Thank you mate, the help was very much appreciated.

I updated the controller and got it working with:




'dataProvider'=>$model2->search(),

'filter'=>$model2,



Now just to investigate the filtering.

-Cheers mate