Cgridview Filter Doesn't Seem To Work.

Hello guys,

I’m pretty new to Yii and I’ve been playing around with boilerplate and a downloaded theme and it went smooth until I stumbled upon this issue with the filters in the cgridview. For some reason it doesn’t return the filtered results. I’ve been trying to pinpoint the problem for a while and I noticed that the snippet that executes javascript


Yii::app()->clientScript->registerScript

does not seem like it’s executing the javascript code but it does show it when you check the source code in Chrome.

When I try to filter the gridview it doesn’t show anything and this also happens if I make a new generated CRUD for another model in Gii, I can’t seem to fish out the problem out of this one. I’ve been struggling with this for hours.

Here’s the piece of code I’m fighting with. If more is necessary I’ll be more than happy to provide it.

My view





            Yii::app()->clientScript->registerScript('search', "

           

                $('.search-button').click(function(){

                        $('.search-form').toggle();

                        return false;

                });

                $('.search-form form').submit(function(){

                        $('#companies-grid').yiiGridView('update', {

                                data: $(this).serialize()

                        });

                        return false;

                });

            ");

            ?>

            

            <div class="box box-solid">

                <div class="box-body">

                 

            <div class="center-content">


            <p>

            You may optionally enter a comparison operator (<b>&lt;</b>, <b>&lt;=</b>, <b>&gt;</b>, <b>&gt;=</b>, <b>&lt;&gt;</b>

            or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.

            </p>

            <?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>

            <div class="search-form" style="display:none">

            <?php $this->renderPartial('_search',array(

                    'model'=>$model,

            )); ?>

                

            </div><!-- search-form -->

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

                    'id'=>'companies-grid',

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

                    'filter'=>$model,

                    'itemsCssClass'=>"table table-bordered table-hover dataTable",

                    'columns'=>array(

                            'cid' => array('header' => '#', 'name'=>'cid', 'filterHtmlOptions'=> array('style' => 'width: 65px;')),

                            'name' => array('header' => 'Bedrijfsnaam', 'name'=>'name'),

                            'first_name' => array('header' => 'Voornaam', 'name'=>'first_name'),

                            'last_name' => array('header' => 'Achternaam', 'name'=>'last_name'),

                            'date_registration' => array('header' => 'Registratie datum', 'name'=>'date_registration', 'filter' => "<input type='text' name='Companies[date_registration]' id='datemask' data-inputmask='alias': 'dd/mm/yyyy' data-mask placeholder='dd/mm/yyyy'"),/*, filterHtmlOptions'=> array('id' => 'datemask', 'data-inputmask' => "'alias': 'dd/mm/yyyy'", 'data-mask'=>"")),*/

                            /*                            'kvk_number',

                            'btw_number',

                            'fax',

                            'phone',

                            'address',

                            'zipcode',

                            'location',

                            'website',

                            'email',

                            'skype',

                            'status',

                            'uid',

                            */

                            array(

                                    'class'=>'CButtonColumn',

                                    'template'=>'{view}{update}{delete}'

                            ),

                    ),

            )); ?>

            </div>



Search action




$model=new Companies('search');

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

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

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


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

			'model'=>$model,

		));



Search in model




public function search()

	{

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


		$criteria=new CDbCriteria;


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

I’m thinking it’s something with the javascript because this code does make sense for me, but hey ho I may be wrong… Would appreciate it if you guys know the solution and thanks in advance!

Got it solved thanks to someone clever in the live chat, the problem was 'em in the main layout where I loaded in jQuery myself when Yii already loads in jQuery from the core and that interfered with the core’s jQuery causing the cGridview and the advanced search to not work.

When you type anything in the grid filter fields and press enter it doesn’t filter the grid? I mean not in the advanced search form.

Please make sure you have added the safe rule in rules() method of the model.


    public function rules()

    {

        return array(

            array('LIST OF COLUMNS HERE', 'safe', 'on' => 'search'),

        );

    }

Edit. I can see you solved it already. Glad everything is clear now.