繁体   English   中英

提交表单后,如何显示下拉菜单过滤所选值?

[英]How to show a dropdown menu filter selected value after form is submitted?

我正在使用具有下拉菜单过滤器的搜索表单。 该网站是由Yii创建的。

我的过滤器表单在提交之前看起来像这样: PIC 1 筛选表格

选择过滤器时的表格: PIC 2 选择过滤器时的表格

但是,当我单击filter按钮后,它又像这样出现: PIC3 表格提交后

但是,我希望在表单显示结果后提交表单后,该表单仍应保留为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',

    )); 
}

主要问题是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对象中使用分页参数。 在第二个选项中,您可以打开数组,并在该数组中打开分页参数数组。 或者在这里做: CPagination文档由您决定。

似乎每次单击按钮“过滤器”时,您都在加载页面,如果您使用Ajax,则可以不带回发地执行此click事件,或者如果您必须以最简单的方式刷新页面以保存下拉菜单“索引”,在您执行过滤器事件按钮之前已选择了该选项,并且当您再次加载页面时,您将传递给您,当您加载页面时需要选择带有索引的下拉菜单。希望对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM