【问题标题】:How to show a dropdown menu filter selected value after form is submitted?提交表单后如何显示下拉菜单过滤器选择的值?
【发布时间】:2014-09-30 04:43:41
【问题描述】:

我正在开发一个具有下拉菜单过滤器的搜索表单。该网站是由 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',

    )); 
}

【问题讨论】:

    标签: javascript php forms yii


    【解决方案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 对象中使用分页参数。在第二个选项中,您可以打开数组并在该数组中打开分页参数数组。或者喜欢这里:CPagination documentation 这取决于你。

    【讨论】:

    • 谢谢。那应该是解决方案。但我不能只是复制过去并开始工作。我收到一个错误。我将这样的代码放入我的表单而不是按钮:tinypic.com/r/2rfzy2d/8,我收到此错误:`Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in /var/www/vhosts/rstest.john- anthony.com/httpdocs/protected/modules/admincms/views/report/index.php 第 86 行`
    • 这不会像示例中所示的那样工作。首先,我怀疑您是否有 div 或任何其他带有 id #search-result-holder 的 html 标签(这应该是您对结果进行 html 处理的地方)。在视图中,您应该通过 renderPartial('report/extra-views-folder/search-result', array('model'=>$model), true); 返回在控制器或其他视图中生成的 html 数据。将渲染部分分配给某个变量并回显 CJSON::encode(array('search-results'=>$assigned_to_renderPartial_variable));当然,在此之前,您必须将 brand_id (SCDcustomer_contacts_cms[brand_id]) 传递给属性。
    • 我的视图文件 (httpdocs\protected\modules\admincms\views\report\index.php) 像这样结束:tinypic.com/r/dqgk1d/8 和我的 ajax 文件 (httpdocs\protected\modules\admincms\views\ report_ajaxContent.php) 是:ldjf.org/_ajaxContent.zip 看看 #50 行
    【解决方案2】:

    似乎您每次单击按钮过滤器时都在加载页面,如果您使用 Ajax,您可以在没有回发的情况下处理此点击事件,或者如果您必须以最简单的方式刷新页面,它只是为了保存下拉菜单在您执行过滤事件按钮之前选择的索引,当您再次加载页面时,您只需将下拉菜单传递给您,在加载页面时需要选择索引。希望对您有所帮助。

    【讨论】:

    • 谢谢。是的..我想在提交页面时刷新页面,因为显示结果。同时我想显示选定的过滤器。 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. 根据我上面的代码我该怎么做。我很困惑。
    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 2011-01-21
    • 1970-01-01
    相关资源
    最近更新 更多