Yii Embedding CHtml::listData into CGrid - yii

I'm trying to build a form that will enable the user to change values for multiple rows and then click submit. I am able to retrieve the rows from the database and display them in a table. However, instead of a textfield. I want to them to be droplists.
The $list contains options I want to display for each row's drop list.
What am I missing?
<div class="row bottom">
<?php
$list = CHtml::listData(Attendancetype::model()->findAll(), 'AttendanceTypeID', 'AttendanceTypeName');
print_r($list);
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=> $model->search(),
'columns'=>array(
'CalendarDate',
'GradeName',
array(
'value'=>'$data->TeacherFirstName . " " . $data->TeacherLastname',
'header'=>'Teacher'
),
array(
'value'=>'$data->FirstName . " " . $data->LastName',
'header'=>'Student'
),
array(
'value'=>$list,
'type'=>'raw',
'header'=>'Status'
),
),
));
?>
</div>
Current error message.
PHP warning
call_user_func_array() expects parameter 1 to be a valid callback, array must have exactly two members
C:\wamp\www\yii\framework\base\CComponent.php(611)
599 * #since 1.1.0
600 */
601 public function evaluateExpression($_expression_,$_data_=array())
602 {
603 if(is_string($_expression_))
604 {
605 extract($_data_);
606 return eval('return '.$_expression_.';');
607 }
608 else
609 {
610 $_data_[]=$this;
611 return call_user_func_array($_expression_, $_data_);
612 }
613 }
614 }
615
616

Just to add to previous answer. If you want to use external variable you need to define it in your CColumn.
So your code will look like:
$list = CHtml::listData(Attendancetype::model()->findAll(), 'AttendanceTypeID', 'AttendanceTypeName');
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=> $model->search(),
'columns'=>array(
....
array('header'=>'Action',
'type'=>'raw',
'value'=> function ($data,$row) use $list {return CHtml::dropDownlist('actionList','', $list,array());}
),
This will work like you need. Same with Ninad answer.
Cgridview is most flexible yii widget and have alot of wiki examples.

Try doing this in your cgridview
array('header'=>'Action',
'type'=>'raw',
'value'=>"CHtml::dropDownlist('actionList','', CHtml::listData(
Attendancetype::model()->findAll(), 'AttendanceTypeID', 'AttendanceTypeName'),array())"
),

Related

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

Custom search in Yii model for CGrid view on relational data

I have a custom search in my model noted below, It works with out any issue in MySQL and when I step through the code with NetBeans Yii builds the query properly but returns no rows. I run the same query in MySQL and I get the intended rows of information. Is there something I am doing wrong?
public function fnSearch(){
$nParts=explode(" ", $_REQUEST['fnText']);
$criteria=new CDbCriteria;
$criteria->condition='first_name like ":fname" and last_name like ":lname"';
$criteria->params=array(';fname'=>trim($nParts[0]).'%', ':lname'=>trim($nParts[1]).'%');
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
My test form looks like this:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'test-form',
'enableAjaxValidation'=>false,
)); ?>
<?= CHtml::textField('fnText'); ?>
<?php $this->endWidget(); ?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'employees-grid',
'dataProvider'=>$model->fnSearch(),
// 'filter'=>$model,
'columns'=>array(
'first_name',
'last_name',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
The controller has this:
public function actionTest(){
$model=new Employees;
$this->render('test',array(
'model'=>$model,
));
}

Display name instead of Id in Yii

I want to display the name of Shift instead of shift_id
I have a dropboxlist from other table that is like this
<div>
<?php echo $form->labelEx($model,'mon'); ?>
<?php echo $form->dropDownList($model, 'mon', CHtml::listData(
Shift::model()->findAll(), 'shft_id', 'name'),
array('prompt' => 'Select a Department')
); ?>
<?php echo $form->error($model,'mon'); ?>
I have two tables that is
Day: id_day,mon,tues,wed,etc
Shift: shft_id,start,end,name,status
Here is the relation in the day
'shift'=>array(self::HAS_MANY,'Shift','shft_id'),
For Shift:
'day'=>array(self::BELONGS_TO,'Day','id_day'),
It is already working. The choices in the dropbox was the name of the Shift, and puts the shft_id in the mon,tues,wed,etc. In the view of the form it looks like
id_user: 3
mon:5
tues:5
wed:6
what I wanted to be is that in the view.
id_user: 3
mon: 6am-5pm
tues: 7am-6pm
etc.etc.
I dont know what command it is. I have no idea. Help me please
Instead of User::getusername() method.
I think you can simply use this in one line.
// format models resulting using listData
<?php echo $form->dropdownlist($model,'user_id', CHtml::listData(User::model()->findAll(),'id', 'name')); ?>
below is an example which I think may solved your problem
in _form.php
<?php echo $form->dropdownlist($model,'user_id', User::getusername()); ?>
<?php echo $form->error($model,'user_id'); ?></th>
in User model
public function getusername()
{
$criteria2=new CDbCriteria;
$criteria2->select='*';
$quser=User::model()->findAll($criteria2);
foreach($quser as $r)
{
$user_id= $r->user_id;
$user_name=$r->user_name;
$user_array[$user_id]=$user_name;
}
return $user_array;
}
I'm trying to understand what you are really asking. I believe you were saying that you are already able to populate your records correctly from the dropdowns. But when you are displaying, the view is only showing the shift_id value? The relation for the day should be able to get your shift name. How are you displaying the list:
mon: 5
tue: 5
wed: 6
I'm trying to understand if you need help displaying the dropdown items properly, or how to display the shift name later after the values have been set in the database. Also, your 'Day' table has me confused. What are the field names? Are they 'day_id' and 'day'? Or do you actually name the fields after each day of the week?
If you are displaying the data through a foreach and are getting each day (let's assume it is $day) object, then the relationship to 'shift' can give you the name like this: echo $day->shift->name; if the name displays as '#am-#pm'. Otherwise you can display it like this:
echo sprintf('%d a.m. - %d p.m.',$day->shift->start,$day->shift->end);
Thanks for the people who helped me. It did lead me to the answer anyone who has the same problem here is what i did.
In my model/Day
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'monday'=>array(self::BELONGS_TO,'Shift','mon'),
'tuesday'=>array(self::BELONGS_TO,'Shift','tue'),
'wednesday'=>array(self::BELONGS_TO,'Shift','wed'),
'thursday'=>array(self::BELONGS_TO,'Shift','thurs'),
'friday'=>array(self::BELONGS_TO,'Shift','fri'),
'saturday'=>array(self::BELONGS_TO,'Shift','sat'),
'sunday'=>array(self::BELONGS_TO,'Shift','sun'),
);
}
and In my models/Shift
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'day_mon' =>array(self::HAS_MANY,'Day','mon'),
'day_tue' =>array(self::HAS_MANY,'Day','tue'),
'day_wed' =>array(self::HAS_MANY,'Day','wed'),
'day_thurs'=> array(self::HAS_MANY,'Day','thurs'),
'day_fri'=>array(self::HAS_MANY,'Day','fri'),
'day_sat'=>array(self::HAS_MANY,'Day','sat'),
'day_sun'=>array(self::HAS_MANY,'Day','sun'),
);
}
The problem is with my relations. its not set properly, so you need to set it properly then go to Day/view
<?php $this->widget('bootstrap.widgets.TbDetailView',array(
'data'=>$model,
'attributes'=>array(
'id_day',
array(
'name'=>'mon',
'value'=>CHtml::encode($model->monday->name)
),
Here is the output
If you have a detail veiw then use the following code:
widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'name',
array(
'label' => $model->shift->getAttributeLabel('shift_name'),
'value' => $model->shift->shift_name
),)); ?>
In case anybody else will be in my boat, what you will need to do is edit view.php in view/modelname/view.php
widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'name',
'country_id')); ?>
into
widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'name',
array(
'label' => $model->country->getAttributeLabel('country'),
'value' => $model->country->name
),)); ?>

How to set defalut value in CGridView

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();
}

Cgridview with data from 2 unrelated models

I am slowly advancing yii
Not how to apply this:
I have a CGridView which shows me data from 3 related tables
I do it this way (modify the default generated by the crud:
model tabla1
public function search()
{
...
...
...
$criteria=new CDbCriteria;
$criteria->with = array('tabla2','tabla3');
$criteria->together = true;
$criteria->addInCondition('t.idtb1',1,3,5,6,7);
$criteria->compare('idtb2',$this->idtb2);
$criteria->compare('date',$this->date,true);
$criteria->compare('tabla2.codigo',$this->codigo,true);
$criteria->compare('tabla3.description',$this->descrip,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
The view
...
...
...
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'redeemed-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name'=>'codigo',
'value'=>'$data->tabla2->codigo',
),
array(
'name'=>'description',
'value'=>'$data->tabla3->descrip',
),
This shows me everything works perfect.
The problem is that I want to use two more tables that are not related, they are tabla4 and tabla5 and aggregate data from both tables to Cgridview.
  as can be done for linking data to table1 to be unrelated to tabla4 and tabla5?
Greetings and thanks