2016-10-18 98 views
0

我有排序在列表視圖yii2中的問題。我不知道如何使用獨立過濾器表單創建下拉列表。yii2與下拉列表排序

在模型中,我有:

$dataProvider->sort->attributes['sort'] = [ 
      'asc' => ['game.rating' => SORT_ASC], 
      'desc' => ['game.rating' => SORT_DESC], 
     ]; 

但如何創建下拉?

型號:

class GameSearch extends Offer 
{ 

    public $status; 
    public $title; 
    public $type; 
    public $platform; 
    public $rating; 
    public $pageSize; 
    public $sort; 


    public function rules() 
    { 
     return [ 
      [['status', 'platform', 'pageSize'], 'integer'], 
      [['title', 'type', 'sort', 'rating'], 'string'] 
     ]; 
    } 

    public function search($params, $query) 
    { 
     $query->joinWith(['game', 'author']); 


     $dataProvider = new ActiveDataProvider([ 
      'query' => $query, 
      'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]], 
      'pagination' => [ 
       'pageSize' => $this->pageSize, 
      ], 
     ]); 

     $dataProvider->sort->attributes['sort'] = [ 
      'asc' => ['game.rating' => SORT_ASC], 
      'desc' => ['game.rating' => SORT_DESC], 
     ]; 

     if (!($this->load($params) && $this->validate())) { 
      return $dataProvider; 
     } 

     $dataProvider->pagination->pageSize = $this->pageSize; 

     $query->andFilterWhere(['like', 'game.title', $this->title]) 
      ->andFilterWhere(['like', 'platform', $this->platform]); 

     return $dataProvider; 
    } 
} 

和我的搜索文件

<?= $form->field($model, 'sort') 
       ->dropDownList([ 
        'rating' => 'rating ASC', 
        '-rating' => 'rating DESC', 
       ]) 
       ->label(false) 
       ->error(false); 
      ?> 

回答

2

我有同樣的proplem,這裏是我的配置 查看。值1排序ASC和值2排序DESC來看

$findUrl = Url::current([], true); 
$pos = strpos($findUrl, '&rating'); 
if($pos){ 
    $findUrl = substr($findUrl, 0 , $pos); 
} 
$app_js = <<<JS 
    $("#find-rating1").change(function() { 
     var rating_value= $(this).val(); 
     location.href="$findUrl" +"&rating="+rating_value; 
    }); 
JS; 
$this->registerJs($app_js); 

正如你看到的,當下拉有改變事件

<?= $form->field($model, 'sort') 
      ->dropDownList([ 
       '1' => 'rating ASC', 
       '2' => 'rating DESC', 
      ],['id' =>'find-rating1']) 
      ->label(false) 
      ->error(false); 
     ?> 

,並添加js的底部。將重定向到URL將對參數1或2進行評級。因此,在Controller中,您可以獲得評分值1或2並設置分類。

$ratingValue = getParam('rating', 1); 

與$ ratingValue你可以自定義搜索查詢

+0

演示網址http://cubic1.jp/search/find-employees?type=1面積http://prntscr.com/cw2weu – dungphanxuan

+0

謝謝你,我會試試你的解決方案 –