search function in yii gridview - php

I have project implemented in Yii. Grid view search function is not working in server. In localhost Grid view search function is working. What could be issue in that. please suggest me where should i change.
my Recipe controller:
public function actionAdmin()
{
$model=new Recipe('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Recipe']))
$model->attributes=$_GET['Recipe'];
$this->render('admin',array(
'model'=>$model,
));
}
My model part:
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$pagination=array('pageSize'=>'10');
$criteria->compare('recipe_id',$this->recipe_id);
$criteria->compare('posted_id',$this->posted_id,true);
$criteria->compare('name',$this->name,true);
$criteria->compare('type',$this->type);
$criteria->compare('cuisinename',$this->cuisinename);
$criteria->compare('course_id',$this->course_id);
$criteria->compare('details',$this->details,true);
$criteria->compare('serving_size',$this->serving_size,true);
$criteria->compare('calorie_count',$this->calorie_count);
$criteria->compare('preparation_time',$this->preparation_time);
$criteria->compare('cooking_instructions',$this->cooking_instructions,true);
$criteria->compare('garnishing_instructions',$this->garnishing_instructions,true);
$criteria->compare('serving_instructions',$this->serving_instructions,true);
$criteria->compare('recipe_image',$this->recipe_image,true);
$criteria->compare('recipe_small_image',$this->recipe_small_image,true);
$criteria->compare('status',$this->status,true);
$criteria->compare('posting_time',$this->posting_time,true);
$criteria->compare('pLike',$this->pLike);
$criteria->compare('pDislike',$this->pDislike);
$criteria->compare('images',$this->images,true);
$criteria->compare('Ingredient_TypeId',$this->Ingredient_TypeId,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,'pagination'=>$pagination
));
}
my view part:
<?php $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'mygridview',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
//'id',
'name',
array(
'class'=>'CButtonColumn','template'=>'{update}{delete}',
),
),
)); ?>

Handle your javascript following way, there will be no double jquery problem after this, add all other js in the 'js' array
$baseurl = Yii::app()->baseUrl;
$cs = Yii::app()->clientScript;
$cs->scriptMap = array('jquery.min.js'=>$baseurl.'/js/jquery-2.0.3.min.js', 'jquery.js'=>$baseurl.'/js/jquery-2.0.3.min.js');
$cs->registerCoreScript('jquery');
Yii::app()->clientScript->addPackage('other-required-scripts', array(
'baseUrl'=>$baseurl,
'js'=>array(
"js/abc.js",
"js/bcd.js",
//...
),
'depends'=>array('jquery')
));
Yii::app()->clientScript->registerPackage('other-required-scripts');

Related

How to define model parameter value in Yii?

I have a user table in which i have multiple roles and i am showing different user on different action by using same model search function. By this
public actionAdmin(){
$model = new User('search')
$model->unsetAttributes();
$model->userRole = UmsConfing::ADMIN;
if(isset($_GET('User')))
$model->attributes = $_GET['User'];
$this->render('userlist',arary('model'=>$model));
}
I am using this function for different roles .This works well. but now i want to show admin and operation user in same list i tried
$model->userRole = UmsConfig::ADMIN || UmsConfig:: OPERATION
but i didn't work Please help.
Please you can try this below code:
public actionAdmin(){
$model = new User('search')
$model->unsetAttributes();
if(isset($_GET('User')))
$model->attributes = $_GET['User'];
$this->render('userlist',arary('model'=>$model));
}
Below codeadd in User Model Search Function:
model/User.php
public function search() {
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria = new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->addInCondition('userRole', array (UmsConfig::ADMIN,UmsConfig:: OPERATION));
$criteria->compare('status',$this->status);
$criteria->compare('is_deleted',$this->is_deleted);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => yii::app()->params->pagesize,
),
'sort'=>array(
'defaultOrder'=>array(
'id'=>CSort::SORT_DESC
),
),
));
}

Modify Data that is put into CGridView - Yii

I'm working on a project in Yii where I would like the user to be able to update information on his mobile "accounts". The database I use has multiple accounts for each person (and, for that matter, has multiple individuals who will use the system). Now, when the user goes to "manage his account" he is able to manage all of the accounts of the database (not just his own).
Currently the code in the controller looks like this
public function actionAdmin()
{
$model =new Account('search');
$userId = Login::model()->getUserId();
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Account']))
{
$model->attributes=$_GET['Account'];
}
$this->render('admin',array(
'model'=>$model,
));
}
UserId is the the id that I would like to restrict that data to. Something along the lines of
$account = Account::model()->findAll(array("condition"=>"user_id = $userId"));
I'm not sure how exactly to go through with this. I've looked around and I know that there are "Criteria" that I could update in the model and there is also a "Filter function in the view". Should I be using one of these two to limit the accounts shown? Or can I do something directly from the controller?
Here is the code in the view
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'account-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'user_id',
'name',
'mobile_comp',
'msisdn',
'pin',
'balance',
/*
'company',
*/
array(
'class'=>'CButtonColumn',
),
),
)); ?>
And the model
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('name',$this->name,true);
$criteria->compare('mobile_comp',$this->mobile_comp,true);
$criteria->compare('msisdn',$this->msisdn);
$criteria->compare('pin',$this->pin);
$criteria->compare('company',$this->company,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
Change your actionAdmin to the following
public function actionAdmin()
{
$model =new Account('search');
$userId = Login::model()->getUserId();
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Account']))
{
$model->attributes=$_GET['Account'];
}
$model->user_id = $userId;
$this->render('admin',array(
'model'=>$model,
));
}
The above code will load only the current user record.
Just Solved - Different way than Think Different:
In my model I added a condition
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$userId = Login::model()->getUserId();
$criteria->addCondition(array("condtion"=>"user_id = $userId"));
$criteria->compare('name',$this->name,true);
$criteria->compare('mobile_comp',$this->mobile_comp,true);
$criteria->compare('msisdn',$this->msisdn);
$criteria->compare('pin',$this->pin);
$criteria->compare('company',$this->company,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}

Page size dropdownlist and CListView not working in yii

I am traying to make a dropdown pagination in my project, but it's not working correctly.
Whatever the number that I select in the dropdownlist, it still shows 10 items in the page.
What I am missing ?
Thank you.
Screenshot
The view code :
<?php
//$dataProvider->pagination->pageSize = 25;
$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.yiiGridView.update('packages-grid',{ data:{pageSize: $(this).val() }})",
'empty'=>'-- Select Page Range --','style'=>'width:198px;'));
$this->widget('zii.widgets.CListView', array(
'id'=>'propertylistview',
'dataProvider'=>$dataProvider,
'summaryText'=>'',
'itemView'=>'_propertyview',
'sortableAttributes' => array(
.........
)
));
?>
The controller code :
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,
));
}
I already added this to model search
return new CActiveDataProvider(get_class($this),array(
'pagination'=>array(
'pageSize'=> Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),
),
'criteria'=>$criteria,
));
and this to config/main.php
'params'=>array('defaultPageSize'=> '10'),
You're doing an ajax call but in your widget you do not state any update part to update. ajaxUpdate is the solution.
And you are using 'onchange'=>"$.fn.yiiGridView.update while using a CListView so change the js to 'onchange'=>"$.fn.yiiListView.update and:
<?php
// your code
$this->widget('zii.widgets.CListView', array(
'id'=>'propertylistview',
'ajaxUpdate' => 'propertylistview', //add this line
'dataProvider'=>$dataProvider,
'summaryText'=>'',
'itemView'=>'_propertyview',
'sortableAttributes' => array(
//etc.. )
));
?>

Ajax filtering not working in CGridView in Yii (also the advanced search form does not open on clicking 'Advanced Search')

I have just started to work on Yii and facing some issues on CGridView..
the ajax filter is not working in the grid view.. when i chceked the console i see that no ajax request is sent.
this is my view (admin.php)
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$('#user-grid').yiiGridView('update', {
data: $(this).serialize()
});
return false;
});
");
?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'email_id',
'name',
'user_type',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
model file(User.php)
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('email_id',$this->email_id,true);
$criteria->compare('name',$this->name,true);
//$criteria->compare('password',$this->password,true);
$criteria->compare('user_type',$this->user_type);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array('pageSize'=>3),
));
}
and inside rules()
array('id, email_id, name, user_type', 'safe', 'on'=>'search'),
in controller file (UserController.php)
public function actionAdmin()
{
$model=new User('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['User']))
$model->attributes=$_GET['User'];
$this->render('admin',array(
'model'=>$model,
));
}
Also advanced search form does not open on clicking 'Advanced Search'..
I have searched a number of threads related to it but its not helping..
pls help me in identifying the problem..
Regards Leo
Debuging tip: If something depending on the JavaScript is not working, 99% you're getting a JavaScript error which terminates the rest of JavaScript.
I had same error some time ago.
It was because I had included my own jquery declaration at the bottom of my page
and there was a conflict with YII auto script management, that includes jquery declaration in the head
of the page.
You should be able to see that by looking at the generated source code of your page...
Good luck

Yii CGridview not filtering

I'm trying to work with a YII CGridview to display some data.
This is home my model search function looks like:
/**
* Retrieves a list of models based on the current search/filter conditions.
* #return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('ip',$this->ip,true);
$criteria->compare('first_use',$this->first_use,true);
$criteria->compare('last_use',$this->last_use);
$criteria->compare('memberid',$this->memberid);
$criteria->compare('countryid',$this->countryid);
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
}
And this is how my view looks like
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'iplog-grid',
'dataProvider'=>$oIPLog->search(),
'filter'=>$oIPLog,
'summaryText' => 'showing you {start} - {end} of {count} logged Ips',
'columns'=>array(
array(
'name'=>'ip',
'type'=>'raw',
),
array(
'name'=>'first_use',
'type'=>'datetime',
),
array(
'name'=>'last_use',
'type'=>'datetime',
),
),
));
Displaying the CGridview works, but I can't seem to get the filter on top of it to work. It sends the call and I don't get any error as reponse, it just returns the whole unfiltered data again..
Any clues?
And how exactly does your controller look like?
For the CGridview filter to work you need to check in your controller if there are any filters set and then return the filtered object.
To clarify, something like this should be placed into your controller action
$oObject = new Object('search');
if (isset($_GET['Object'])) {
$oObject->attributes = $_GET['Object'];
}
Hope this helps
You Have to apply these points:
1.Specify the global variable($_REQUEST) in function of your controller
for example
$model = new User('search');
$model->unsetAttributes(); // clear any default values
if (isset($_REQUEST['User'])){
$model->attributes = $_REQUEST['User'];
}
$this->render('admin', array(
'model' => $model,
));
Set method type in search form
<?php $form=$this->beginWidget('CActiveForm', array(
'action'=>Yii::app()->createUrl('user/admin'),
'method'=>'POST',
)); ?>
3.In Cgrid view you have to define the url like
'ajaxUrl'=>Yii::app()->createUrl( 'controller/function' ),

Categories