Created
December 4, 2013 12:32
-
-
Save umidjons/7786750 to your computer and use it in GitHub Desktop.
Yii: Send additional data in yiiGridView.update(), and access it in action & model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // file (view): test.php | |
| <script type="text/javascript"> | |
| jQuery(document).ready(function($) | |
| { | |
| // #f_for_block - is checkbox | |
| $('#f_for_block').on('change', function() | |
| { | |
| var id=this.id, val=$(this).prop('checked')*1, data={}; | |
| data[id]=val; // {f_for_block: 1} | |
| $('#my-grid-view').yiiGridView('update', {data: data}); | |
| }); | |
| }); | |
| </script> | |
| // actionTest in controller | |
| public function actionTest() | |
| { | |
| $model = new MyModel( 'search' ); | |
| $model->unsetAttributes(); | |
| if ( isset( $_GET[ 'MyModel' ] ) ) | |
| $model->attributes = $_GET[ 'MyModel' ]; | |
| // access additional data | |
| if ( isset( $_GET[ 'f_for_block' ] ) ) | |
| $model->f_for_block = $_GET[ 'f_for_block' ]; // $model has to be public field f_for_block | |
| $this->render( 'test', array( 'model' => $model ) ); | |
| } | |
| // using in MyModel | |
| public $f_for_block; // public field | |
| public function search() | |
| { | |
| $criteria = new CDbCriteria; | |
| $criteria->compare( 'product_id', $this->product_id, true ); | |
| $criteria->compare( 'product_code', $this->product_code, true ); | |
| $criteria->compare( 'product_name', $this->product_name, true ); | |
| $criteria->compare( 'brand_name', $this->brand_name, true ); | |
| $criteria->compare( 'price_retail', $this->price_retail, true ); | |
| $criteria->compare( 'group_id', $this->group_id ); | |
| $criteria->compare( 'group_title', $this->group_title, true ); | |
| $criteria->compare( 'current_position', $this->current_position ); | |
| if ( $this->f_for_block == 1 ) // using that additional value | |
| { | |
| $criteria->addCondition( 'pm=1' ) | |
| ->addInCondition( 'pp', array( 1, 3, 4, 5 ) ) | |
| ->addCondition( 'cbid=30' ) | |
| ->addCondition( 'price_retail>0' ); | |
| } | |
| return new CActiveDataProvider( $this, array( | |
| 'criteria' => $criteria, | |
| ) ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment