2015-07-06 28 views
1

在搜索model.php多個表排序我有2台州和國家現在我出狀態模塊和我的國家名單,我展示國家也在狀態列表像而使用Yii2

StateName CountryName ActionButtons 
Victoria  Australia  edit | delete 
NSW   Australia  edit | delete 
Auckland  New Zealand edit | delete 
California USA   edit | delete 

我想排序列狀態和國家名稱,但國家名稱排序不工作在這裏。

因爲我在YII版本1.1.16上工作我使用CSort但我不知道如何在YII2中使用它。請幫助我在YII2中完成這項工作。

+0

顯示您的代碼,請 – scaisEdge

+0

哎@scaisEdge感謝尋找我找到解決方案 –

回答

1

我找到的解決方案將幫助別人

我stateSearch模型添加setSort爲數據提供程序

$dataProvider->setSort([ 
      'attributes' => [ 
       'country_name' => [ 
        'asc' => ['country_name' => SORT_ASC, 'country_name' => SORT_ASC], 
        'desc' => ['country_name' => SORT_DESC, 'country_name' => SORT_DESC], 
        'default' => SORT_ASC 
       ], 
       'state_name' => [ 
        'asc' => ['state_name' => SORT_ASC, 'state_name' => SORT_ASC], 
        'desc' => ['state_name' => SORT_DESC, 'state_name' => SORT_DESC], 
        'label' => 'state_name', 
        'default' => SORT_ASC 
       ],     
      ] 
     ]); 

$query->andFilterWhere([ 
      'state_name' => $this->state_name, 
      'country_name' => $this->country_name, 
     ]); 

和工作!

與參考以下鏈接

http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/