How to set defalut value in CGridView - yii

How can I set default value if $data->pic=="" in my dataprovider values. Set data pic as na.jpg
widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'type'=>'raw',
'name'=>'pic',
'value'=>'CHtml::image("http://localhost/studentpics/".$data->pic)',
),
));

widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'name'=>'your Image URL',
'type'=>'image',
'htmlOptions'=>array('width'=>'30px','height'=>'30px'),
),
));

value is evaluated as a php expression so it is ok to use a condition in it
'value'=>'CHtml::image(($data->pic)?"http://localhost/studentpics/".$data->pic:"default_image_url")',
P.S it's a bad idea to use absolute url's everywhere.

I think you can override afterFind method of CActiveRecord
protected function afterFind()
{
if($this->pic ===null)
{
$this->pic = na.jpg;
}
parent::afterFind();
}

Related

How to hide a column in yii

I want to hide a column in yii based on a condition. I have used a function in model. but visible functionality is not working in this column.
$this->widget('zii.widgets.grid.CGridview', array(
'id'=>'gridview',
'dataProvider'=>$dp,
'columns'=>array(
array(
'header' => 'Entries',
'value' => '$data->entry_name'
),
array(
'name' => 'value',
'value' => '$data->entry_name',
'visible'=>'$data->show()',
'type'=>'raw'
),
)
)
);
function in Model
public function show()
{
.........
return 1 or 0;
}
but it not working. Please help
I don't see a condition anywhere yet. You should use a comparison operator like return $this->attribute !== null; in your show function. Which returns true if attribute contains some value. Or if you want to do it on random you can return rand(0,1) == 1;.

cgridveiw to display fetched data via customized query

I am a newbei in yii, when a person clicks on a category display him all products under that particular category in a gridview
view
productcategory
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'admin-grid',
'dataProvider'=>$model->SelectedCategoryProducts,
'filter'=>$model,
'columns'=>array(
'Name',
'Model',
'Brand',
'Price',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
controller
product
public function actionProductcategory($id)
{
$model= Product::model()->SelectedCategoryProducts($id);
var_dump($model);
$this->render('productcategory',array(
'model'=>$model,'id'=>$id,
));
}
model
product
public function SelectedCategoryProducts($id)
{
$dataProvider=new CActiveDataProvider('Product', array(
'criteria'=>array(
'select'=>'name,model,price,brand',
'condition'=>'category=:category',
'params'=>array(':category'=>$id),
)));
var_dump($dataProvider);
return $dataProvider;
}
CException
Property "CActiveDataProvider.sellerSelectedCategoryProducts" is not defined.
PLEASE HELP! I am losing my mind on this ... not able to display in gridview.
Pass $id to your view file.
$this->render('productcategory',array('model'=>$model,'id'=>$id));
Then pass id to model function in ccgridview function.
'dataProvider'=>$model->SelectedCategoryProducts($id),
Hope this might help
Controller file
public function actionProductcategory($id)
{
$model=new Product;
$this->render('productcategory',array('model'=>$model,
'id'=>$id));
}
In View file
'dataProvider'=>$model->SelectedCategoryProducts($id),
UPDATE 1
'columns'=>array(
'name',
'model',
'brand',
'price',
change them to lowercase which are your original column names
$dataProvider=new CActiveDataProvider('Product', array(
'criteria'=>array(
'select'=>'name,model,price,brand',
'condition'=>'category=:category',
'params'=>array(':category'=>$id),
)));
this can retrieve needed data.... first check that data is perfect ... is it right after that take second step....

Date Field Search not filtering for 'dd/mm/yyyy' format - YII Framework

CJUIDatePicker not filtering value for dd/mm/yyyy format but filtering if i gave DB date format manually like shown in image, I have attached all codes including View and Model for Search panel and Date Widget, Please look into this,
My Model Search(),
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('crm_base_contact_id',$this->crm_base_contact_id);
$criteria->compare('name',$this->name,true);
$Date = date('Y-m-d',strtotime($this->created)); // get proper Y-m-d
$startOfDay = $Date . ' 00:00:00'; // from start of the day
$endOfDay = $Date . ' 23:59:59'; // until end of the day
// the rows between these
$criteria->addBetweenCondition('created', strtotime($startOfDay) , strtotime($endOfDay) );
$criteria->compare('createdby',$this->createdby,true);
$criteria->compare('description',$this->description);
$criteria->compare('is_active',$this->is_active);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
My View for CGridView,
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'basecontact-grid',
'dataProvider'=>$model->search(),
//'filter'=>$model,
'columns'=>array(
array('class'=>'CButtonColumn',),
'name',
'crm_base_contact_id',
array(
'name'=>'is_active',
'filter'=>CHtml::dropDownList('Event[is_active]', '',
array(
''=>'',
'1'=>'Y',
'0'=>'N',
)
),
'value' =>'($data->is_active==1)?"Y":"N"',
),
'created',
'createdby',
'description',
),
)); ?>
My View Code for Search column,
<?php $this->widget('zii.widgets.jui.CJuiDatePicker',array(
'model'=>$model,
'id'=>'Search-Created',
'attribute'=>'created',
'options'=>array(
'dateFormat'=>'dd/mm/yy',
'showAnim'=>'fold',
'buttonImage'=>Yii::app()->baseUrl.'/images/icons.date.png',
'buttonImageOnly'=>true,
'buttonText'=>'',
'showAnim'=>'fold',
'showOn'=>'button',
'showButtonPanel'=>false,
),
));?>
you should probably change the date format. set it like this, 'dateFormat'=>'dd-mm-yy',
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model'=>$model,
'attribute'=>'dob',
// additional javascript options for the date picker plugin
'options' => array(
'showAnim' => 'fold',
'dateFormat'=>'dd/mm/yyyy',
),
'htmlOptions' => array(
'style' => 'height:20px;'
),
));
Update:
In your model, define it as following, in my case dob is the attribute name. you can have your own instead.
This will change your dateformat from "dd/mm/yyyy" to "yyyy-mm-dd internally before you run any find.
Ref : http://www.yiiframework.com/doc/api/1.1/CActiveRecord#beforeFind-detail
protected function beforeFind()
{
$this->dob=date('Y-m-d', strtotime($this->dob));
parent::beforeFind();
}
update 2
Replace your search method with this,
public function search() {
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria = new CDbCriteria;
$criteria->compare('crm_base_contact_id', $this->crm_base_contact_id);
$criteria->compare('name', $this->name, true);
$criteria->compare('created', '07/12/2013'); //date('Y-m-d', strtotime($this->created))
$criteria->compare('createdby', $this->createdby, true);
$criteria->compare('description', $this->description);
$criteria->compare('is_active', $this->is_active);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
Update 3
change this line $criteria->compare('created', '07/12/2013'); to
$criteria->compare('created', date('Y-m-d', strtotime($this->created));

Yii data provider with clist view

i want to search multiple models in Yii application. The search result are displayed in CList view. Need to use dataprovider in list view. So how can i use multiple dataprovider in Clist view?
You cannot make use of multiple dataproviders rather you combine the result in a single dataprovider
Something like this to get you started:
public function actionSearch($q) {
// Sanitize input
$q = strtolower(strip_tags($q));
$q = preg_replace('/[^a-z 0-9 _ \- \']/', '', $q);
$model1 = Model1::model()->findAll('title LIKE "%'.$q.'%"');
$model2 = Model2::model()->findAll('title LIKE "%'.$q.'%"');
$rawData = array_merge($model1, $model2);
$dataProvider = new CArrayDataProvider($rawData, array(
'sort'=>array(
'attributes'=>array(
'datePublished DESC', 'title',
),
),
'pagination'=>array(
'pageSize'=>10,
),
));
$this->render('search', array(
'dataProvider' => $dataProvider,
'query' => $q,
));
}

How do you add a button to a CGridView?

Sounds simple, right? I've searched high and low and I can't find out how to do this. I have a CGridView:
$dataProvider = new CArrayDataProvider ($auctions);
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
'id::ID',
'product.title::Title',
'state::Status',
),
));
I want to add a fourth column that only contains a simple button that will execute javascript when pressed. I've tried:
array(
'class' => 'CButtonColumn',
),
This just gives me an error:
Undefined property: stdClass::$primaryKey
Any ideas?
Try this:
$dataProvider = new CArrayDataProvider ($auctions);
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
'id::ID',
'product.title::Title',
'state::Status',
array(
'type' => 'raw',
'value' => '<button onclick=\'alert("It works!")\' value="clickme"/>'
)
),
));
The best way to do it is with CButtonColumn::template and CButtonColumn::buttons.
array(
'class' => 'CButtonColumn',
'template' => '{view} {update} {delete} {copy}',
'buttons'=>array(
'copy' => array(
'label'=>'Copy', // text label of the button
'url'=>"CHtml::normalizeUrl(array('copy', 'id'=>\$data->id))",
'imageUrl'=>'/path/to/copy.gif', // image URL of the button. If not set or false, a text link is used
'options' => array('class'=>'copy'), // HTML options for the button
),
),
),
In this example there are the three default buttons and one custom, the 'copy' button.
If you don't want some of the default buttons (i.e. view, update and delete), you can remove them. Then, define the properties of the button that you added. I defined label, url, image and html options.