sorting relation table in yii's view - yii

I am trying sorting in cListView.. which works perfectly fine for me with the sorting attributes from the same table.. i want to know if its possible to sort based on the fields from a related table..
for example..
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_viewnew',
'sortableAttributes'=>array(
'Code'
),
)); ?>
works fine..
but i wish to sort from another table field like this..
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_viewnew',
'sortableAttributes'=>array(
'relation_name.Make'
),
)); ?>
this is what i have so far!! and doesn't work
In my model
/**
* #return array relational rules.
*/
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(
'PMake'=>array(self::BELONGS_TO, 'PMake', 'ID'),
);
}
in my controller
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('table1');
$criteria=new CDbCriteria;
$criteria->together=true;
$criteria->with=array('PMake');
$dataProvider->criteria=$criteria;
$dataProvider->sort->defaultOrder='PMake.Make ASC';
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
in my view, the sort here is what i'm trying to get to work.
<?php
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'sortableAttributes' => array('PMake.Make'=>'Make'),
'loadingCssClass' => '', //remove loading icon
));
?>

Try This in Controller
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('table1');
$criteria=new CDbCriteria;
$criteria->together=true;
$criteria->with=array('PMake');
//Made changes here
$criteria->order = 'PMake.Make ASC'
$dataProvider->criteria=$criteria;
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}

Related

Yii Cgridview filter not working?

I am trying to implement a dropdown filter in Cgridview, The column is from another table.
//model search code
//person table
public function search(){
$criteria=new CDbCriteria;
$criteria->alias='per';
$criteria->compare('LastName',$this->LastName,true);
$criteria->compare('FirstName',$this->FirstName,true);
$criteria->join='right JOIN person_surveys ps ON ps.id = per.P_Id';
}
//relation to person survey
public function relations()
{
return array(
'user' => array(self::BELONGS_TO, 'PersonSurvey', 'P_Id'),
);
}
//person survey table model search
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('person_id',$this->person_id,true);
$criteria->compare('survey_ids',$this->survey_ids,true);
$criteria->join='left JOIN persons ps ON ps.id = per.P_Id';
}
//controller
public function actiongriddisplay(){
$model2=new Persons('search');
if(isset($_GET['ajax'])){
$model2->attributes =$_GET['Persons'];
$this->render('griddisplay',array('model2'=>$model2));
}
else{
$this->render('griddisplay',array('model2'=>$model2));
}
}
//in view file
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model2->Search(),
'filter' => $model2,
'ajaxType'=>'GET',
// 'ajaxUpdate'=>'items',
//'ajaxUrl' =>$this->createUrl('userManagement'),
'columns' => array(
array(
'name' => 'FirstName',
'header'=>'Full Name',
),
array(
'name' => 'user.survey_ids',
'header'=>'Survey ids',
'filter'=>CHtml::activeDropDownList($model2 ,'survey_ids', array('224'=>'224','223'=>'223','225'=>'225')),
'value'=>'isset($data->user->survey_ids)?$data->user->survey_ids:null'
),
I have put the search on, in models. why is the dropdown filter not working?
Why this is not working please help?
This link (http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/) will solve your problem.

How to do a default sort on cgridview in yii1

i have the following cgrid view
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'complaint-job-grid',
'dataProvider'=>$model->search($complaint),
//~ 'filter'=>$model,
'columns'=>$columns,
'enableSorting'=>true,
));
my model code is
public function search($complaint)
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->with = array('complaint_job','user');
$criteria->compare('complaint_job.job_desc',$this->job_search, true);
if($this->user_search)
$criteria->addSearchCondition('CONCAT(first_name," ",last_name)', $this->user_search);
$criteria->compare('complaint_id',$complaint);
$criteria->compare('id',$this->id);
$criteria->compare('job_id',$this->job_id);
$criteria->compare('local_description',$this->local_description,true);
$criteria->compare('employee_id',$this->employee_id);
$criteria->order = 't.id ASC';
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
so what i basically want is to show values in the cgridview in descending order of id(primary key). is it possible?
i tried like this with no success
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array(
'defaultOrder'=>'id ASC',
),
You're doing sort in CDbCriteria;
That's why sorting in CGridView which uses CActiveDataProvider not work.
Remove from your model:
$criteria->order = 't.id ASC';
And add to CActiveDataProvider
'defaultOrder'=>'t.id ASC',

undefined variable when variable is defined yii

Is this not defined?! It says dataProvider is not defined. Btw, I did read this. Does it mean I have to list each items in view? Not following what it's saying.
noob question. Sorry.
public function actionIndex()
{
$id=1;
$dataProvider = new CArrayDataProvider(array(
array('product_name'=>Product::model()->product_name,
'inventory_id'=>Product::model()->inventory_id,
'product_price'=>Product::model()->product_price,
),
$this->render('index',array(
'model'=>$this->loadModel($id),
'dataProvider'=>$dataProvider,
))
));
}
view
<div class="widget-body">
<?php $this->widget('bootstrap.widgets.TbDetailView', array(
'type'=>'striped bordered condensed',
'dataProvider'=>$dataProvider,
'columns'=>array(
array('name'=>'product_name', 'header'=>'Product Name'),
array('name'=>'inventory_id', 'header'=>'Inventory ID'),
array('name'=>'product_price', 'header'=>'Price'),
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
'htmlOptions'=>array('style'=>'width: 50px'),
),
),
)); ?>
TbDetailView is supposed to display a single model, you don't need any data provider for it.
Controller code:
public function actionIndex()
{
$id=1;
$this->render('index', array(
'model'=>$this->loadModel($id),
));
}
View:
<?php $this->widget('bootstrap.widgets.TbDetailView', array(
'type'=>'striped bordered condensed',
'data'=>$model,
'attributes'=>array(
'product_name',
'inventory_id',
'product_price',
),
)); ?>

display pagination in view with CActiveDataProvider model

I have following code in action:
$criteria=new CDbCriteria(array(
'condition'=>'status='.Post::STATUS_PUBLISHED,
'order'=>'sortOrder ASC',
'with'=>'commentCount',
));
$criteria->addSearchCondition('tags','home');
$dataProvider=new CActiveDataProvider('Post', array(
'pagination'=>array(
'pageSize'=>Yii::app()->params['postsPerPageHome'],
),
'criteria'=>$criteria,
));
$this->render('home',array(
'dataProvider'=>$dataProvider,
));
home View:
<?php foreach($dataProvider->getData() as $post) { ?>
// ....... displaying $post values
<?php } ?>
How can I display the pagination in above view. I have searched for it, I found only using with zii.widgets.grid.CGridView but here I haven't use the widget.
You need the CPagination from the CActiveDataProvider:
<?php $this->widget('CLinkPager', array(
'pages' => $dataProvider->pagination,
)); ?>

yii update clistview by dropdownlist

I'm a very newbie in Yii framework. I would like to create a page. Which is when the dropdown list change, the listview/gridview will be change by dropdown value.
this is my view
<div class="row">
<?php
$records = Company::model()->findAll();
$company_list = CHtml::listData($records, 'id', 'name');
echo CHtml::dropDownList('company_id','', $company_list,
array(
'onchange'=>"$.fn.yiiListView.update('ajaxListView', {url: '".Yii::app()->createUrl('department/dynamicsectionlist')."?company_id='+$('#company_id option:selected').val()})",
'prompt'=>'Please select a company',
)); ?>
</div>
<?php
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view_section',
'id'=>'ajaxListView',
));
?>
This is model
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('p_id',$this->p_id);
$criteria->compare('created',$this->created,true);
$criteria->compare('updated',$this->updated,true);
$criteria->compare('company_id',$this->company_id);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
This is Controller
public function actionDynamicsectionlist()
{
$company_id = $_POST['company_id'];
$criteria=new CDbCriteria();
$criteria->condition .= 't.id IN (SELECT t2.id, t2.name FROM department t2 WHERE t2.company_id = :company_id)';
$criteria->params[':company_id'] = $company_id;
$dataProvider = new CActiveDataProvider( 'Department', array( 'criteria' => $criteria, ) );
$this->render( 'sectionlist', array( 'dataProvider' => $dataProvider ) );
}
But it is not working. Please help me.
Regads
Tharsoe
I solved it.
This is controller
// Initial view (department/depatmentlist)
public function actionDepartmentlist()
{
$model=new Department('search');
$model->unsetAttributes(); // clear any default values
$model->p_id = 0;
// $dataProvider->getData() will return a list of Post objects
// $dataProvider=new CActiveDataProvider('Department');
$this->render('list_department',array(
'model'=>$model,
));
}
// when the user selected the company from dropdown list
public function actionDynamicsectionlist()
{
$model=new Department('dsearch');
$model->unsetAttributes(); // clear any default values
$model->p_id = 0;
if(isset($_GET['company_id']))
$model->company_id = $_GET['company_id'];
$this->render('sectionlist',array(
'model'=>$model,
));
}
This is model (nothing change)
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('p_id',$this->p_id);
$criteria->compare('created',$this->created,true);
$criteria->compare('updated',$this->updated,true);
$criteria->compare('company_id',$this->company_id);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
This is view (list_department.php)
<h1>Departments List</h1>
<div class="row">
Company<br />
<?php
$records = Company::model()->findAll();
$company_list = CHtml::listData($records, 'id', 'name');
echo CHtml::dropDownList('company_id','', $company_list,
array('prompt'=>'Please select a company',)); ?>
</div>
<?php
/*
for ListView
$this->widget('zii.widgets.CListView', array(
//'dataProvider'=>$dataProvider,
'dataProvider'=>$model->search(),
'itemView'=>'_view_section',
'id'=>'ajaxListView',
));
*/
?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'department-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'name',
//'p_id',
'created',
'updated',
//'company_id',
),
));
?>
<?php
Yii::app()->clientScript->registerScript('search',
"$('#company_id').change(function(){
var companyId = $('#company_id option:selected').val();
$.fn.yiiGridView.update(
'department-grid',
{ type: 'GET',
url: 'http://localhost/mmaig_ceo/ceo-control-system/index.php?r=department/dynamicdepartmentlist&ajax=department-grid&company_id=' + companyId
}
);
});
")
?>