2014-09-30 127 views
0

我正在搜索表單上有一個下拉菜單過濾器。該網站由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 
      } 
} 

阿賈克斯文件:

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

    )); 
} 

回答

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

以及您必須修改它專門爲您的情況。我懷疑$分頁變量會在那裏工作。我更喜歡在CActiveDataProvider對象中使用分頁參數。在第二個選項中,您可以打開數組,並在該數組中打開分頁參數數組。或者喜歡這裏:CPagination documentation這取決於你。

+0

謝謝你。這應該是解決方案。但我不能只是複製過去並開始工作。我收到一個錯誤。我把這樣的代碼放在我的表單中,而不是按鈕:http://tinypic.com/r/2rfzy2d/8 ,我得到這個錯誤:' 解析錯誤:語法錯誤,意外的T_CONSTANT_ENCAPSED_STRING,期待')'在線86上的/var/www/vhosts/rstest.john-anthony.com/httpdocs/protected/modules/admincms/views/report/index.php' – 2014-09-30 06:43:42

+0

這將不起作用,如示例中所示。首先,我懷疑你有div或任何其他的HTML標籤與ID#search-result-holder(這應該是你的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])傳遞給屬性。 – juslintek 2014-09-30 07:41:50

+0

我的視圖文件(httpdocs \ protected \ modules \ admincms \ views \ report \ index.php)的結尾是這樣的:http://tinypic.com/r/dqgk1d/8 和我的ajax文件(httpdocs \ protected \ modules \ admincms \ views \ report \ _ajaxContent.php)是:http://www.ldjf.org/_ajaxContent.zip查看第50行 – 2014-09-30 07:59:08

0

看起來你每次點擊按鈕過濾器時都會加載頁面,如果你使用Ajax,你可以通過回發來完成這個點擊事件,或者如果你必須刷新頁面,最簡單的方法就是保存下拉菜單。那是在你做過濾器事件按鈕之前選擇的,當你再次加載你的頁面時,你只需要傳遞給你下拉菜單的索引,當你加載頁面時需要選擇。我希望它能幫助你。

+0

謝謝你。 是的..我想在提交頁面時刷新頁面,因爲要顯示結果。同一時間我想顯示選定的過濾器。'保存下拉菜單索引是在你做過濾器事件按鈕之前選擇的,當你再次加載你的頁面時,你會傳遞給你下載菜單的索引,當你加載頁面時需要選擇。'我怎麼能根據我的代碼做到這一點。我很困惑。 – 2014-09-30 05:20:39

相關問題