簡體   English   中英

在Yii2的Search model.php中使用多個表進行排序

[英]Sorting while using multiple tables in Search model.php in Yii2

我現在有2個表State and Country,我在狀態模塊中顯示狀態列表,而我也在狀態列表中顯示國家

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中完成這項工作。

我發現解決方案將對某人有幫助

我加入setSort為數據提供程序中stateSearch模型

 $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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM