Yii Framework Forum: Cgridview Didn't Work Dropdown Filter - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Cgridview Didn't Work Dropdown Filter troubles with validation in model Rate Topic: -----

#1 User is offline   terkin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 07-October 12

Posted 07 October 2012 - 03:10 PM

$this->client_id didn't pass the validation, when I change $criteria->compare('client_id',$this->client_id); to $criteria->compare('client_id',$_REQUEST['Tasks']['client_id']); in search() all work fine, but how to fix my code?

My database relations in attach


My model:
public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('client_id, task', 'required'),
            array('client_id, complete', 'numerical', 'integerOnly'=>true),
            array('task', 'length', 'max'=>5000),
            array('start_date','default',
                'value'=>new CDbExpression('NOW()'),
                'setOnEmpty'=>false,'on'=>'insert'),
            array('task_id, client_id, task, start_date, end_date, complete', 'safe', 'on'=>'search'),
        );
 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(
            'client' => array(self::BELONGS_TO, 'Client', 'client_id'),
        );
    }

    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;
        $criteria->with = array('client');
        $criteria->compare('task_id',$this->task_id);
        $criteria->compare('client_id',$this->client_id);
        $criteria->compare('name',$this->client->name);
        $criteria->compare('task',$this->task,true);
        $criteria->compare('start_date',$this->start_date,true);
        $criteria->compare('end_date',$this->end_date,true);
        $criteria->compare('complete',$this->complete);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }


and my view:
$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$model->search(),
    'filter' => $model,
    'columns'=>array(
        array(
            'name' => 'client_id',
            'filter' => CHtml::listData(Client::model()->findAll(), 'client_id', 'name'),
            'value'=>'$data->client->name'
        ),
        'task'
     )
));

Attached File(s)

  • Attached File  22.jpg (15.96K)
    Number of downloads: 11

0

#2 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 677
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 07 October 2012 - 05:03 PM

what validation error you get?
0

#3 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 545
  • Joined: 09-April 10

Posted 07 October 2012 - 10:22 PM

Try this - taken from here
$criteria->with = array('task');
$criteria->compare('task.id', $this->task_id);


Matt
0

#4 User is offline   terkin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 07-October 12

Posted 08 October 2012 - 01:03 AM

I have not any validation error, when I changed value in dropdown filter - the data in table didn't changed. I tried trick from wiki, but also no result.
0

#5 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 545
  • Joined: 09-April 10

Posted 08 October 2012 - 11:22 AM

Post your Controller and View code, please.
0

#6 User is offline   terkin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 07-October 12

Posted 09 October 2012 - 01:44 AM

View Postwaterloomatt, on 08 October 2012 - 11:22 AM, said:

Post your Controller and View code, please.

Controller
<?php

class TasksController extends Controller
{
	public function actionIndex()
	{
        $model=new Tasks;
		$this->render('index',array(
            'model'=>$model
        ));
	}

    public function actionCreate()
    {
        $model=new Tasks;
        if(isset($_POST['Tasks']))
        {
            $model->attributes=$_POST['Tasks'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->task_id));
        }

        $this->render('index',array(
            'model'=>$model,
        ));
    }

    public function actionView($id){
        $this->render('view',array(
            'model'=>$this->loadModel($id),
        ));
    }

    public function actionFinish(){
        if(isset($_POST['Tasks']['task_id'])) {
            $id=$_POST['Tasks']['task_id'];
            $model=$this->loadModel($id);
            $model->complete=1;
            $model->end_date = new CDbExpression('NOW()');
            if($model->save())
                $this->redirect(array('view','id'=>$model->task_id));

        }

    }

    public function loadModel($id)
    {
        $model=Tasks::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}


View
<?php
/* @var $this TasksController */

$this->breadcrumbs=array(
	'Tasks',
);
$this->beginWidget('CActiveForm', array(
    'id'=>'user-form',
    'enableAjaxValidation'=>false,
    'htmlOptions'=>array('enctype'=>'multipart/form-data'),
));
echo CHtml::errorSummary($model);
$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$model->search(),
    'filter' => $model,
    'columns'=>array(
        array(
            'name' => 'client_search',
            'filter' => CHtml::listData(Client::model()->findAll(), 'client_id', 'name'),
            'value'=>function($data) {
                return $data->client->name;
            }
        ),
        'task'
     )
));


        echo CHtml::submitButton('Login');


$this->endWidget('CActiveForm');


Model
<?php

/**
 * This is the model class for table "tasks".
 *
 * The followings are the available columns in table 'tasks':
 * @property integer $task_id
 * @property integer $client_id
 * @property string $task
 * @property string $start_date
 * @property string $end_date
 * @property integer $complete
 *
 * The followings are the available model relations:
 * @property Client $client
 */
class Tasks extends CActiveRecord
{
    public $client_search;


	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Tasks the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tasks';
	}

    public function finish(){
        echo    'www';
    }

        /**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('client_id, task', 'required'),
			array('client_id, complete', 'numerical', 'integerOnly'=>true),
			array('task', 'length', 'max'=>5000),
            array('start_date','default',
                'value'=>new CDbExpression('NOW()'),
                'setOnEmpty'=>false,'on'=>'insert'),
			array('task_id, client_search, task, start_date, end_date, complete,client_id', 'safe', 'on'=>'search'),
		);

	}

    public function getClient_Search() {
        return $this->client_search;
    }
	/**
	 * @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(
			'client' => array(self::BELONGS_TO, 'Client', 'client_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'task_id' => 'Task',
			'client_id' => 'Client',
			'task' => 'Task',
			'start_date' => 'Start Date',
			'end_date' => 'End Date',
			'complete' => 'Complete',
            'client_search'  => 'Клиент'
		);
	}

	/**
	 * 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()
	{

		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
        $criteria->with = array('client');
		$criteria->compare('t.task_id',$this->task_id);
		#$criteria->compare('client_id',$this->client_id);
        $criteria->compare('t.client_id', $this->client_search);
        $criteria->compare('t.name',$this->client->name);
		$criteria->compare('t.task',$this->task,true);
		$criteria->compare('t.start_date',$this->start_date,true);
		$criteria->compare('t.end_date',$this->end_date,true);
		$criteria->compare('t.complete',$this->complete);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
            'sort'=>array(
                'attributes'=>array(
                    'client_search'=>array(
                        'asc'=>'client.client_id',
                        'desc'=>'client.client_id DESC',
                    ),
                    '*',
                ),
            ),
		));
	}
}

0

#7 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 545
  • Joined: 09-April 10

Posted 09 October 2012 - 02:45 AM

Which controller action is rendering this view? If you look at a typical Admin action, it collects $_GET variables and populates the model, thereby creating a filter. My point is, I don't see any action that collects data and populates a model - except for actionCreate; but it's doing a redirect so I assume it's not that one.


public function actionAdmin()
{
    $model = new Inventory('search');

    $model->unsetAttributes(); // Clear default values

    if (isset($_GET['Inventory']))
        $model->attributes = $_GET['Inventory']; // This is where the filtering is being done

    $this->render('admin', array(
        'model' => $model
    ));
}



Matt
0

#8 User is offline   terkin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 07-October 12

Posted 09 October 2012 - 03:12 AM

View Postwaterloomatt, on 09 October 2012 - 02:45 AM, said:

Which controller action is rendering this view? If you look at a typical Admin action, it collects $_GET variables and populates the model, thereby creating a filter. My point is, I don't see any action that collects data and populates a model - except for actionCreate; but it's doing a redirect so I assume it's not that one.
In my controller actionIndex action render my view. I think I work with model without controller, look at 'dataProvider'=>$model->search(), when I change dropdown list it make AJAX request.

public function actionAdmin()
{
    $model = new Inventory('search');

    $model->unsetAttributes(); // Clear default values

    if (isset($_GET['Inventory']))
        $model->attributes = $_GET['Inventory']; // This is where the filtering is being done

    $this->render('admin', array(
        'model' => $model
    ));
}



Matt

0

#9 User is offline   terkin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 07-October 12

Posted 09 October 2012 - 03:42 AM

I understand my mistake, I change my controller and all work fine! Thx all.

public function actionIndex()
        {
        $model=new Tasks;
        if(isset($_REQUEST['Tasks']))
            $model->attributes=$_GET['Tasks'];
                $this->render('index',array(
            'model'=>$model
        ));
}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users