How to lesser text description in CGridView in Yii - php

I have that code in my admin.php i.e. view file in my Yii Project.
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'topic-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'name',
'description:html', // I want to change this
array(
'class'=>'CButtonColumn',
),
),
)); ?>
Actually description:html is showing a lot of text description which is stored in my database.
So my question is that, I want to print just a single line of description here.
So I don't know how to accomplish it.
Any help will be appreciated.
Thanks.

Although hett has given an answer but you can do this also
array(
'name'=>'description',
'value'=>array($this,'showFewLines')
),
and then in your controller create a function
public function showFewLines($data,$row)
{
$allData=$data->description;
return substr($allData, 0, 40);
}

somehow
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'topic-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'name',
array(
'name'=>'description',
'value'=>function($data) {
return substr($data->description, 0, strpos($data->description, '<br>'));
},
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>

Related

How can we implement soft delete on cgridview?

This is my cgridview.Here view,update,delete works fine. but I want to impliment soft deletion . so how can i customize the delete button here?
<div id="status" style="display:none;margin-left:450px;margin-top:25px;"></div>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'printstatusforlocal-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'order_id',
'albumname',
'username',
'usermail',
'bookid',
array('header'=> 'No. of Pages',
'name'=>'noofpages',
'value'=>$model->noofpages,
),
array('header'=> 'Order Date',
'name'=>'orderdate',
'value'=>$model->orderdate,
),
array('header'=> 'Synchronized',
'name'=>'synchronize',
'value'=>'$data->Syncronize',
),
array(
'class'=>'CButtonColumn',
),),
));
?>
</div> <?php $this->endWidget(); ?>
Help me please..
You can create a separate action and set the url of the delete button using deleteButtonUrl:
...
array(
'class'=>'CButtonColumn',
'deleteButtonUrl' => 'array("controller/action", "id" => "$data->id")'
),),

Yii framework : Display value from relation table in admin form

I have a admin.php created by gii, inside it there's a table column 'lang_id' have relation to primary key 'id' of table 'lang'.
What should I put in the columns array ? I think i should use "Lang.name" but it didn't work.
protect/view/mainmenu/admin.php
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'mainmenu-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'menu_id',
'Lang.name', // I want this column display the name of Language, instead of lang_id
'name',
'remark',
array(
'class'=>'CButtonColumn',
'template'=>'{update}'
),
),
)); ?>
protect/model/Mainmenu.php
public function relations(){
return array(
'lang'=>array(self::HAS_ONE, 'Lang', 'lang_id')
);
}
public function search(){
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,true);
$criteria->compare('menu_id',$this->menu_id);
$criteria->compare('lang_id',$this->lang_id);
$criteria->compare('name',$this->name,true);
$criteria->compare('sorting',$this->sorting);
$criteria->compare('remark',$this->remark,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
the relation name is lang. Whereas in the columns array you have used Lang (with l in uppercase).
So you will have to do this
'lang.name'
in the columns array
and second thing in the search()
You need to add one more line
$criteria->with = array(
'lang'
);
maybe this can work
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'mainmenu-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'menu_id',
array(
'name'=>'Lang',
'value'=>'$data->lang->name',
),
'name',
'remark',
array(
'class'=>'CButtonColumn',
'template'=>'{update}'
),
),
)); ?>

Yii use ItemAlias in CGridView

I am using Yii's 'ItemAlias' function to give a custom name to a value, like this:
<?php
public static function itemAlias($type,$code=NULL) {
$_items = array(
'currency' => array(
'1'=>'US Dollar',
'2'=>'Euro',
'3'=>'GB Pound',
'4'=>'Chinese RMB',
'5'=>'Singaporese Dollar'
)
);
if (isset($code))
return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
else
return isset($_items[$type]) ? $_items[$type] : false;
}
?>
Now, in CGridView, it currently displays the number (key) of the currency which it found in the database, but I want to display it's label. The CGridView is generated like this:
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'purchases-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
'id',
'name',
'currency',
[..etc]
Can someone point me into the right direction to display the label instead of the key of the value? Thanks!
Based off of the limited code you showed this is what I think you want if itemAlias() is a function built into your model:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'purchases-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
'id',
'name',
array(
'name' => 'currency',
'value' => '$data->itemAlias("currency",$data->currency)',
),
...
If it is some generic function somewhere not part of your model you would do this:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'purchases-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
'id',
'name',
array(
'name' => 'currency',
'value' => 'itemAlias("currency",$data->currency)',
),
...

CGridView encoded the value

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....???

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