Unable to set pageSize for pager in CGridView - Yii - php

I am trying to set the pageSize property of pager in CGridView but in vain. By the way currently there are total of 2 items, i want to display only 1 on 1 page. Thanks.
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'pager' => array(
'pageSize' => 1,
),
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'username',
'email',
'pass',
'type',
'date_entered',
array(
'class'=>'CButtonColumn',
),
),
)); ?>

remove the following from the view
'pager' => array(
'pageSize' => 1,
),
inside the dataprovider array in your model search method add this code
$dataProvider = new CActiveDataProvider('your_model', array(
'pagination'=>array(
'pageSize'=>your_page_size,
),
'criteria'=>$criteria,
));

Related

Yii Booster Gridview table joining table

i want to create a table like this Yii Booster Gridview table
here is my code in controller:
$rawData=Jobspecs::model()->with('customer')->findAll();
$gridDataProvider=new CArrayDataProvider($rawData, array(
'id'=>'user',
'sort'=>array(
'attributes'=>array(
'id', 'customer',
),
),
));
$gridColumns = array(
array('name'=>'id', 'header'=>'Js No.', 'htmlOptions'=>array('style'=>'width: 60px')),
array('name'=>'WHAT TO PUT HERE TO SHOW CUSTOMER NAME', 'header'=>'Customer Name'),
array(
'htmlOptions' => array('nowrap'=>'nowrap'),
'class'=>'booster.widgets.TbButtonColumn',
'viewButtonUrl'=>null,
)
);
in my model sapcustomers:
return array(
'customer'=>array(self::BELONGS_TO, 'Sapcustomers', 'customer'),
);
jobspecs model
return array(
'cardname'=>array( self::HAS_MANY, 'Jobspecs', 'customer' ),
);
my view
<?php
$this->widget(
'booster.widgets.TbGridView',
array(
'type' => 'bordered',
'dataProvider' => $gridDataProvider,
'template' => "{items}",
'columns' => $gridColumns,
)
);
?>
as you can see i joined sapcustomers and jobspecs table. my question is what code i need to put on the $gridcolums to show the customer name data from the table sapcustomer. thanks for the help
I think this should work for you:
$gridColumns = array(
[...]
array(
'name'=>'customer',
'value'=>'$data->customer->name',
'header'=>'Customer Name'
),
);
Explanation
The $data stands for the Jobspecs-model from the actual row. Then you access the related customer and his name.

how to filter using the user's role at user grid view?

How to filter the records using user's role at user grid view?
I am using yii-user extension. I am able to show user's role on user/admin gridview,but i can i use the filter on this? Here is my view:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name' => 'id',
'type'=>'raw',
'value' => 'CHtml::link(CHtml::encode($data->id),array("admin/update","id"=>$data->id))',
),
array(
'name' => 'username',
'type'=>'raw',
'value' => 'CHtml::link(UHtml::markSearch($data,"username"),array("admin/view","id"=>$data->id))',
),
array(
'name'=>'email',
'type'=>'raw',
'value'=>'CHtml::link(UHtml::markSearch($data,"email"), "mailto:".$data->email)',
),
'create_at',
'lastvisit_at',
/*array(
'name'=>'superuser',
'value'=>'User::itemAlias("AdminStatus",$data->superuser)',
'filter'=>User::itemAlias("AdminStatus"),
),*/
array(
'name'=>'status',
'value'=>'User::itemAlias("UserStatus",$data->status)',
'filter' => User::itemAlias("UserStatus"),
),
array(
//'name'=>'assignments',
'header'=>Rights::t('core', 'Roles'),
'type'=>'raw',
'value'=>function($data) {
$roles = Rights::getAssignedRoles($data->id);
foreach($roles as $role){
$user_role=$role->name;
}
return $user_role;
}
),
array(
'class'=>'CButtonColumn',
'template'=>'{view}{delete}'
),
),
));
You can add filter like below:
array(
//'name'=>'assignments',
'header'=>Rights::t('core', 'Roles'),
'type'=>'raw',
'filter'=>CHtml::listData(Rights::model()->findAll(),'id','name'), //***
'value'=>function($data) {
$roles = Rights::getAssignedRoles($data->id);
foreach($roles as $role){
$user_role=$role->name;
}
return $user_role;
}
),
I assumed that Rights is a model and holds roles on it with id,name.
By the line that I indicated that with *** in comments, Yii will generate a dropdown as a filter which shows roles name as option value and roles id as option value.

Include a pagination and summary text alone before gridview in YII

I want show pagination before gridview content. i tried renderPager() I cannot get widget object call the method
code here...
$gridview = $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$expirations,
'id'=>'image-grid-list',
'itemsCssClass'=>'table collections',
'ajaxUpdate'=>true,
'template'=>'{items}{summary}',
'pager'=>array(
'class'=>'CLinkPager',
'header'=>'',
),
'columns'=>array
(
array(
'class'=>'CCheckBoxColumn',
'selectableRows' => null,
'name'=>'image_id',
"value"=>'$data->image_id',
"id"=>"expirations",
"htmlOptions"=>array("style"=>"width:30px;")
),
array(
'name'=>'',
'type'=>'html',
'value'=>'
(!empty($data->image_title))?CHtml::image("/".Yii::app()->params["imagePath"]."/".$data->catalog->catalog_title."/thumb_".$data->image_title,"",array()):"no image"',
'header'=>''
),
array(
'name'=>'image_title',
'type'=>'raw',
'value'=>'CHtml::link($data->catalog->catalog_title."/".$data->image_title,"javascript:void(0);")."<br>#".$data->image_id."<br/><br/> Number of light boxes ".$data->collection_count." <br/><br/> Number of downloads ".$data->downloaded_count ',
'header'=>'Title'
),
array( // display 'author.username' using an expression
'name'=>'copyright_expiration_date',
'type'=>'raw',
'value'=>'(!empty($data->copyright_expiration_date))?date("m-d-Y",$data->copyright_expiration_date):"-"',
'header'=>'Rights expiration date'
),
array( // display 'author.username' using an expression
'name'=>'copyright_type',
'type'=>'raw',
'value'=>'$data->copyright_type',
'header'=>'Rights'
),
array( // display 'author.username' using an expression
'name'=>'',
'value'=>'$data->usage_and_terms',
'header'=>'Usage and terms'
),
array( // display 'create_time' using an expression
'name'=>'photographer_name',
'value'=>'$data->photographer_name',
'type'=>'raw',
'header'=>'Photographer name'
),
),
),true);
$gridview->renderPager();
I cannot call the method .. I want create widget object , call renderContent, renderPager etc
use template attribute.
'template' => '{pager}{items}{summary}',
eg.,
$gridview = $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$expirations,
'id'=>'image-grid-list',
'itemsCssClass'=>'table collections',
'ajaxUpdate'=>true,
'template' => '{pager}{items}{summary}',
'pager'=>array(
'class'=>'CLinkPager',
'header'=>'',
),
'columns'=>array
(
......

How to update the yii CGridview summary after deleting a row

Deletion is happening using ajax, but the count summary showing like (1-25 of 100 ) has to be updated as (1-25 of 99). how to do that?
UPDATED THE CODE:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$provider,
'filter' => $model,
'hideHeader'=>false,
'afterAjaxUpdate'=>'function(id,data){jQuery("#summaryUpdate").html(" - " +jQuery(".grid-view .summary").html());}',
'columns'=>array(
'id',
'username',
'dob',
'email',
array(
'class'=>'CCustomColumn',
'afterDelete' => 'function(id,data){jQuery("#summaryUpdate").html(" - " +jQuery(".grid-view .summary").html());}',
'updateButtonImageUrl'=>Yii::app()->baseUrl.'/images/edit.png',
'deleteButtonImageUrl'=>Yii::app()->baseUrl.'/images/delete.png',
'buttons'=>array(
'update'=>array('url'=>'Yii::app()->createUrl("user/update", array("id"=>$data["id"]))', ),
'delete'=>array('url'=>'Yii::app()->createUrl("user/delete", array("id"=>$data["id"]))', )
)
),
),
'cssFile'=>Yii::app()->request->baseUrl."/css/gridview.css",
));
it should update the summary automatically
did you use
$.fn.yiiGridView.update('grid-name');
to update the grid ?

CGridView Filter doesn't work when you add more than one button to the CButtonColumn

I have a CGridView as follows,
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'order-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'order_id',
//'ordered_datetime',
'customer_id',
'status',
//'delivery_address',
array(
'class'=>'CButtonColumn',
'template' => '{view} {rollback} {receive}{pack} {dispatch}{delivered}',
'htmlOptions'=>array('width'=>'250px'),
'buttons'=>array(
'receive'=>array(
'id'=>'receive',
'name'=>'receive',
'url'=>'$this->grid->controller->createUrl("/shop/order/admin&received=true", array("id"=>$data->order_id,"asDialog"=>1,"gridId"=>$this->grid->id))',
'type'=>'submit',
'imageUrl'=>'/mdg/images/Receive1.png',
'visible'=>'($data->status=="pending")?true:false;'
),
'rollback'=>array(
'id'=>'rollback',
'name'=>'rollback',
'click'=>''
'url'=>'$this->grid->controller->createUrl("/shop/order/admin&rollback=true", array("id"=>$data->order_id,"asDialog"=>1,"gridId"=>$this->grid->id))',
'imageUrl'=>'/mdg/images/rollback.jpg',
'visible'=>'($data->status=="pending")?false:true;'
),
),
),
),
)); ?>
And When I add one more button to the buttons array, the filter doesn't work. Any Idea why that is?
There is an extra click => '', in the recieve button's array.

Categories