How to update the yii CGridview summary after deleting a row - php

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 ?

Related

Decimal Format in Yii TBGridview

I have code like this :
<?php
$this->widget('bootstrap.widgets.TbGridView',array(
'id'=>'appliances-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name'=>'price',
'type'=>'number',
'htmlOptions'=>array('style' => 'text-align: right;'),
),
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
'template'=>'{update}{delete}'
),
),
));
?>
the result is integer number only and search filter is work, how to change it into decimal format but search filter still working ?
I tried to change it into :
array(
'name'=>'price',
'type'=>'number',
'htmlOptions'=>array('style' => 'text-align: right;'),
'value'=>function($data)
{
return number_format($data->price,2,',','.');
},
),
But it makes search filter not working properly.
I found the answers here :
Yii Number Formatting
How to format numeric string using C.Gridview - Yii
And also I found this link Yii , show different date format in cgridview
Try This:-
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(
'cost', // display the 'cost' attribute
array( // display the 'cost_in_dollars' using an expression
'name' => 'cost_in_dollars',
'value' => 'number_format($data->cost/100, 2)',
),
),
));

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.

Unable to set pageSize for pager in CGridView - Yii

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,
));

how to show "Yes/No" CGridView yii depending on the flag field 0/1?

I am stuck in a problem in CGridView yii, my refund field show 0/1 but I want to show "Yes" if 0 and "No" if 1, without using any second table.
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'transaction-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'member_id',
array(
'header' => 'MemberName',
'name' => 'member_id',
'value' => '$data->member->f_name'
),
'refund',
'band_id',
array(
'class'=>'CButtonColumn',
'template'=>'{view}',
),
),
));
Both of the other answers will work, but the cleanest way to do it is:
'columns'=>array(
'id',
'member_id',
...
'refund:boolean',
),
There are a bunch of CGridView column data types that are auto-used if you use colons like above. More info here: https://github.com/samdark/a-guide-to-yii-grids-lists-and-data-providers/blob/master/grid-columns.md
array(
'name' => 'refund',
'header' => "Refund",
'value' => '$data->refund?Yii::t(\'app\',\'Yes\'):Yii::t(\'app\', \'No\')',
'filter' => array('0' => Yii::t('app', 'No'), '1' => Yii::t('app', 'Yes')),
'htmlOptions' => array('style' => "text-align:center;"),
),
Hope this will solve your problem.
Replace "refund" with this code.
array(
'header' => 'Refund',
'name' => 'refund',
'value' => '($data->refund == 0) ? "Yes" : "No"'
),
When displaying a boolean field in a CGridView use the name:type:header format when creating the columns to specify the type as boolean. E.g.
$this->widget('zii.widgets.grid.CGridView', array(
...
'columns'=>array(
'id',
'refund:boolean',
),
If you want to change the way the field is displayed in a CActiveForm change the render method to use either a checkbox or a dropdown list. My preference is dropdown list because it gives you the option of setting the value back to null.
$form->dropDownList($model,'refund', array(null=>"Not checked", 0=>"No", 1=>"Yes"));
Quick fix:
Replace 'refund', with:
array(
'name' => 'refund',
'type' => 'raw',
'value' => function($model){
return $model->refund == 1 ? 'No' : 'Yes';
}
),
IN VIEWS NAMES ADMIN.PHP
array(
'name'=>'status',
'header'=>'status',
'filter'=>array('1'=>'Inacive','2'=>'Active'),
'value'=>'($data->status=="1")?("Inacive"):("Active")'
),

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