I need to print out the result in CActiveDataProvider with CGridView and with pagination
The following is my function in model
public function getCompaniesJobsByCompanyId ( $companyId )
{
$criteria = new CDbCriteria(array(
'with'=>array(
'jobs'=>array(
'scopes' => array( 'offline' => array(0), ),
'vacancies'=>array(
'scopes'=>array(
'removed' => array(0),
'archived' => array(0),
),
),
),
),
'condition' => $this->getTableAlias(false) . '.company_id = ' . (int) $companyId,
)
);
$criteria->together = true;
$dataProvider = new CActiveDataProvider($this, array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => 20, //Yii::app()->params['pageSize'],
),
));
return $dataProvider;
}
How could be the CGridView to render my data?
By this way I iterate the result
$dataProvider = Employers::model() -> getCompaniesJobsByCompanyId(2);
foreach ( $dataProvider->getData() as $data ) {
echo $data['name'];
foreach ( $data->jobs as $jobs ) {
echo ' ---- ' .($jobs->employer_id) . ' ---- ';
foreach ( $jobs->vacancies as $vacancies ) {
echo '<br />' . ($vacancies->id) . '<br />';
}
}
}
And My view
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider' => $dataProvider,
'columns'=>array(
'title', // display the 'title' attribute
'id', // d
array(
'name'=>'job id',
//'value'=> '$data->jobs[0]["id"]',
//'value'=> 'jobs.id',
//'type' => 'raw'
),
array(
'name'=>'vacancy id',
//'value'=> '$data->jobs[0]->vacancies[0]->id',
//'value'=> 'print_r($data->jobs[0])',
'value'=> '$data->jobs["id"]',
//'type' => 'raw'
),
array(
'name'=>'employer name',
'type'=>'raw', // to encode html string
'value'=>'$data->name',
),
),
));
Any one can help me to print the values in jobs and vacancies relations?
UPDATE
I tried adding 'value' => '$data->jobs->id' but get an error Trying to get property of non-object
Update :
I tried 'value' => '$data->jobs[0]["id"]' and it display the the result correctly, but if there are only 1 record on the table. When there is more than 1 record on the table,
I need to print all result, how to loop on it ?
This line 'value' => '$data->jobs->id' raised an error Trying to get property of non-object because you have been permitted to accessed the property of object instead of array of objects (jobs)
The workaround is you declare a function to do the task on the controller which rendered your gridview
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
...
array(
'name'=>'newColumn',
//call the method 'gridDataColumn' from the controller
'value'=>array($this,'gridDataColumn'),
'type'=>'raw'
)
),
));
class MyController extends Controller
{
//called on rendering the column for each row
protected function gridDataColumn($data,$row)
{
$cellResult = "";
//do your loop here
//example
foreach ( $data->children as $child ) {
$cellResult .=$child->id . "<br/>";
$cellResult .=$child->name . "<br/>";
}
return $cellResult ;
}
...
}
Yii Tutorial
CGridView: Render customized/complex datacolumns
Related
Hi I have CRUD generated CGridView in yii. I need to add a new button to CGridView rows and hide it if appointment_status(one of CGridView column) value equals 0
This is my code of CGridView,
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'bookings-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'id',
'name',
'email',
'telephone',
'time',
'employee',
'appointment_status',
'client_ip',
'link' => array(
'header' => 'Confirmation',
'type' => 'raw',
'value' => 'CHtml::button("$data->appointment_status",array("onclick"=>"document.location.href=\'".Yii::app()->controller->createUrl("controller/action",array("id"=>$data->id))."\'"))',
'visible'=>$data->appointment_status==1,
),
array(
'class' => 'CButtonColumn',
),
),
));
But all I'm getting is error stating,
Undefined variable: data
It would be great help if someone can look into it.
you can use like this:
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'bookings-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'id',
'name',
'email',
'telephone',
'time',
'employee',
'appointment_status',
'client_ip',
'link' => array(
'header' => 'Confirmation',
'type' => 'raw',
'value' => function ($data) {
if ($data->appointment_status == 1) {
return CHtml::button("$data->appointment_status", array("onclick" => "document.location.href=\'" . Yii::app()->controller->createUrl("controller/action", array("id" => $data->id)) . "\'"));
} else {
return;
}
}
),
array(
'class' => 'CButtonColumn',
),
),
));
Your 'visible' handling the column visibility and not the button, you can use custom attribute on model to create and handle the button visibility.
add to your model:
public function getConfirmationButton()
{
if ($data->appointment_status == 1) {
return CHtml::button($this->appointment_status,array("onclick"=>"document.location.href=\'".Yii::app()->controller->createUrl("controller/action",array("id"=>$this->id))."\'"));
} else {
return '';
}
}
and call it in your view:
..........
'link' => array(
'header' => 'Confirmation',
'type' => 'raw',
'value' => '$data->confirmationButton',
),
...........
visible is a boolean or a PHP expression that will be evaluated to give a boolean. During the evaluation $data is assigned to the current item from the dataProvider used. $data doesn't exist outside of the evaluation function evaluateExpression(). As such the implementation should be:
`visible` => '$data->appointment_status == 1',
You need to quote value of visible key in link array. So instead of this:
'visible'=>$data->appointment_status==1
It should be:
'visible'=>'$data->appointment_status==1'
it should work now.
You will get undefined variable because visible not allow any callback.
Try this solution, it's yii2 code and i don't know much of Yii.
'delete' => function ($url, $model) {
return ($model->isVisible($model)) ?
Html::a('<span class="glyphicon glyphicon-trash"></span>',
$url,
['title' => Yii::t('app', 'Delete')]) : '';
public static function isVisible($data)
{
return ($data->appointment_status == 1) ? true : false;
}
I'm a newbie in Yii programming.
I'm using boostrap library on Yii via Yiistrap/Yiiwheels
I've created a relation table view
The related view is a Whgridview itself
The first (master grid) has a TbRelationColum clicking it i display the second grid (detail grid).
When I click on the row to display the sub grid, everything appears ok. When I change the sort order or the page of the sub grid disappear both grid.
I understand we should differentiate the css class of the pager and the sort of sub grid from the main grid. How to do this specifically in Yii-Way?
Is This the problem?
This is the view of the main grid:
$this->widget('yiiwheels.widgets.grid.WhGridView',array(
'id'=>'masterGrid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'template' => "{summary}{items}<div class=\"row-fluid\"><div class=\"pull-right\">{pager}</div></div>",
'type' => array(TbHtml::GRID_TYPE_BORDERED, TbHtml::GRID_TYPE_STRIPED),
'columns'=>array(
array(
'class' => 'yiiwheels.widgets.grid.WhRelationalColumn',
//'name' => 'multiMembers.id',
'type' => 'raw',
'header' => 'Sub Items',
'url' => $this->createUrl('multiGroup/ajaxSubItems'),
'cacheData' => false,
'value' => "CHtml::tag('button',array('class'=>'btn btn-primary'),'Sub Items')",
'htmlOptions'=>array('style'=>'width:90px;'),
'cssClass' => 'showSubItems',
),
'id',
'title',
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
),
),
));
This is the sub-grid:
echo CHtml::tag('h3',array(),'Sub Items Group #"'.$id.'"');
$this->widget('yiiwheels.widgets.grid.WhGridView', array(
'id'=>'subGrid_'.$id,
'type'=>array(TbHtml::GRID_TYPE_BORDERED, TbHtml::GRID_TYPE_STRIPED),
'dataProvider' => $gridDataProvider,
'template' => "{summary}{items}<div class=\"row-fluid\"><div class=\"pull-right\">{pager}</div></div>",
'columns' => $gridColumns,
));
This is the controller:
/**
* Manages all models.
*/
public function actionAdmin()
{
$model=new MultiGroup('search');
$model->unsetAttributes(); // clear any default values
if (isset($_GET['MultiGroup'])) {
$model->attributes=$_GET['MultiGroup'];
}
$this->render('admin',array(
'model'=>$model,
));
}
public function actionAjaxSubItems()
{
$id = Yii::app()->getRequest()->getParam('id');
$model = $this->loadModel($id);
if($model->numSubItems > 0) {
$this->renderPartial('_child', array('id' => $id,
'gridDataProvider' => $this->getGridDataProvider($id),
'gridColumns' => $this->getGridColumns()
), false, true);
} else {
echo 'Non ci sono Sub Items.';
}
}
public function getGridDataProvider($id) {
$sql = 'SELECT * FROM multi_member WHERE groupid = :groupid ORDER BY lastname,firstname';
$cmd = Yii::app()->db->createCommand($sql);
$cmd->bindParam(':groupid', $id, PDO::PARAM_INT);
$result = $cmd->queryAll();
$dataProvider = new CArrayDataProvider(
$result, array(
'sort' => array(
'attributes' => array('id','groupid','firstname','lastname','membersince'),
'defaultOrder' => array('lastname' => CSort::SORT_ASC, 'firstname' => CSort::SORT_ASC),
),
'pagination' => array(
'pageSize' => 2,
),
));
return $dataProvider;
}
public function getGridColumns() {
return array('id', 'lastname', 'firstname', 'membersince');
}
How can I do?
thank you ..
If the extensions you're using all extend CGgridView, then you should be able to use option 'ajaxUpdate' (link to documentation).
Try setting 'ajaxUpdate'=>false in one of the grids (or in both of them) to see whether it helps you.
Sometimes setting ajaxUpdate to false is the only way I get get some grids to behave the way I want them to...
I am trying to create a PDF from a filtered CGridView. The value will be passed via dropdown in Advanced search but the problem is that i am unable to filter the search by my pdf function.
Controller
public function actionPrint() {
$mPDF1 = Yii::app()->ePdf->mpdf('ar','A4','14','dejavusanscondensed');
$model=new Country('search');
$model->center_id = 1;// This value will be passed from dropdown
//and i want the report to be made on this
$model->unsetAttributes();
if(isset($_GET['Country']))
$model->attributes=$_GET['Country'];
$html = '';
$html .= $this->renderPartial('candidates', array('model'=>$model, 'enablePagination' => false),true);
$mPDF1->WriteHTML($html, false);
$mPDF1->Output('list.pdf','D');
}
View
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'country-grid',
'dataProvider'=>$model->search($enablePagination),
'summaryText' => '',
// 'enablePagination' => false,
'filter'=>$model,
'columns'=>array(
'name',
array(
'header'=>' Total Registered Candidates',
'value'=>'$data->itemsTotal',
),
),
));
echo CHtml::link(
'Save as PDF',
Yii::app()->createUrl('country/print'),
array('class'=>'btnPrint btn btn-danger','target'=>'_blank'));
Model
public function search($enablePagination = true)
{
$criteria->together= true;
$criteria->with=array('center');
$criteria->compare('center.name', $this->center_id, true);
..........
if ($enablePagination)
{
$pagination = array(
'pageSize' => 30,
);
}
else
{
$pagination = false;
}
return new CActiveDataProvider($model, array(
'criteria' => $criteria,
'pagination' => $pagination,
));
}
Since center_id is a foreign key the line
$criteria->compare('center.name', $this->center_id, true);
should read
$criteria->compare('center_id', $this->center_id);
You could also do the following but this adds a condition on the joined table and could lead to slower queries.
$criteria->compare('center.id', $this->center_id);
This is follow up on this question:
Display related has_many data inside once cell in Yii TbExtendedGridView
I got that cell working, but now i have no idea how to make it sortable and filterable (filter field is hidden).
View:
$this->widget('bootstrap.widgets.TbExtendedGridView', array(
'id'=>'user-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'user_name',
'favorite_color',
array(
'value'=>'$data->getAllDates()',
),
),
));
One user can have many Dates that can be single date or date range, so i have getAllDates function that fetches em all and put em all inside string so they can be represented inside single cell for that user.
Model:
public function relations()
{
return array(
'listofdates' => array(self::HAS_MANY, 'Dates', 'user_id'),
);
}
public function getAllDates()
{
$data = '';
foreach ($this->listofdates as $date) {
$data .= $date->start_date.'-'.$date->end_date;
}
return $data;
}
I have no idea how to enable filtering and search for dates column. There is no even title for it or filter field.
I can enable filter field for that column by adding 'name' => 'whatever', but of course there is no single column in database for that data so i get MYSQL error.
I'm guessing i need to create another custom function for filtering but i have no idea where to start.
you can create an additional field for filtering and sorting $datesFilter and filter like this:
in model:
public $datesFilter;
public function rules()
{
array(
array('datesFilter', 'safe', 'on'=>'search')
);
}
public function search()
{
$criteria=new CDbCriteria;
...
// filtering
if ($this->datesFilter!==null)
$criteria->addCondition('YOUR QUERY CONDITION');
...
// sorting
$sort = new CSort;
$sort->attributes =
array(
...
'datesFilter'=>=>array('asc'=>'YOUR SORT ASC', 'desc'=>'YOUR SORT DESC'),
);
}
in view:
$this->widget('bootstrap.widgets.TbExtendedGridView', array(
'id'=>'user-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'user_name',
'favorite_color',
array(
'value'=>'$data->getAllDates()',
'name'=>'datesFilter',
),
),
));
I have:
$dataProvider = new CArrayDataProvider ($items ,
array(
'pagination'=>array(
'pageSize'=> 10,
),
));
And I render:
$this->widget('zii.widgets.CListView', array(
'dataProvider'=> $dataProvider,
'itemView' => 'items',
'summaryText' => '',
'emptyText' => '
',
));
Problem is it whines about the ID in the dataset. It wants it to be called "id". So I geT:
Undefined property: stdClass::$id
C:\wamp\www\yii\web\CArrayDataProvider.php(108)
How can I change it so that it uses "itemid" instead of "id" as the column reference?
Set the data provider's keyField property:
$dataProvider = new CArrayDataProvider ($items ,
array(
'keyField' => 'itemid',
'pagination'=>array(
'pageSize'=> 10,
),
));