FahadAkram
FahadAkram

Reputation: 475

Yii Refresh Grid On DropDown change

In YII views folder i have test module and admin.php file to manage contents are below and i render form here where i put the code of form and dropdown in it , i want that grid refresh value of status change in dropdown Suppose i select "Approved" than Grid show the data where status is approved

    <?php
Yii::app()->clientScript->registerScript('dropdown', "

$('.dropdown-form form').submit(function(){
$('#testimonial-grid').yiiGridView('update', {
        data: $(this).serialize()
    });
    return false;
});
");
?>
<h1>Manage Testimonials</h1>
<div class="dropdown-form">
<?php $this->renderPartial('_dropdownform',array(
    'model'=>$model,
)); ?>
</div><!-- search-form -->
    <?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'testimonial-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'id',
        'created_by',
        'test_name',
        'test_email',
        'comments',
        'created_at',
        /*
        'status',
        'approved_on',
        'approved_by',
        */
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

Form below is _dropdownform , it contain a form and dropdown from this dropdown i am choosing the value of status

<div class="wide form">
    <?php
    $form = $this->beginWidget('CActiveForm', array(
        'action' => Yii::app()->createUrl($this->route),
        'method' => 'get',
    ));
    ?>
    <div class="row">
        <?php
        echo CHtml::dropDownList('status', '', array(0 => 'New', 1 => 'Approved', 2 => 'Declined'), array(
            'prompt' => 'Select Status',
            'ajax' => array(
                'type' => 'POST',
                'url' => Yii::app()->createUrl('testimonial/loadthedata'), 
                //or $this->createUrl('loadcities') if '$this' extends CController
                'update' => '#testimonial-grid', //or 'success' => 'function(data){...handle the data in the way you want...}',
                'data' => array('status' => 'js:this.value'),
        )));
        ?>
    </div>

    <div class="row buttons">
<?php //echo CHtml::submitButton('Search');   ?>
    </div>
        <?php $this->endWidget(); ?>
</div><!-- search-form -->

AND THE CODE IN MY CONTROLLER OR URL GIVEN IN DROPDOWN TO FETCH DATA IS FOLLOWING ACTION BUT I DONT KNOW HOW TO FETCH DATA FROM THIS FUNCTION AND PASS TO GRID VIEW

public function actionloadthedata() {
    if (isset($_POST['status'])) {
        $status = $_POST['status'];
       if($status==0){
           $status='New';
       }
       if($status==1){
           $status='Approved';
       }
       if($status==2){
           $status='Declined';
       }
        Testimonial::model()->findByAttributes(array('status'=>$status));


    }
}

Upvotes: 0

Views: 2535

Answers (3)

Evhz
Evhz

Reputation: 9284

In your gridview file, make sure you have this code:

Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
    $('.search-form').toggle();
    return false;
});
$('.search-form form').submit(function(){
    $('#ad-grid').yiiGridView('update', {
        data: $(this).serialize()
    });
    return false;
});
");

then in the CGridView definition:

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'testimonial-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        ...
        array(
            'name'=>'Status',
            'filter'=>CHtml::dropDownList('YourModel[status]', $model->status, array(0 => 'New', 1 => 'Approved', 2 => 'Declined'), array('empty' => '--all--') ),
            'value'=>'( $data->status == 0) ? "New": ( $data->status == 1) ? "Approved" : "Declined"',
            'htmlOptions' => array(
                'style' => 'width: 40px; text-align: center;',
            ),
        ),
        ...
        array(
            'class'=>'CButtonColumn',
        ),
    ),
));
// CGridView 

In order to save if/else in the 'value' section, you can implement a method in your model that returns the string associated to the integer.

It works great, just with the default Yii admin.php view, which you can edit as much as you need.

Update
Added support for empty status, does not filter query results

Upvotes: 1

FahadAkram
FahadAkram

Reputation: 475

Thanks @Alex for your help but i am successful to make filterdropdown for grid , code is following but will you please tell me that i want that grid show only values where status=New , how i can do when page load grid show values where status is New but first i paste the working code of dropdown filter for grid Here is my admin.php file

    <?php
$this->breadcrumbs = array(
    'Testimonials' => array('index'),
    'Manage',
);
$this->menu = array(
    array('label' => 'List Testimonial', 'url' => array('index')),
    array('label' => 'Create Testimonial', 'url' => array('create')),
);
?>
<h1>Manage Testimonials</h1>
<!-----------drop down form------------->
<?php
Yii::app()->clientScript->registerScript('dropdownfilter', "

$('.dropdown-form form #staticid').change(function(){
    $.fn.yiiGridView.update('testimonial-grid', {
        data: $(this).serialize()
    });
    return false;
});
");
?>
<div class="dropdown-form">
    <?php
    $this->renderPartial('_dropdownfilter', array(
        'model' => $model,
    ));
    ?>
</div><!-- search-form -->
<?php
$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'testimonial-grid',
    'dataProvider' => $model->search(),
   // 'filter' => $model,
    'columns' => array(
        'id',
        'created_by',
        'test_name',
        'test_email',
        'comments',
        'created_at',
        'status',
        array(
            'class' => 'CButtonColumn',
        ),
    ),
));
?>

Here is my render partial form where i place the static drop dow

<div class="wide form">
<?php
$form = $this->beginWidget('CActiveForm', array(
    'action' => Yii::app()->createUrl($this->route),
    'method' => 'get',
));
?>
<div class="row">
    <?php
    echo CHtml::dropDownList('staticid', '', array('0' => 'New', '1' => 'Approved', '2' => 'Declined'), array(
        // 'onChange' => 'this.form.submit()',
        'ajax' => array(
            'type' => 'POST', //request type
    )));
    ?>
</div>
<?php $this->endWidget(); ?>

And Here is code of my adminaction in controller

public function actionAdmin() {
    $model = new Testimonial('search');
    $model->unsetAttributes();  // clear any default values
    if (isset($_GET['staticid'])) {
        $getStatus = $_GET['staticid'];
        if ($getStatus == 0)
            $status = 'New';
        if ($getStatus == 1)
            $status = 'Approved';
        if ($getStatus == 2)
            $status = 'Declined';
        $model->status = $status;
    }
    if (isset($_GET['Testimonial']))
        $model->attributes = $_GET['Testimonial'];
    $this->render('admin', array(
        'model' => $model,
    ));
}

Now i want that when i actionadmin triggered first time it show status=New values

Upvotes: 0

Alex
Alex

Reputation: 8072

You can use CGridView property filterCssClass to link the grid filter, for example

$this->widget('CGridView', array(
    'id' => 'my-list',
    'filterCssClass' => '#filterFormId .filter',

And there is filter form

<?php $form = $this->beginWidget('CActiveForm', array(
    'id' => 'filter-fomr-id',
)); ?>

<div class="filter clearfix">
    <?php echo $form->dropDownList($model, 'name', [0=>'all', '1'=>'some else']); ?>
</div>

Replace #filterFormId .filter on jquery selector specific to you form. In other words, set id attribute for filter form, then use "#THISID .row".

Upvotes: 1

Related Questions