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

时间:2014-09-30 04:43:41

标签: javascript php forms yii

我正在处理一个带有下拉菜单过滤器的搜索表单。该网站由Yii创建。

我的过滤表单在提交之前看起来像这样:PIC 1 filter form

选择过滤器时的表单:PIC 2 form while select the filters

但点击filter按钮后,它会再次出现:PIC3 after form is submitted

但是我希望在显示结果后提交表单后,表单应保持为PIC 2

我的表格是:

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

规则功能是:

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'),
        );
    }

表单过滤器是:

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文件是:

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 个答案:

答案 0 :(得分:1)

主要问题是DOM刷新页面,因为可能会定期提交。没有提交方法不能更具体地告诉任何事情。如果你执行ajax,你将获得json数据持有者的所有数据。然后你将它解析为对象和html所需的部分到你想要显示结果的位置。所有过滤等都必须在模型中进行。控制器只将值传递给它。

将过滤按钮设为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); }'
        )); ?>

更新:2014 - 09 - 30一些额外的处理数据。

你应该用你想要结果的方式的html做一些额外的视图。通过renderPartial将属性值传递给该视图(这必须在控制器内发生)。例如:

//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>
...

同样你必须根据你的情况专门修改它。而且我怀疑$ paging变量会在那里工作。我更喜欢在CActiveDataProvider对象中使用pagination参数。在第二个选项中,您可以打开数组并在该数组中打开分页参数数组。或者像这里一样:CPagination documentation这取决于你。

答案 1 :(得分:0)

每次单击按钮Filter时,您似乎正在加载页面,如果您使用Ajax,则可以在没有回发的情况下执行此单击事件,或者如果您必须刷新页面,则只需保存下拉菜单的最简单方法在您执行过滤器事件按钮之前选择的索引,当您再次加载页面时,您只需传递给您,当您加载时需要选择带有索引的下拉菜单页面。我希望它能帮到你。