dongwang3066 2014-09-30 04:43
浏览 75
已采纳

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

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',

    )); 
}
  • 写回答

2条回答 默认 最新

  • dongxing4643 2014-09-30 06:31
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀