YII1 clistview data is empty - yii

I have a custom dropdown pagination in my project and using CListView to display the items, but it's not working correctly. Whatever the number that I select in the dropdownlist, or the page number, the content of the list is empty. I can see that the summary number is changed and correctly shown, but the list is not shown. What I am missing?
Below is the model - candidateSearch() code:
public function candidateSearch()
{
$criteria = new CDbCriteria;
$criteria->compare('can_id',$this->can_id);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array(
'defaultOrder'=>'name ASC',
),
'pagination'=>array(
'pageSize'=> Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']),
),
));
}
Below is the controller code:
public function actionIndex()
{
if (isset($_GET['pageSize']))
{
Yii::app()->user->setState('pageSize',(int)$_GET['pageSize']);
unset($_GET['pageSize']);
}
$model = new JbMeetGreet('candidateSearch');
$model->unsetAttributes(); // clear any default values
$model->can_id = Yii::app()->user->id;
$model->created_by = Yii::app()->user->id;
$model->created_by_lvl = Yii::app()->user->user_level;
//search function
if(isset($_POST['JbMeetGreet']))
{
$model->attributes = $_POST['JbMeetGreet'];
}
$this->render('index', array('model'=>$model));
}
Below is the index page code:
$pageSize = Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']);
echo CHtml::dropDownList('pageSize',$pageSize,array(5=>5,10=>10,15=>15,20=>20,25=>25,30=>30),
array('onchange'=>"$.fn.yiiListView.update('meet-greet-grid',{ data:{pageSize: $(this).val() }})",
'empty'=>'-- Select Page Range --','style'=>'width:100%;'));
$this->widget('zii.widgets.CListView',array(
'id'=>'meet-greet-grid',
'dataProvider'=>$model->candidateSearch(),
'itemView'=>'_profile_list',
'template'=>'{summary}{pager}{items}',
'summaryText'=>'Result {start} - {end} of {count} results',
});

You must add your dropDownList into your grid (to make ajax call to work):
// into your view
$pageSize = Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']);
$perPagina = CHtml::dropDownList('pageSize', $pageSize, array(5=>5,10=>10,15=>15,20=>20,25=>25,30=>30),
array('onchange' => "$.fn.yiiGridView.update('meet-greet-grid',{ data:{pageSize: $(this).val() }})")
);
$this->widget('zii.widgets.CListView',array(
'id'=>'meet-greet-grid',
// add this
'header' => $perPagina,
'dataProvider'=>$model->candidateSearch(),
'itemView'=>'_profile_list',
'template'=>'{summary}{pager}{items}',
'summaryText'=>'Result {start} - {end} of {count} results',
});

I managed to solve it. I have animation in my item view page. Deleted the animation code, and data in the subsequent pages are displayed. Thank you.

Related

yii dependent dropdown is not working

This is my view page
<?php echo $form->dropDownListRow($order,'area_id',Area::model()->getActiveAreaList(),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('currentController/getdeliveryforarea'),
'update'=>'#pickup_time_slot', //selector to update
'data' => array('area_id' => 'js:this.value',),
)));
?>
<?php echo $form->dropDownListRow($order,'pickup_time_slot',array(),array('prompt'=>'Select time')); ?>
and in my controll the getdeliveryforarea is like
public function actionGetdeliveryforarea()
{
$data=Areatimeslot::model()->findAll('area_id=:area_id',
array(':area_id'=>(int) $_POST['id']));
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
here my dependent dropdown is not working ,from getActiveAreaList i will get the area list in first dropdown and once i choose an area it must show corrosponding time list in second dropdown,i hope someone will help me out,thanks in advance
Use Juqery migrate plugin or try something like,
jQuery.browser = {};
(function () {
jQuery.browser.msie = false;
jQuery.browser.version = 0;
if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) {
jQuery.browser.msie = true;
jQuery.browser.version = RegExp.$1;
}
})();

How to upload multiple file at a time using TbActiveForm?

I am using TbActiveForm. I want upload multiple file at a time, Please help me.Thanks
In your TbActiveForm you have to use a widget for multiple file upload. You could write it yourself or use widget like CMultiFileUpload.
Here is reference for CMultiFileUpload.
Example for view:
<?php
$this->widget('CMultiFileUpload', array(
'model'=>$model,
'attribute'=>'photos',
'accept'=>'jpg|gif|png',
'options'=>array(),
'denied'=>'File is not allowed',
'max'=>10, // max 10 files
));
?>
Example for controller:
public function actionCreate()
{
$model = new Photo;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
$type = isset($_GET['type']) ? $_GET['type'] : 'post';
if (isset($_POST['Photo'])) {
$model->attributes = $_POST['Photo'];
$photos = CUploadedFile::getInstancesByName('photos');
// proceed if the images have been set
if (isset($photos) && count($photos) > 0) {
// go through each uploaded image
foreach ($photos as $image => $pic) {
echo $pic->name.'<br />';
if ($pic->saveAs(Yii::getPathOfAlias('webroot').'/photos/path/'.$pic->name)) {
// add it to the main model now
$img_add = new Photo();
$img_add->filename = $pic->name; //it might be $img_add->name for you, filename is just what I chose to call it in my model
$img_add->topic_id = $model->id; // this links your picture model to the main model (like your user, or profile model)
$img_add->save(); // DONE
}
else{
echo 'Cannot upload!'
}
}
}
if ($model->save())
$this->redirect(array('update', 'id' => $model->id));
}
$this->render('create', array(
'model' => $model,
));
}
Source reference.

how to update a record with file uploaded already?

I want to UPDATE record without losing the File that i have uploaded. Below is my create action.
public function actionCreate()
{
$model = new Page;
if (isset($_POST['Page']))
{
$model->attributes = $_POST['Page'];
$model->filename = CUploadedFile::getInstance($model, 'filename');
if ($model->save())
{
if ($model->filename !== null)
{
$dest = Yii::getPathOfAlias('application.uploads');
$model->filename->saveAs($dest . '/' . $model->filename->name);
$model->save();
}
$this->redirect(array('view', 'id' => $model->id));
}
}
$this->render('create', array(
'model' => $model,
));
}
In yii, update method is designed to update the values that you are posting from your form. It will not update all Model properties.
For suppose, your Page model has 4 model properties, assume all are mandatory.
title
description
keywords
image
Since you don't want to update image field, you can make it non mandatory field by setting scenario for image field in your Page model rules.
class Page extends CActiveRecord
{
/*
Coding
*/
public function rules()
{
return array(
array('title, description,keywords', 'required'),
array('image', 'file', 'types'=>'jpg, png'),
array('image', 'required', 'on' => 'insert'),
// => You need image field to be entered on Create, not on Update
.................
}
}
So, Show file input in your form on Create, Hide on Update action so that image field won't submit.
//Your page form
<?php if($model->isNewRecord):?>
echo $form->labelEx($model, 'image');
echo $form->fileField($model, 'image');
echo $form->error($model, 'image');
<?php endif;?>
Since your are using Yii-2 you can use skippOnEmpty validator for the same.

Yii Model with combined tables using relations in Active Record

I've got the model class Contactindiv with relations and search as follows.
public function relations()
{
return array(
'contactlogs' => array(self::HAS_MANY, 'Contactlog', 'ContactIndivID'),
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('ContactIndivID',$this->ContactIndivID);
$criteria->compare('PersonalTitle',$this->PersonalTitle,true);
$criteria->compare('NameLast',$this->NameLast,true);
$criteria->compare('NameMiddle',$this->NameMiddle,true);
$criteria->compare('NameFirst',$this->NameFirst,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
The current page shows the data in a searchable CGridView format.
My goal is to combine the 'contactlogs' from relations into the Model in order to have it show up on the page in a searchable fashion in the GridView. Basically add a searchable GridView column for each contact showing their contact log.
Thanks ahead of time for your help!
For your first goal (show contactlogs in model) you can write a getter in your main model. It depends, what you want to show in your gridview column but you could use something like:
public function getContacts()
{
$names = array();
foreach($this->contactlogs as $log)
$names[] = $log->name;
return implode(', ', $names);
}
Now you can use contacts as if it where a regular attribute of your "Contactindiv" model.
For your second goal you could add a public property which will contain the filter value, and which you can use in your search() method:
public $contactFilter;
public function search()
{
// ...
if(!empty($this->contactFilter)) {
$criteria->with = array(
'contactlogs' => array(
'joinType' => 'INNER JOIN',
'on' => 'contactlogs.name = :name',
),
);
$criteria->params[':name'] = $this->contactFilter;
}
// ..
}
Now you only need to add all the above in your gridview's columns configuration:
array(
'name' => 'contacts',
'filter' => CHtml::activeTextField($model, 'contactFilter'),
)
Please note, that i'm writing most of this from the top of my head and couldn't fully test it. But it should hopefully make the basic concept clear to you. Please let me know if it works.

Show "page=1" params in Yii CLinkPager first page link

I want to make a CListView that remembers search form input. My application saves every search input (and the current page viewed by user) into user session and add the search id into the pagination params.
$pagination = array(
'pageVar' => 'page',
'currentPage' => $currentPage - 1,
'params' => array(
'search' => $searchId,
)
);
But since the first page link at the CLinkPager don't show 'page=1' parameter, everytime I click the link from other page (say page 2), the application will think that I need the current 'search' (whose current page value is 2).
To rephrase the question, is there any way to make CLinkPager shows param 'page=1' at the first page link? Or do you have another suggestion on handling this :)
Thanks
Edited
My overall code is below. If you want to give a better suggestion, please do :)
public function actionIndex() {
$model = new User();
if (isset($_POST['User'])) {
$model->setAttributes($_POST['User']);
} else {
if (isset($_GET['search'])) {
$searchId = $_GET['search'];
//Just getting a variable stored in the session
//array(
// $searchId => array(
// 'model' => $model //the search model
// 'pageSize' => $pageSize //current pagination page size
// 'currentPage' => $currentPage //the search model
// )
//);
$search = Yii::app()->user->getSearch($searchId);
$model->setAttributes($search['model'], false);
$currentPage = $search['currentPage'];
$pageSize = $search['pageSize'];
} else {
//create default search
}
}
//Managing pageSize parameter
if (isset($_GET['pageSize'])) { //Check whether there is pageSize in $_GET parameters
if (in_array($_GET['pageSize'], $this->getPageSizes())) {
$pageSize = $_GET['pageSize'];
} else {
$pageSize = self::DEFAULT_SEARCH_PAGESIZE;
}
} else if (!isset($pageSize)) { //Check whether pageSize is already defined before
$pageSize = self::DEFAULT_SEARCH_PAGESIZE;
}
//Managing currentPage parameter
if (isset($_GET['page'])) { //Check whether there is page parameter in $_GET parameter
$currentPage = $_GET['page'];
} else if (!isset($currentPage)) { //Check whether page is already defined before
$currentPage = self::DEFAULT_CURRENT_PAGE;
}
//Saving all into new session
$newSearch = array(
'model' => $model->getAttributes(),
'currentPage' => $currentPage,
'pageSize' => $pageSize,
);
//Check whether if there is a search before and if it exists check whether the two search is the same
if (!isset($search) || (serialize($search) != serialize($newSearch))) {
//Saves into session
$searchId = Yii::app()->user->saveSearch($newSearch);
} //else use the old search id
//-----------------------------------------------------
//Building the search
//Setting criteria
$criteria = new CDbCriteria(array(
'select' => array(
'*', //just to simplify things
),
));
//Appending criteria to search model used
$criteria = $model->search($criteria);
//Appending pagination to current variables
$pagination = array(
'pageSize' => $pageSize,
'pageVar' => 'page',
'currentPage' => $currentPage - 1,
'params' => array(
'search' => $searchId, //include the search ID in the link pager request URL
)
);
//Creating dataProvider for the search
$dataProvider = new CActiveDataProvider('Property', array(
'criteria' => $criteria,
'pagination' => $pagination,
));
//Rendering
$this->render('index', array(
'dataProvider' => $dataProvider,
'pageSize' => $pageSize,
'model' => $model,
'searchId' => $searchId,
));
}
You could probably extend CPagination by modifying the createPageUrl method and remove/change the if($page>0) condition to always be true. (Or better yet, add an additional parameter to turn on/off that behavior.)