How to Make Drop down pagination in Yii framework? - yii

I want to make Drop down pagination in yii frame work, any sugession will be helpfull. thanks in advance.

You can use this code..
In your contrller:
public function actionAdmin(){
if (isset($_GET['pageSize'])) {
Yii::app()->user->setState('pageSize',(int)$_GET['pageSize']);
unset($_GET['pageSize']);}
$model=new Packages('search');
$model->unsetAttributes();
if(isset($_GET['Packages']))
$model->attributes=$_GET['Packages'];
$dataProvider=new CActiveDataProvider('Packages');
$this->render('admin',array('model'=>$model,'dataProvider'=>$dataProvider,
));
}
and in model search function use this
return new CActiveDataProvider(get_class($this),array(
'pagination'=>array(
'pageSize'=> Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),
),
'criteria'=>$criteria,
));
in your view use this
$pageSize=Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']);
echo CHtml::dropDownList('pageSize',$pageSize,array(10=>10,20=>20,50=>50,100=>100,500=>500,1000=>1000),array('onchange'=>"$.fn.yiiGridView.update('packages-grid',{ data:{pageSize: $(this).val() }})",'empty'=>'-- Select Page Range --','style'=>'width:198px;'));
now dropdown pagination implemented in your yii project.

Related

Clistview With Pagination in Yii

I don't get along with pagination for one of my CListView. In my controller I fetch records to a CActivedataProvider by using a criteria and I pass that data provider to my CListView. I can see that database fetch is working fine since I can see all records in my CListView if I don't apply pagination.
The problem begins when pagination is turned on. I would like to see all records on a separate page with a pagination at the bottom.
What I'm doing wrong?
Thanks in advance,
Here is what I have in my controller:
if(isset($_POST['Lista']))
{
$model->attributes=$_POST['Lista'];
}
$egyedi_osszetevo_idk = Lista::getEgyediOsszetevoIdk($model->datum_tol, $model->datum_ig);
$criteria = new CDbCriteria();
$criteria->addInCondition("id", $egyedi_osszetevo_idk);
$count = EtelOsszetevo::model()->count($criteria);
$pages=new CPagination($count);
$pages->pageSize=1;
$pages->applyLimit($criteria);
$dataProviderEgyediOsszetevok = new CActiveDataProvider('EtelOsszetevo', array(
'criteria'=>$criteria,
'pagination'=>$pages,
));
$this->render('view',array('model'=>$model,
'egyedi_osszetevo_idk'=>$dataProviderEgyediOsszetevok,
));
Here is my CListView in my view file:
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$egyedi_osszetevo_idk,
'itemView'=>'_view',
));

Yii load datepicker in partial view

Trying to load a block with a datepicker from a view.. but it loads only the textfield without datepicking feature. What am I doing wrong?
The create view
<?php echo $form->dropDownList($model,'operation_type',CHtml::listData(OperationType::model()->findAll(),
'id','name'),array(
'class'=>'span3',
'empty'=>'----- Type -----',
'id'=>'idOpType',
'ajax'=>array(
'type'=>'POST',
'url'=>CController::createUrl('operations/meta'),
//'dataType'=>'json',
'data'=>array('idOpType'=>'js:this.options[this.selectedIndex].innerHTML'),
'success'=>'function(data){
$("#opTypeBlock").html(data);
}',
),
)); ?>
The Controller Action
public function actionMeta(){
$data= new OperationsMeta();
$this->renderPartial('_meta',array('model'=>$data));
}
The view I am trying to load
<p>Select due date</p>
<?php $this->widget('zii.widgets.jui.CJuiDatePicker',array(
'attribute'=>'param',
'options'=>array(
'showAnim'=>'fold',
),
'model'=>$model,
'htmlOptions'=>array(
'style'=>'height:20px;',
'class'=>'inline',
'id'=>'datepickerOpType',
),
));
?>
Try
$this->renderPartial('_meta',array('model'=>$data), false, true);
so it loads JS files.
Use renderAjax() which injects into the rendering result with JS/CSS scripts and files which are registered with the view, as stated here.

CGridview filter on page load with pre define value in search field

I am working with the Yii framework.
I have set a value in one of my cgridview filter fields using:
Here is my jQuery to assign a value to the searchfield:
$('#gridviewid').find('input[type=text],textarea,select').filter(':visible:first').val('".$_GET['value']."');
And here my PHP for calling the cgridview:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'bills-grid',
'dataProvider'=>$dataProvider,
'filter'=>$model,
'cssFile'=>Yii::app()->baseUrl . '/css/gridview.css',
'pager'=>array(
'class'=>'AjaxList',
'maxButtonCount'=>25,
'header'=>''
),
'columns' => $dialog->columns(),
'template'=>"<div class=\"tools\">".$dialog->link()." ".CHtml::link($xcel.' Export to excel', array('ExcelAll'))."</div><br />{items}{summary}<div class=\"pager-fix\">{pager}</div>",));
The value appears in the search field and my cgridview works correctly without any issues, but I am unable to trigger the cgridview to refresh or filter. Does anyone know who to trigger the cgridview to filter after page load with a predefined value?
Any help would be greatly appreciated and please let me know if you need additional information.
Thank you.
You can solve the problem without any clientside code modification. In your controller action just set the default value for the attribute as shown below
public function actionAdmin()
{
$model = new Bills();
$model->unsetAttributes();
$model->attribute_name="default filter value";//where attribute_name is the attribute for which you want the default value in the filter search field
if(isset($_GET['Bills'])){
$model->attributes = $_GET['Bills'];
}
$this->render('admin',array('model'=>$model));
}
Have a look at 'default' index action that gii generates:
public function actionIndex()
{
$model = new Bills();
$model->unsetAttributes();
if(isset($_GET['Bills'])){
$model->attributes = $_GET['Bills'];
}
$this->render('index',array('model'=>$model));
}
So if you add one line like: $model->attribute = 'test';, you're done. 'attribute' is of course the attribute that has to have the default filter value (in this case value is 'test') :). So your code looks like:
public function actionIndex()
{
$model = new Bills();
$model->unsetAttributes();
if(isset($_GET['Bills'])){
$model->attributes = $_GET['Bills'];
}
if(!isset($_GET['Bills']['attribute']) {
$model->attribute = 'test';
}
$this->render('index',array('model'=>$model));
}
Of course youre attribute will have a test value (in filter) set up as long as you wont type anything in its filter field. I hope that that's what you're looking for. Your filter should work as always.
Sorry for my bad english :)
Regards
You can use Yii's update:
$.fn.yiiGridView.update('bills-grid', {
type: 'GET',
url: <?php echo Yii::app()->createUrl('controller/action') ?>"?Class[attribute]=<?php echo $_GET['value'] ?>
success: function() {
$.fn.yiiGridView.update('bills-grid');
}
});
This is how i do it, just change the URL, it should be the same controller action of the gridview and change URL parameters to the structure represented in there, should be like Bills[attribute]=value.

Data saved as null when using RadioButtonList or dropDownList

I'm new to yii. I have this problem where my data stored in radiobuttonlist or dropDownList is not saved in the database. It always shows as null. here's my code
View:
<?php
$form = $this->beginWidget('CActiveForm');
echo $form->label($model,'gender');
echo $form->radioButtonList($model,'gender',array('M'=>'Male','F'=>'Female'));
echo $form->label($model,'cat');
echo $form->dropDownList($model,'cat',$category);
echo CHtml::submitButton('Submit');
$this->endWidget();
?>
Controller:
public function actionCreate()
{
$model=new Test;
if(isset($_POST['Test']))
{
$model->attributes=$_POST['Test'];
if($model->save()){
$this->redirect(array('index'));
}
else
var_dump($model->errors);
}
$cat = array('st'=>'STAFF','ot'=>'OTHERS');
$model->gender='M';
$this->render('create',array(
'model'=>$model,'category'=>$cat
));
}
Kindly help... Thanks in advance
EDIT: After adding the required in the rule section it works like a charm
Well here's the modified Test model
public function rules()
{
return array(
array('gender,cat', 'required'),
array('name', 'length', 'max'=>45),
);
}
Post your model here.I think your problem is in Test model.
I see you solved it using 'required', but if there are some fields that are not mandatory, you can just use the 'safe' rule. The point is that every attribute of your form has to be on the rules of your model.
Have a look to Understanding "Safe" Validation Rules.

Yii CListview -> pagination and AjaxLink/ajaxButton

I have problems regarding with pagination and Ajax form.
Here is my code for Controller:
$dataProvider = new CActiveDataProvider('User', array(
'pagination'=>array(
'pageSize'=>10,
),
));
$this->render('users',array(
'dataProvider'=>$dataProvider,
));
For view -> users:
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_user',
);
For render _users:
echo CHtml::ajaxLink($text, $this->createUrl('admin/deleteuser',array('id'=>$data->iduser)), array('success'=>"js:function(html){ alert('remove') }"), array('confirm'=>_t('Are you sure you want to delete this user?'), 'class'=>'delete-icon','id'=>'x'.$viewid));
if i have 15 rows in a database it will only show 10 and will generate a pagination (ajaxUpdate = true) for next 5. The first 10 rows has no problem with the ajaxLink because the clientscript was generated but problem is the when I move to the next page, the ajaxLink is not working because its not generated by the pagination .
any idea? thanks
An alternate method, check this post in the yii forum. So your code will become like this:
echo CHtml::link($text,
$this->createUrl('admin/deleteuser',array('id'=>$data->iduser)),
array(// for htmlOptions
'onclick'=>' {'.CHtml::ajax( array(
'beforeSend'=>'js:function(){if(confirm("Are you sure you want to delete?"))return true;else return false;}',
'success'=>"js:function(html){ alert('removed'); }")).
'return false;}',// returning false prevents the default navigation to another url on a new page
'class'=>'delete-icon',
'id'=>'x'.$viewid)
);
Your confirm is moved to jquery's ajax function's beforeSend callback. If we return false from beforeSend, the ajax call doesn't occur.
Another suggestion, you should use post variables instead of get, and also if you can, move the ajax call to a function in the index view, and just include calls to the function from the all links' onclick event.
Hope this helps.
Not fully sure, but given past experiences I think the problem is in the listview widget itself.
If the owner of the widget is a Controller, it uses renderPartial to render the item view.
Renderpartial has, as you may or may not know, a "processOutput" parameter which needs to be set to TRUE for most of the AJAX magic (its FALSE by default).
So perhaps you can try to just derive a class of the listview and add a copy in there of "renderItems()". There you would have to change it so that it calls renderPartial with the correct parameters.
In the widget Clistview, I added afterAjaxUpdate
$jsfunction = <<< EOS
js:function(){
$('.delete-icon').live('click',function(){
if(confirm('Are you sure you want to delete?'))
{
deleteUrl = $(this).attr('data-href');
jQuery.ajax({'success':function(html){ },'url':deleteUrl,'cache':false});
return false;
}
else
return false;
});
}
EOS;
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'id'=>'subscriptionDiv',
'itemView'=>'_subscription',
'afterAjaxUpdate'=>$jsfunction,
'viewData'=>array('is_user'=>$is_user,'all'=>$all),
'htmlOptions'=>($dataProvider->getData()) ? array('class'=>'table') : array('class'=>'table center'),
)
);
and in the _user just added attribute data-href
<?php echo CHtml::ajaxLink($text, $this->createUrl('admin/deletesubscription',array('id'=>$data->idsubscription)),
array('success'=>"js:function(html){ $('#tr{$viewid}').remove(); }"),
array('confirm'=>_t('Are you sure you want to unsubscribe?'),
'class'=>'delete-icon',
'id'=>'x'.$viewid,
'data-href'=>$this->createUrl('admin/deletesubscription',array('id'=>$data->idsubscription))
)); ?>
try
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_user',
'enablePagination' => true,
);
if this doesn't solve, try including the total number of records in the data provider options as -
'itemCount' => .....,