display pagination in view with CActiveDataProvider model - yii

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,
)); ?>

Related

sorting relation table in yii's view

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

Yii update cgridview via ajax button not working.

I have in my controller:
public function actionFilterClients {
if (Yii::app()->request->isAjaxRequest) {
if (isset($_POST['category_id'])) {
$criteria = new CDbCriteria;
$criteria->condition = "user_id=:user_id";
$criteria->params = array(':user_id' => Yii::app()->user->id);
$criteria->compare('category_id',$_POST['category_id'],true);
$dataProvider = new CActiveDataProvider('Client', array(
'criteria'=>$criteria,
));
$this->renderPartial('transfer_step_3' , array('dataProvider'=>$dataProvider)) ;
}
}
}
In my view among other things I have:
<?php $filter=$this->beginWidget('CActiveForm', array(
'id'=>'client-filter-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('class'=>'form-horizontal'),
)); ?>
<label for="category_id">View clients in category:</label>
<?php echo CHtml::dropDownList('category_id','',Client::clientCategories(), array('options' => array('2'=>array('selected'=>true)))); ?>
<?php
echo CHtml::ajaxButton(
'Filter Clients',
'filterclients',
array(
'type'=>'POST',
'update' => 'client-grid' ,
'success' =>"function(data) {
\$.fn.yiiGridView.update('client-grid');}",
)
);
?>
<?php $this->endWidget(); ?>
and
<?php $this->widget('bootstrap.widgets.TbGridView',array(
'type'=>'bordered striped condensed',
'id'=>'client-grid',
'ajaxUpdate' => true ,
'rowCssClassExpression'=>'($data->duplicate==2)?"yellow":($data->duplicate==1?"blue":"")',
'dataProvider'=>(isset($dataProvider)?$dataProvider:$clients->moveclients()),
'template'=>"{items}\n{pager}",
'columns'=>array(
array(
'class'=>'CCheckBoxColumn',
'selectableRows'=>2,
'id'=>'clients',
),
'name',
'surname',
'telephone',
'email',
array(
'header'=>'Category',
'name' => 'category_title',
'type' => 'raw',
'value' => '$data->category->title',
),
),
)); ?>
Because this is a multi-step form, the cgridview dataprovider defaults to listing all clients ($clients->moveclients() lists all clients).
The ajax button posts the category_id to the client/filterclients url correctly.
I can see with firebug that actionFilterClients returns the rendered html correctly (with the correct clients) but the gridview is not updated...
Any ideas on why not?
In the end I added another view that had only a gridview in it and modified my code thus:
Controller:
$this->renderPartial('_ajax_transfer_step_3' , array('dataProvider'=>$dataProvider)) ;
Original view:
<?php $filter=$this->beginWidget('CActiveForm', array(
'id'=>'customer-filter-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('class'=>'form-horizontal'),
)); ?>
<label for="category_id">View customers in category:</label>
<?php
echo CHtml::dropDownList('category_id', '', Customer::customerCategories(),
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('filtercustomers'),
'data'=>'js:jQuery(this).serialize()',
'success'=>'function(response) {
jQuery("#customer-grid").html(response)
}',
)
)
);
?>
<?php $this->endWidget(); ?>
Now it just replaces a portion of the page.
I still haven't figured out why my original code didn't update the gridview though.

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',
),
)); ?>

How can you call a CJuiDialog in beforesave

I will appreciate if somebody could help me to find how to solve out one problem, I have a checkbox in my create form. If i pushed the create button I want to have a popup window if the checkbox is checked and do nothing if the checkbox is unchecked.
my codes in _form
<?php echo $form->checkBoxRow($model, 'default'); ?>
<div class="form-actions">
<?php $this->widget('bootstrap.widgets.TbButton', array(
'buttonType'=>'submit',
'type'=>'primary',
'label'=>$model->isNewRecord ? 'Create' : 'Save',
)); ?>
</div>
in my create controller
public function actionCreate()
{
$model=new EmpSched;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['EmpSched']))
{
$model->attributes=$_POST['EmpSched'];
if($model->save())
$this->redirect(array('view','id'=>$model->id_empsched));
}
$this->render('create',array(
'model'=>$model,
'emp'=> new CActiveDataProvider('schedule'),
));
}
in my create.php
<?php
$this->breadcrumbs=array(
'Emp Scheds'=>array('index'),
'Create',
);
$this->menu=array(
array('label'=>'List EmpSched','url'=>array('index')),
array('label'=>'Manage EmpSched','url'=>array('admin')),
);
?>
<h1>Create EmpSched</h1>
<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>
<?php $this->widget('bootstrap.widgets.TbGridView',array(
'id'=>'schedule-grid',
'dataProvider'=>$emp,
'columns'=>array(
'id_sched',
'sched_name',
array(
'name'=>'mon',
'value'=>'$data->fkidday->monday->name'
),
array(
'name'=>'tues',
'value'=>'$data->fkidday->tuesday->name'
),
array(
'name'=>'wed',
'value'=>'$data->fkidday->wednesday->name'
),
array(
'name'=>'thurs',
'value'=>'$data->fkidday->thursday->name'
),
array(
'name'=>'fri',
'value'=>'$data->fkidday->friday->name'
),
/*'sat',
'sun',
*/
),
));
?>
<script>
$(function() {
// when row is clicked
$('#schedule-grid tbody:first').on('click', 'tr', function() {
// get the ID
var id = $(this).find('td:first').text();
// select the same option in dropdown list with same value
$('#EmpSched_fk_id_sched')
.find("option[value="+ id +"]")
.prop("selected", "selected");
});
});
</script>
Add custom javascript (supposing the create button will submit the form)
$('#form-id').submit(function() {
if $(this).find('#check-box-id').is(':checked') {
// open popup here
}
return false;
});

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
}
);
});
")
?>