My controller code
public function actionRead() {
$criteria=new CDbCriteria(array(
'order'=>'id desc',
));
$enP=new CActiveDataProvider('Readss', array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>1,
),
));
$this->renderPartial('_read', array('data'=>$enP));
}
it display the gridview properly ,
my view code
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'dfsdfsdf',
'dataProvider' => $data,
'itemsCssClass' => 'sdfsdfdl',
'ajaxUpdate' => false,
'template' => '{summary}{pager}{items}{summary}{pager}',
'columns'=>array(
array(
'header'=>'#',
'value'=>'++$row',
'htmlOptions'=>array(
'style'=>'width:50px;'
)
),
'name',
array(
'name'=>'Date',
'header'=>'Date',
'value'=>'Yii::app()->dateFormatter->format("d MMM y",strtotime($data["work_date"]))'
),
),
));
But when i click my pagination my page is refressing , what mistake i did here ,
I want to load data on click pagination without page refresh.
Hope this would work you can just make the 4rd argument true
//This will disable jquery being loaded again (use this if you have jquery already loaded
//means you are calling this in ajax)
//Yii::app()->clientScript->scriptMap['jquery.js'] = false;
//Yii::app()->clientScript->scriptMap['jquery.min.js'] = false;
$this->renderPartial('_read', array('data'=>$enP),false,true);
In your CGridView you indicated to disable AJAX by following line:
'ajaxUpdate' => false,
You must change it to TRUE:
'ajaxUpdate' => true,
Related
How to solve problem?
array(
'header'=>'Manage',
'class'=>'CButtonColumn',
'template'=>'{Manage}',
//'visible'=>'$data->checkSub($data->sub_id)', //it not work not call function
'visible'=>Model::test(), //it works
'buttons'=>array(
'Manage Exam Result'=>array(
'label'=>'<i class="fa fa-cogs"></i>',
),
),
),
),
visible is not evaluated for each row, it must be set to true or false in the end. So to determine if current column is visible, you have to call single function from your view that returns true or false.
[
...
'visible' => Model::checkIfVisibleColumn(),
...
],
array(
'class'=>'CButtonColumn',
'buttons'=>array(
'manage' => array('visible' => 'name_function($data->id)'),
),
),
<?php
$this->widget('zii.widgets.CListView', array(
'dataProvider' => $dataProvider,
'itemView' => '_view',
'ajaxUpdate' => true,
'pager' => array(
'class' => 'CLinkPager',
),
'template' => '{pager}{items}',
));
?>
after executing this the pager components are displaying as column format.is their any way to change that into row format?
I guess you getting this widget through renderPartial() method. When using the renderPartial() method controller not sending js and css files with response by default. If you want to get response with these files you should pass true in fourth parameter:
$this->renderPartial('myView',array(), false, true);
What I'm trying to accomplish with the following code is when the 'roleSelector' dropdown is changed, it fires a ajax request to the 'admin/permissions/assign' url which in turn, sets a state for the role selected. I need it as a state so I can use it in the CLinkColumn column within the CGridView. However, when the success javascript fires and the grid view reloads the content of the cgridview does not update, while the summary does (it shows the number of results that should be in the grid view).
I'm not sure why either but this seems to be the only CGridView that I've had to use the 'ajaxUrl' property in order for the $.fn.yiiGridView.update() call to work. If I omit the url property it returns an error stating it could not find the url which I don't recall having to set in the past.
The Action:
class AssignAction extends CAction {
// Used in the url to determine if we are assigning or revoking permissions
const FLAG_APPLY = 'apply';
const FLAG_REVOKE = 'revoke';
// Identifier to the user saved state of the role selected
const STATE_ROLE = 'roleSelected';
public function run($name='', $action='') {
// Get the role the user selected
if (isset($_POST['roleSelector'])) {
$roleSelected = $_POST['roleSelector'];
Yii::app()->user->setState(self::STATE_ROLE, $roleSelected);
Yii::app()->end();
}
// Get all of the roles from the system
$roleSelected = Yii::app()->user->getState(self::STATE_ROLE);
$roles = AuthItem::model()->byTypes(array(CAuthItem::TYPE_ROLE))->findAll();
$authItemProvider = new CActiveDataProvider('AuthItem', array(
'criteria' => array(
'condition' => (!$roleSelected) ? '1=0' : '',
),
'pagination' => array(
'pageSize' => 30,
)
));
$this->getController()->render('assign', array(
'roles' => $roles,
'roleSelected' => $roleSelected,
'authItemProvider' => $authItemProvider,
));
}
}
The View:
<div id="content-header">
<h1 class="p-mt10">Assign Permissions</h1>
<div class="p-fr p-pb10">
<?php
$roleList = CHtml::listData($roles, 'name', 'label');
echo CHtml::dropDownList('roleSelector', $roleSelected, $roleList, $htmlOptions=array(
'empty' => 'Select a Role',
'ajax' => array(
'type' => 'POST',
'url' => $this->createUrl('/admin/permissions/assign'),
'data' => array('roleSelector' => 'js: $(this).val()'),
'success' => 'js: function() { $.fn.yiiGridView.update("GridView-AuthItem") }',
),
));
?>
</div>
</div>
<div id="content-body">
<table>
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'GridView-AuthItem',
'dataProvider' => $authItemProvider,
'columns' => array(
'label:properCase',
'typeName:properCase',
'description',
'bizrule',
array(
'class' => 'CLinkColumn',
'label' => 'Assign',
'urlExpression' => 'array("/admin/permissions/assign",
"name" => $data->name,
"action" => (AuthItemChild::model()->byParentAndChild(Yii::app()->user->getState(AssignAction::STATE_ROLE), $data->name)->find()
? AssignAction::FLAG_REVOKE : AssignAction::FLAG_APPLY),
)',
'htmlOptions' => array(
'style' => 'text-align: center',
),
'linkHtmlOptions' => array(
'class' => 'button gray icon i_stm_edit',
),
),
),
));
?>
</table>
</div>
I forgot I left the table tags in there since it originally was a CListView. Removing those tags seems to have fixed my problem.
Below is my code in view.php file
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'myGrid',
'dataProvider' => $model->search(),
'filter' => $model,
'enableSorting' => true,
'columns' => array(
array(
'name' => 'id',
'header' => 'ID',
'value' => $data->id,
),
array(
'header' => 'Value',
'value' => '$data->getValue($data->id)', //getValue($id) is a custom method in the model.php file which returns a value after some calculations
'filter' => $model->listOfFilterValues(), //listOfFilterValues() is a custom method in the model.php file which returns a CHtml::listData
),
),
)
);
As you can observe, I am using a custom method in model.php file to get the Value column(because i cannot get it from a query).
The gridView appears and the dropdown list appears. Works fine till now.
The problem is that the filtering using the dropdown (in Value column) doesnt work. (because its not a column from the query output)
And also the sort on the Value column(when i click on the column header) doesnt work.
Is there a way to get this done? Your help is highly appreciated. Thank you.
Try this:
In Model:
class Plans extends BasePlans
{
...
public $planTypeSearch;
...
public function search() {
...
$criteria->compare('plan.plan_type', $this->planTypeSearch, true);
...
),
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'sort'=>array(
'attributes'=>array(
'planTypeSearch'=>array(
'asc'=>'plan.plan_type',
'desc'=>'plan.plan_type DESC',
),
),
),
);
In your view:
array(
'name'=>'planTypeSearch',
'header'=> 'Plan Type',
'value'=>'(isset($data->plan)) ? $data->plan->plan_type: null',
'filter'=>isset($plans) ? CHtml::listData($plans, 'plan_type','plan_type') : NULL,
),
I'm following the documents here http://www.yiiframework.com/wiki/278/cgridview-render-customized-complex-datacolumns/
So I have the following in view
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'item-table-grid',
'dataProvider'=>$model->search(),
'itemsCssClass'=>'item-table-grid',
'columns'=>array(
'customer_name',
array(
'name'=>'Edit',
'value'=>array($model, 'editLink'),
),
),
));
And here is the editLink function in model
public function editLink($data, $row) {
$link = '';
if ($data->is_draft) {
$link = 'Edit';
}
return $link;
}
The problem that I'm having is that the return value is encoded so I get <a href=...>
Is there a way to tell CGridView not to encode the value?
Thanks
Solution A:
array(
'name'=>'Edit',
'type' => 'raw',
'value'=>array($model, 'editLink'),
),
B: (not good enough)
array(
'name' => 'Edit',
'class' => 'CLinkColumn',
'urlExpression' => '$data->is_draft ? "customer/update/{$data->id}" : "#disabled"',
'label' => 'edit',
),
try this ..
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'item-table-grid',
'dataProvider'=>$model->search(),
'itemsCssClass'=>'item-table-grid',
'columns'=>array(
'customer_name',
array(
'name'=>'Edit',
'type' => 'raw',
'value'=>array($model, 'editLink'),
),
),
));
my code in CGridView
array(
'name'=>'Post Content',
'value'=>array($model,'postContent'),
'type'=>'html',
'htmlOptions'=>array('width'=>'380'),
),
in Model I have the following method
public function postContent($data){
echo $data->content;
}
it works fine but when i click on another page of my pagination
then it doesn't works means it work only on Index page first time opened...
plz any solution....???