CGridView filter with CActiveForm

Hi guys,

Is there any example for filter CGridView with CActiveForm. Currently, the filtering result with CActiveForm work well for the 1st page… But the following for 2nd page and others in CGridView will gt error. It will show the 2nd page without filtering result when i click the 2nd page. ~~

Is there anyone facing this problem before using CActiceForm filtering?? Pls helpsss…

Can you post your code… the form code… the controller code that processes the form values and the model code that does the filtering…

Would this help you out?

http://www.yiiplayground.cubedwater.com/index.php?r=InternationalizationModule/datetime/userinput

hi enfield


http://www.yiiplaygr...etime/userinput 

that one is update cgridview rather filtering info. =)

Hi mdomba,

View:




<div class="form">

    <?php

    $form = $this->beginWidget('CActiveForm', array(

        'id' => 'sale-form',

        'enableClientValidation' => true,

        'enableAjaxValidation' => false,

        'clientOptions' => array(

            'validateOnSubmit' => true,

        ),

            ));

    ?>


    <table border="1">


        <tr>

            <td>

                <?php echo $form->labelEx($model, 'machine_tag'); ?>

            </td>

            <td>

                <?php echo $form->dropDownList($model, 'machine_tag', $model->getMachineOptions(), array('empty' => '--Please Select--')); ?>

                <?php echo $form->error($model, 'machine_tag'); ?>

            </td>

        </tr>

          <td colspan="4">

                <?php

                $this->widget('zii.widgets.jui.CJuiButton', array(

                    'buttonType' => 'submit',

                    'name' => 'btnSubmit',

                    'value' => 'Submit',

                    'caption' => Yii::t('ui', 'View'),

                ));

                ?>

            </td>

        </tr>

    </table>


    <?php $this->endWidget(); ?>


</div><!-- form -->


    <?php

    if($model->getProductTarget()->totalItemCount > 0)

   

        $this->widget('zii.widgets.grid.CGridView', array(

            //'id' => 'detail-grid',

            'dataProvider' => $model->getProductTarget(),

            //'filter' => $model,

            'columns' => array(

                array(

                    'name' => 'description',

                    'header' => t("sales", "Product"),

                ),

                array(

                    'name' => 'total',

                    'header' => t("sales", "Total Sales"),

                ),

            ),

        ));

    }

    ?>






Controller:


public function actionProduct_target()

    {


        $model = new SaleForm('getProductTarget');


        // collect user input data

        if(isset($_POST['SaleForm']))

        {

            $model->attributes = $_POST['SaleForm'];

        }

        $this->render('product_target', array(

            'model' => $model,

        ));

    }

Model:


 public function getProductTarget()

    {

        $sql = "";

        $count = 0;


        // collect user input data

        if(isset($_POST['SaleForm']))

        {


            query..

        }


        return new CSqlDataProvider($sql, array(

                    'pagination' => array(

                        'pageSize' => 5,

                    ),

                ));

    }



Please helps me… Any problem inside?

Problem is in the $model->getProductTarget()

The line - if(isset($_POST[‘SaleForm’])) - is not true for pagination or sorting becasue those are done with GET not POST.

I would suggest you to remove that line… you already test that in the controller… here you can just check the $model variable (that is set in the controller)

hi Mdomba,

model::


public function getProductTarget()

    {

        $sql = "";

        $count = 0;


            query..


        return new CSqlDataProvider($sql, array(

                    'totalItemCount' => $count,

                    'keyField' => 'description',

                    'pagination' => array(

                        'pageSize' => 5,

                    ),

                ));

    }

i already remove if(isset($_POST[‘SaleForm’]))in model, but the result still same.

The filtering result with CActiveForm work well for the 1st page… But the following for 2nd page and others in CGridView will gt error. It will show the default 2nd page without filtering result when i click the 2nd page. ~~ Any ideas… Helpss

hmm… yes… sorry for the previous "incomplete" answer…

the same logic problem is in the controller… sorting and pagination uses GET not POST and here is the problem… if you change your form to GET all should work…

emm…





 if(isset($_GET['SaleForm']))

        {

            $model->attributes = $_GET['SaleForm'];

        }

if change to GET, i can’t get user value for filtering option. Helps.

Did you change your form so that it posts with GET instead of POST?


Did you change your form so that it posts with GET instead of POST? 

How to change it?? i don’t have idea on it??

Mdomba help me… @.@

Oh… come on… this is simple…

You just need to check the documentations… don’t be lazy !

For this time… here it is - http://www.yiiframework.com/doc/api/1.1/CActiveForm#method-detail

Mdomba is a great teacher… Thanks =)

Thanks n grateful have you in Yii…