Search/filter Does Not Work In Gridview With Custom Column

Hi,

This is very simple, but I could not find the answer, so please help. In my gridview, I added a custom column that changed the column header, and the filter does not work. It returned error on some column, and if one column worked, then the custom column showed nothing.




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

	'id'=>'user-grid',

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

	'filter'=>$model,

	'columns'=>array(

		'id',

		'username',

		array(

            'name'=>'password',

            'value'=>$model->password,

			),

		'password',

		'usertype_id',

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



and the controller is just default, created by yii




public function actionAdmin()

	{

		$model=new User('search');

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

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

			$model->attributes=$_GET['User'];


		$this->render('admin',array(

			'model'=>$model,

		));

	}



the column password is used as custom column.

Thank you.

check whether the password column is safe on search scenario in you model’s rules() and moreover check whether the password column is used for searching in your model’s search() method

You have the same column twice?




array(

   'name'=>'password',

   'value'=>$model->password,

),



[size="2"]and[/size]


'password',

thank you for your reply. I think this is simple with just adding the column with custom header, but looks like the ajax call giving error.

this is the search function in model, again, it is by default




public function search()

	{

		// @todo Please modify the following code to remove attributes that should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('id',$this->id);

		$criteria->compare('username',$this->username,true);

		$criteria->compare('password',$this->password,true);

		$criteria->compare('usertype_id',$this->usertype_id);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}



And, even I remove the default password column, it still gives error. This is the link that gave error




index.php/user/admin?User%5Bid%5D=&User%5Busername%5D=&User%5Bpassword%5D=prop&User%5Busertype_id%5D=&User_page=1&ajax=user-grid



And it does not matter which field, I take this model because it is short and easy to look at. The error in yii pop up window shows "Use of Undefined Constant"

Please help

At least post the complete error… does it say the name of the undefined constant? where the error happens, what line…

Thank you,

I changed the trouble column to username




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

	'id'=>'user-grid',

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

	'filter'=>$model,

	'columns'=>array(

		'id',

		//'username',

		array(

            'name'=>'username',

            'value'=>$model->username,

			),

		'password',

		'usertype_id',

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



This is the error that yii popped up when doing a search on the username field. If do search on other fields, it works with blank username (no data fill in that username field)




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

<p>Use of undefined constant prop - assumed 'prop' (/var/www/html/yii/yii-1.1.14.f0fee9/framework/base/CComponent.php(612) : eval()'d code:1)</p>

<pre>#0 /var/www/html/yii/yii-1.1.14.f0fee9/framework/base/CComponent.php(612) : eval()'d code(1): CWebApplication->handleError()

#1 /var/www/html/yii/yii-1.1.14.f0fee9/framework/base/CComponent.php(612): eval()

#2 /var/www/html/yii/yii-1.1.14.f0fee9/framework/zii/widgets/grid/CDataColumn.php(137): CDataColumn->evaluateExpression()

#3 /var/www/html/yii/yii-1.1.14.f0fee9/framework/zii/widgets/grid/CGridColumn.php(151): CDataColumn->renderDataCellContent()

#4 /var/www/html/yii/yii-1.1.14.f0fee9/framework/zii/widgets/grid/CGridView.php(616): CDataColumn->renderDataCell()

#5 /var/www/html/yii/yii-1.1.14.f0fee9/framework/zii/widgets/grid/CGridView.php(572): CGridView->renderTableRow()

#6 /var/www/html/yii/yii-1.1.14.f0fee9/framework/zii/widgets/grid/CGridView.php(482): CGridView->renderTableBody()

#7 /var/www/html/yii/yii-1.1.14.f0fee9/framework/zii/widgets/CBaseListView.php(167): CGridView->renderItems()

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

#9 /var/www/html/yii/yii-1.1.14.f0fee9/framework/zii/widgets/CBaseListView.php(150): preg_replace_callback()

#10 /var/www/html/yii/yii-1.1.14.f0fee9/framework/zii/widgets/CBaseListView.php(135): CGridView->renderContent()

#11 /var/www/html/yii/yii-1.1.14.f0fee9/framework/web/CBaseController.php(173): CGridView->run()

#12 /var/www/html/qtireport/protected/views/user/admin.php(60): UserController->widget()

#13 /var/www/html/yii/yii-1.1.14.f0fee9/framework/web/CBaseController.php(126): require()

#14 /var/www/html/yii/yii-1.1.14.f0fee9/framework/web/CBaseController.php(95): UserController->renderInternal()

#15 /var/www/html/yii/yii-1.1.14.f0fee9/framework/web/CController.php(869): UserController->renderFile()

#16 /var/www/html/yii/yii-1.1.14.f0fee9/framework/web/CController.php(782): UserController->renderPartial()

#17 /var/www/html/qtireport/protected/controllers/UserController.php(143): UserController->render()

#18 /var/www/html/yii/yii-1.1.14.f0fee9/framework/web/actions/CInlineAction.php(49): UserController->actionAdmin()

#19 /var/www/html/yii/yii-1.1.14.f0fee9/framework/web/CController.php(308): CInlineAction->runWithParams()

#20 /var/www/html/yii/yii-1.1.14.f0fee9/framework/web/filters/CFilterChain.php(133): UserController->runAction()

#21 /var/www/html/yii/yii-1.1.14.f0fee9/framework/web/filters/CFilter.php(40): CFilterChain->run()

#22 /var/www/html/yii/yii-1.1.14.f0fee9/framework/web/CController.php(1145): CAccessControlFilter->filter()

#23 /var/www/html/yii/yii-1.1.14.f0fee9/framework/web/filters/CInlineFilter.php(58): UserController->filterAccessControl()

#24 /var/www/html/yii/yii-1.1.14.f0fee9/framework/web/filters/CFilterChain.php(130): CInlineFilter->filter()

#25 /var/www/html/yii/yii-1.1.14.f0fee9/framework/web/CController.php(291): CFilterChain->run()

#26 /var/www/html/yii/yii-1.1.14.f0fee9/framework/web/CController.php(265): UserController->runActionWithFilters()

#27 /var/www/html/yii/yii-1.1.14.f0fee9/framework/web/CWebApplication.php(282): UserController->run()

#28 /var/www/html/yii/yii-1.1.14.f0fee9/framework/web/CWebApplication.php(141): CWebApplication->runController()

#29 /var/www/html/yii/yii-1.1.14.f0fee9/framework/base/CApplication.php(180): CWebApplication->processRequest()

#30 /var/www/html/qtireport/index.php(13): CWebApplication->run()

</pre>



and I am using version $yii=dirname(FILE).’/../yii/yii-1.1.14.f0fee9/framework/yii.php’;

Thank you for your help

in your search url there is an property named prop also sended to the search function and yii does not recognized this

it is actually the string that I entered in the input box for searching. If I enter Yii, it will say "Yii" is the undefined constant.

Thank you

so whatever you entered in that text box is send as property to the yii and yii tries to find it. due to unavailable of such attribute in the model it throws an exception this is what happened for you I think

Thank you for replying,

Yes, it looks like that in the error message. But if you look at the url sent by ajax, the "prop" string is the value of password field. So, I just wonder why it turned out to be something else.

Thanks

Thank you all for your time. I think I found the solution, eventhough not fully understand it yet. I found this document in Yii gridview about header name


change the column header

If you want to change the column header in your grid view:

array(            // display 'category.name' using an 'Category' as header

            'name'=>'category.name',

            'header'=>'Category',

        ),



so I dropped the line:




'value'=>$model->username,



in my view, then it works.

Thanks all again