如何在提交表单后显示下拉菜单筛选选定的值?

I am working on a search form which have a drop down menu filter. And the site is created by Yii.

My filter form looks like this before it's submitted: PIC 1 filter form

Form when I select the filters: PIC 2 form while select the filters

But after I click the filter button it comes again like this: PIC3 after form is submitted

But I want the form should remain as PIC 2 after I submit the form when it shows the results.

My form is:

<div class="row">
    <div style="float: left">
        <label class="form_controller required">By Brand</label>

        <select style="width: 148px;" id="select_options" name="SCDcustomer_contacts_cms[brand_select_option]">
            <option value="none">Select an option</option>
            <option value="brand_preference">Brand Preference</option>
            <option value="brand_purchased">Brand Purchased</option>
        </select>
    </div>
    <div id="select_a_brand" name="select_a_brand"> 
        <?php echo $form->dropDownList($model,'brand_id', gGetBrandList('brand_id', 'brand_id, brand_name'), array('prompt'=>'Select a brand')); ?> 
    </div>

    <script type="text/javascript">
        $(document).ready(function(){
            jQuery('#select_a_brand').hide();

            $("#select_options").change(function(){
                $( "select option:selected").each(function(){

                    if(($(this).attr("value")=="brand_preference") || ($(this).attr("value")=="brand_purchased") ){                                 
                        $("#select_a_brand").show();
                    }

                    if($(this).attr("value")=="none"){                                  
                        $("#select_a_brand").hide();
                    }

                });
            });
        });
    </script>
</div>

Rules function is:

public function rules()
    {
        return array(           
            array('scd_cust_id,cust_id,order_first_name,order_surname,order_email,order_postcode, brand_id, brand_select_option', 'safe', 'on'=>'search'),
        );
    }

Form filter is :

if(!empty($filter)) {
    if ($this->brand_select_option == "brand_preference") {
                $criteria->select .= ', COUNT(*) as brand_preference';              
                $criteria->join .= ' INNER JOIN order_table ot ON ot.billing_contactid = t.contactid';
                $criteria->join .= ' INNER JOIN order_item oi ON ot.scd_order_id = oi.scd_order_id';        
                $criteria->join .= ' INNER JOIN johnanthony_rstest.product p ON p.product_id = oi.product_id';
                $criteria->condition .= (!empty($criteria->condition) ? ' AND ' : '') . 'p.brand_id=:brand_id';
                $criteria->group = 't.contactid';
                $criteria->order = 'COUNT(*) DESC';
                $criteria->params = array(':brand_id'=>$this->brand_id);
                $paging['pagination']['route']='report/index?SCDcustomer_contacts_cms[brand_id]=' . $this->brand_id;//For ajax pagination URL
            }

            if ($this->brand_select_option == "brand_purchased") {
                $criteria->select .= ', SUM(product_price) AS brand_purchased';    
                $criteria->join .= ' INNER JOIN order_table ot ON ot.billing_contactid = t.contactid';
                $criteria->join .= ' INNER JOIN order_item oi ON ot.scd_order_id = oi.scd_order_id';  
                $criteria->join .= ' INNER JOIN johnanthony_rstest.product p ON p.product_id = oi.product_id';
                $criteria->condition .= (!empty($criteria->condition) ? ' AND ' : '') . 'p.brand_id=:brand_id';
                $criteria->group = 't.contactid';
                $criteria->order = 'SUM(product_price) DESC';
                $criteria->params = array(':brand_id'=>$this->brand_id);
                $paging['pagination']['route']='report/index?SCDcustomer_contacts_cms[brand_id]=' . $this->brand_id;//For ajax pagination URL
            }
 }

Ajax file is:

if (!empty($model->brand_id) && ($model->brand_select_option == "brand_preference")) {
    array_push($options['columns'], array(
        'name'=>'brand_preference',
        'filter'=>false,
        'header'=>'Brand Preference (No of purchased items)',
        'type'=>'raw',
        'value'=>'$data->brand_preference',

    )); 
}
if (!empty($model->brand_id) && ($model->brand_select_option == "brand_purchased")) {
    array_push($options['columns'], array(
        'name'=>'brand_purchased',
        'filter'=>false,
        'header'=>'Brand Purchased (Sum of purchased items)',
        'type'=>'raw',
        'value'=>'$data->brand_purchased',

    )); 
}

Main problem is DOM refreshes page, because probably regular submit is made. Without submit method can't tell anything more specifically. If you do ajax, you get all data in json data holder. Then you parse it into object and html required parts to where you want to show results. All filtering and etc. has to happen in model. Controller only passes values over to it.

Make filter button as ajaxButton:

<?php echo CHtml::ajaxSubmitButton(
    'Filter',
     Yii::app()->createUrl('report/index'),
     array(
        'type'=>'POST',
        'data'=> 'js:{"SCDcustomer_contacts_cms[brand_select_option]": $('#select_options').val(), "SCDcustomer_contacts_cms[brand_id]": $('#select_brand').val() }',
        'success'=>'js:function(data){ var data = JSON.parse(data);
        $('#search-result-holder').html(data.search-results); }'
        )); ?>

UPDATE: 2014 - 09 - 30 Some extra data for processing.

You should make some extra view with html of the way you would want your results to look like. Pass attribute values to that view via renderPartial (this has to happen within controller). For example:

//Controller part which passes posted data to model

<?php
  public function actionIndex() {
      ....

      if(Yii::app()->request->isAjaxRequest){
          $attributes = Yii::app()->request->getParam('SCDcustomer_contacts_cms');
          $model->attributes = $attributes;
      }

    ...
//Part of the controller which returns resulting model after specific functions
      $model = $model->getDataByFilterQuery();
      $search_results = $this->renderPartial('report/extra-views/search-results', array('model'=>$model), true);
      echo CJSON::encode(array('search-results'=>$search_results));
    ...
}

?>

//In model
public function brandPreference() {
                $criteria = new CDbCriteria;
                $criteria->select .= ', COUNT(*) as brand_preference';              
                $criteria->join .= ' INNER JOIN order_table ot ON ot.billing_contactid = t.contactid';
                $criteria->join .= ' INNER JOIN order_item oi ON ot.scd_order_id = oi.scd_order_id';        
                $criteria->join .= ' INNER JOIN johnanthony_rstest.product p ON p.product_id = oi.product_id';
                $criteria->condition .= (!empty($criteria->condition) ? ' AND ' : '') . 'p.brand_id=:brand_id';
                $criteria->group = 't.contactid';
                $criteria->order = 'COUNT(*) DESC';
                $criteria->params = array(':brand_id'=>$this->brand_id);
                $paging['pagination']['route']='report/index?SCDcustomer_contacts_cms[brand_id]=' .                $this->brand_id;//For ajax pagination URL
        return new CActiveDataProvider($this, array(
            'criteria' => $criteria, $paging
        ));

}

public function brandPurchased() {
                $criteria = new CDbCriteria;
                $criteria->select .= ', SUM(product_price) AS brand_purchased';    
                $criteria->join .= ' INNER JOIN order_table ot ON ot.billing_contactid = t.contactid';
                $criteria->join .= ' INNER JOIN order_item oi ON ot.scd_order_id = oi.scd_order_id';  
                $criteria->join .= ' INNER JOIN johnanthony_rstest.product p ON p.product_id = oi.product_id';
                $criteria->condition .= (!empty($criteria->condition) ? ' AND ' : '') . 'p.brand_id=:brand_id';
                $criteria->group = 't.contactid';
                $criteria->order = 'SUM(product_price) DESC';
                $criteria->params = array(':brand_id'=>$this->brand_id);
                $paging['pagination']['route']='report/index?SCDcustomer_contacts_cms[brand_id]=' . $this->brand_id;//For ajax pagination URL
        return new CActiveDataProvider($this, array(
            'criteria' => $criteria, $paging
        ));
}



public function getDataByFilterQuery() {
    if($this->brand_select_option == 'brand_preference')
      return $this->brandPreference();
    elseif($this->brand_select_option == 'brand_purchased')
      return $this->brandPurchased();
    else
      return null //or whatever you want
}


//Search result file
  <?php $this->widget('zii.widgets.CDetailView', array(
    'data'=>$model,
    'attributes'=>array(
        'id',
        'brand',
        'product_title',
        'description' => array(
            'name' => 'description',
            'value' => html_entity_decode(CHtml::decode($model->description)), //this is only for example purposes
        ),
        'price',
        'date',
    ),
));?>

//Index file
...
<?php echo CHtml::ajaxSubmitButton(
    'Filter',
     Yii::app()->createUrl('report/index'),
     array(
        'type'=>'POST',
        'data'=> 'js:{"SCDcustomer_contacts_cms[brand_select_option]": $('#select_options').val(), "SCDcustomer_contacts_cms[brand_id]": $('#select_brand').val() }',
        'success'=>'js:function(data){ var data = JSON.parse(data);
        $('#search-result-holder').html(data.search-results); }'
        )); ?>

...
<div id="search-result-holder"></div>
...

As well you have to modify it specifically for your case. And I'm in doubt that $paging variable will work there as it is. I prefer using pagination parameter in CActiveDataProvider object. In second option you can open array and within that array open pagination parameters array. Or do like here: CPagination documentation it is up to you.

It seems you are loading the page every time when you click on button Filter,if you using Ajax you can to this click event with out postback,or if you have to refresh page the simplest way it just to save drop down menu Index's that was selected before you do the filter event button, and when you load your page again you just will pass to you drop down menu's with index's is need to be selected when you load the page.I hope it will help you.