CGridView Problem

Hi

I have a CGridView as below :




<?php 


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

	'id'=>'mail-grid',

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

	'filter' => $model,

	'pager'=>array(

            'header'=>'',

	    'pageSize' => 1,

            'firstPageLabel'=>'&lt;&lt;',

            'prevPageLabel'=>'&lt;',

            'nextPageLabel'=>'&gt;',

            'lastPageLabel'=>'&gt;&gt;',

        ),

	'columns' => array(

		'id',

                array('class' => 'CCheckBoxColumn','id' => 'check-boxes' ,'selectableRows' => '2','header'=>'Selected',),

                array('name'=>'mail_subject', 'value' => '$data->mail_subject'),

                array('name'=>'from_name', 'value' => '$data->from_name'),

                array('name'=>'to_user_email', 'value' => '$data->to_user_email'),

                

,),)); ?>



the problem that when i try to search a record with an id it’s giving any result(using the filter).

the second problem is that the “page numbers” below the record seems without any ‘href’ an empty link when i click in page ‘2’ for example nothing happen i used firebug and i found that there is a request with this parameter :




ajax    mail-grid



this is the search method in the model




 public function search()

    {

            $criteria=new CDbCriteria;

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

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

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

            .

            .

            .

            .


            return new CActiveDataProvider($this, array(

                'criteria'=>$criteria,

                'pagination' => array('pageSize'=>10),


            ));

    }



so any idea about ti ? :)

hello guys

any suggestion to resolve this issues :)

Hi maher,

Um, I really don’t know what’s wrong …

Try commenting out the pager configuration, just for a test.




<?php 

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

	'id'=>'mail-grid',

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

	'filter' => $model,

//	'pager'=>array(

//            'header'=>'',

//	    'pageSize' => 1,

//            'firstPageLabel'=>'&lt;&lt;',

//            'prevPageLabel'=>'&lt;',

//            'nextPageLabel'=>'&gt;',

//            'lastPageLabel'=>'&gt;&gt;',

//        ),

	'columns' => array(

		'id',

                array('class' => 'CCheckBoxColumn','id' => 'check-boxes' ,'selectableRows' => '2','header'=>'Selected',),

                array('name'=>'mail_subject', 'value' => '$data->mail_subject'),

                array('name'=>'from_name', 'value' => '$data->from_name'),

                array('name'=>'to_user_email', 'value' => '$data->to_user_email'),

                

,),)); ?>



Hi

before commenting the pager it shows this :

< 1 2 >

after commenting the pager it shows this :

Go to page: < Previous 1 2 Next >

but still same problem, no links to the pages.

I see.

Well, I don’t see anything strange in your code as long as I see. :(

Do you have anything else to show us?

this is my search method :




public function search()

    {

            

            $criteria=new CDbCriteria;

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

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

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

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

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

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

            

            $sort = new CSort();

            //$sort->defaultOrder = 'id DESC';

            $sort->attributes = array(

                            'id',

                            'from_name',

                            'to_user_email',

                            'send_status'

            );


            $dataProvider = new CActiveDataProvider(get_class($this), array(

                'criteria'=>$criteria,

                'pagination' => array('pageSize'=>Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize'])),

                'sort' =>$sort,

                'criteria'=>$criteria

            ));

            

            return $dataProvider;

    }



and my action :




public function actionList(){

        

//this is for the pagesize

        if (isset($_GET['pageSize'])) {

            Yii::app()->user->setState('pageSize',(int)$_GET['pageSize']);

            unset($_GET['pageSize']);  // would interfere with pager and repetitive page size change

        }

        

        $model = new SystemMailOut('search');

       

        //search function

        $model->unsetAttributes();

        if(isset($_GET['SystemMailOut'])){

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

        }

        

        // Setup args and render view

        $args = array();

        $args['model'] = $model;

        $this->render('list',$args);

    }



Maybe this is not a problem, but …




            $dataProvider = new CActiveDataProvider(get_class($this), array(

                'criteria'=>$criteria,

                'pagination' => array('pageSize'=>Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize'])),

                'sort' =>$sort,

//              'criteria'=>$criteria

            ));



And, how about your view script?

I guess you are enclosing the grid in a form, aren’t you?

If so, please make sure that you don’t have any orphaned tags (opening tags without the corresponding closing tags, or vice versa) around your grid.

Actually It ddn’t work

this is my view




<?php

$pageSize=Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']);

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

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

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

	return false;

});

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

	$.fn.yiiGridView.update('mail-grid', {

		data: $(this).serialize()

	});

	return false;

});

");

?>





<p>This list should contain all PageMail-Models</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'=>'mail-grid',

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

	'filter' => $model,

//	'pager'=>array(

//            'header'=>'',

//            'pageSize' => 1,

//            'firstPageLabel'=>'&lt;&lt;',

//            'prevPageLabel'=>'&lt;',

//            'nextPageLabel'=>'&gt;',

//            'lastPageLabel'=>'&gt;&gt;',

//        ),

	'columns' => array(

		'id',

                array('class' => 'CCheckBoxColumn','id' => 'check-boxes' ,'selectableRows' => '2','header'=>'Selected',),

                array('name'=>'created_at', 'value' => '$data->createdDate', 'type'=>'raw'),

                array('name'=>'mail_subject', 'value' => '$data->mail_subject', 'type'=>'raw'),

                array('name'=>'from_name', 'value' => '$data->from_name', 'type'=>'raw'),

                array('name'=>'to_user_email', 'value' => '$data->to_user_email', 'type'=>'raw'/*,'filter' => CHtml::listData(SystemMailOut::model()->findAll(array('select'=>array('to_user_email'),'distinct'=>TRUE)), "to_user_email", "to_user_email")*/ ),

                array('name'=>'send_status', 'filter' => array(0 =>"not read", 1 =>'read'),'value' => '@$data->send_status ? "read" : "not read"', 'type'=>'raw'),

                

                array(

			'class'=>'CButtonColumn',

			'template'=>'{viewbutton}{sendbutton}',

                        'header'=>CHtml::dropDownList('pageSize',$pageSize,array(5=>5,10=>10,20=>20,50=>50,100=>100),array(

                            // change 'user-grid' to the actual id of your grid!!

                            'onchange'=>"$.fn.yiiGridView.update('mail-grid',{ data:{pageSize: $(this).val() }})",

                        )),

			'buttons'=>array(

				'viewbutton' => array (

					'label'=>'View',

					'imageUrl'=>'/images/admin/view.png',

					'url'=>'"/admin/mail/view/id/".$data->id',

				),

				'sendbutton' => array (

					'label'=>'delete',

					'imageUrl'=>'/images/admin/email.png',

					'url'=>'"/admin/mail/send/id/".$data->id',

                                        'visible'=>'$data->send_status <> 1',

				),

			),

		),

				

	),

)); ?>




I see.

Maybe I’ve overlooked something very simple, but, well, I’m lost, too. :(

Well thank you any way for your intervention, me too I’m still confused what’s wrong

Maybe you can post the screenshot of the problematic pagers, along with the corresponding HTML outputs.

BTW, are you using a customized CLinkPager for the grid?

this is a screenshot 1

3168

screenshot 1.png

and when i click in 2 (for page Num 2) i get this in firebug :


GET list?ajax=mail-grid


http://......./admin/mail/list?ajax=mail-grid

I thought that this is the problem since the begenning, it should send a link with the items to show and the page number.

But i’m new with yii and I’m stuck there.

And what do you mean by "a customized CLinkPager for the grid"

It seems to me that something interferes or messes with the createUrl() method…

Normally clicking page 2 would target something like

Sometimes when I’m stuck in circles, I build again the action, search method, and view from scratch, preferably using Gii’s (or Giix) standard CRUD. And step by step add additional features and customization.

Ah, bennouna has pointed out one thing very important.

Try resetting your ‘urlManager.urlFormat’ from ‘path’ to ‘get’ in the application configuration, and see what happens.




	'components'=>array(

		...

		'urlManager'=>array(

			// 'urlFormat'=>'path',

			'urlFormat'=>'get',

			...



I mean, your ‘urlManager.rules’ may not be properly configured to work with ‘path’ format for your mailController actions in your admin module. It also explains your 1st problem that you can’t get the search result.

By resetting ‘urlFormat’ to ‘get’, the creation of the urls may start to work correctly.

The way you create urls for those buttons are not recommended. They won’t work with ‘get’ format.

The recommended way for creating urls is to use ‘createUrl’ method like the following:




	'buttons'=>array(

		'viewbutton' => array (

			'label'=>'View',

			'imageUrl'=>'/images/admin/view.png',

			'url'=>'Yii::app()->createUrl("/admin/mail/view", array("id" => $data->id))',

		),

	'sendbutton' => array (

		'label'=>'delete',

			'imageUrl'=>'/images/admin/email.png',

			'url'=>'array("/admin/mail/send", "id=>".$data->id)',

                        'visible'=>'$data->send_status <> 1',

		),

	),



The 2nd one used for ‘sendbutton’ is the shortcut way of calling ‘createUrl’.

[EDIT]

I’m sorry. I’ve made a typo.

This is correct:




			'url'=>'array("/admin/mail/send", "id"=>$data->id)',



I changed the urlFormat to get and the changes are that when i click on any link nothing happens, I’m always in the index, even that the link on the browser changes.

And for the problem is not on delete or view button, but in the links for the nex pages of the records.

How do you construct the urls in your links?

Are you using ‘createUrl’ like in the following?




echo CHtml::link('List', Yii::app()->createUrl('/admin/mail/list'));

// or

echo CHtml::link('List', array('/admin/mail/list'));



Or are you just giving a string like this?




echo CHtml::link('List', '/admin/mail/list');



The former will work both for ‘path’ and ‘get’ formats, and will work even if ‘showScriptName’ is true or false.

But the latter only works when ‘urlFormat’ is ‘path’ and ‘showScriptName’ is false. And you have to manually synchronize the url strings with your ‘rulManager.rules’.

We should use the former method for Yii application, because it’s much more flexible and solid.

Using the latter for Yii app may looks easier, but it will surely be the cause of many troubles.

(We have to use the latter for the urls outside of our app, of course.)

Your delete and view buttons are working "by accident".

The links for the pagers are constructed by Yii using "createUrl". And "createUrl" fails to create the right url with your "urlManager.rules".

Once you have configured your “urlManager.rules” correctly, then links in the pager will work, but your delete and view buttons may not work any more. Thus I strongly recommend you to use ‘createUrl’ for your links. :)

Hey guys I gor it, the problem was in the UrlManager, and the thing happen in the




public function createUrl



and since there is no route whoch mean no $mappedRoute, I had to add this case by adding this




if($mappedRoute){

		.

                .

                .

                .

                .

                .

                .

	  }

          else{

               // if no mapped route exists we use the original link creation method

              $route = parent::createUrl($route, $params, $ampersand);

          }



Finnally, and i want to thank you guys with your replies, i found out that#s something with createUrl.

:D