In Yii 2.0, based on the data provider, the support of list interface sorting, the sorting rule refers to the implementation of Zen Tao
1. View the task list page of Zen Tao, and its sorting is based on the descending order of ID, as shown in Figure 1
2. After clicking on the consumption field, the triangle is displayed as dark, indicating that the current sorting is based on the consumption ascending order, and if its value is the same, the ascending order is arranged based on the ID, as shown in Figure 2
3. After clicking the consumption field again, the inversion of the triangle is displayed as dark, indicating that the current sorting is based on the consumption descending order, and if its value is the same, then the ID is arranged in descending order, as shown in Figure 3
4. If you want to restore to the default sort, that is, the descending order based on the ID, then click 2 times the ID field (the 1st time is ascending, the 2nd time is descending), the inverted triangle is displayed as dark, indicating that the current sorting is based on the ID Arranged in descending order, as shown in Figure 4
5. The corresponding configuration of the data provider is as follows, and specify the default sorting based on the ID descending sort through yii\data\sort::$defaultOrder
return Yii::createObject([
'class' => ActiveDataProvider::className(),
'query' => $query,
'pagination' => [
'params' => $requestParams,
],
'sort' => [
'defaultOrder' => [
'id' => SORT_DESC,
],
'params' => $requestParams,
],
]);
6. GET request:http://api.pcs-api.localhost/v1/plans, generate SQL, the ORDER BY clause in the SQL statement is arranged in descending order, as shown in Figure 5
SELECT `pa_plan`.* FROM `pa_plan` GROUP BY `pa_plan`.`id` ORDER BY `id` DESC LIMIT 20
7. GET request:http://api.pcs-api.localhost/v1/plans?sort=importance, generate sql , the ORDER BY clause in the sql statement is based on the importance ascending order (expected to be based on the importance and id ascending order), as shown in Figure 6
SELECT `pa_plan`.* FROM `pa_plan` GROUP BY `pa_plan`.`id` ORDER BY `importance` LIMIT 20
8. GET request:http://api.pcs-api.localhost/v1/plans?sort=-importance, the ORDER BY clause in the SQL statement is generated based on the IMPORANCE descending order (expected to be based on the IMPORANCE and id descending order), as shown in Figure 7
SELECT `pa_plan`.* FROM `pa_plan` GROUP BY `pa_plan`.`id` ORDER BY `importance` DESC LIMIT 20
9. The corresponding configuration adjustment of the data provider is as follows, and the ASC (based on ID ascending order) and DESC (based on ID ascending order) and DESC (based on ID ascending order) and desc (based on ID in descending order) is specified through yii\data\sort::$defaultOrder. Element; ASC (based on IMPORANCE and ID ascending order) and DESC (based on IMPORANCE and ID descending order) elements; ASC (based on IS_UNITED attribute) is_united and id ascending order) and DESC (based on is_united and id descending order) elements
return Yii::createObject([
'class' => ActiveDataProvider::className(),
'query' => $query,
'pagination' => [
'params' => $requestParams,
],
'sort' => [
'defaultOrder' => [
'id' => SORT_DESC,
],
'attributes' => [
'id' => [
'asc' => ['id' => SORT_ASC],
'desc' => ['id' => SORT_DESC],
'default' => SORT_ASC,
'label' => Yii::t('model/plan', 'ID'),
],
'importance' => [
'asc' => ['importance' => SORT_ASC, 'id' => SORT_ASC],
'desc' => ['importance' => SORT_DESC, 'id' => SORT_DESC],
'default' => SORT_ASC,
'label' => Yii::t('model/plan', 'Importance'),
],
],
'params' => $requestParams,
],
]);
10. GET request:http://api.pcs-api.localhost/v1/plans, which generates SQL , the ORDER BY clause in the SQL statement is arranged in descending order of id
SELECT `pa_plan`.* FROM `pa_plan` GROUP BY `pa_plan`.`id` ORDER BY `id` DESC LIMIT 20
11. GET request:http://api.pcs-api.localhost/v1/plans?sort=importance, generate SQL, the ORDER BY clause in the SQL statement is based on the importance and id ascending order, as shown in Figure 8
SELECT `pa_plan`.* FROM `pa_plan` GROUP BY `pa_plan`.`id` ORDER BY `importance`, `id` LIMIT 20
12. GET request:http://api.pcs-api.localhost/v1/plans?sort=-importance, which generates sql , the ORDER BY clause in the sql statement is based on the descending order of importance and id, as shown in Figure 9
SELECT `pa_plan`.* FROM `pa_plan` GROUP BY `pa_plan`.`id` ORDER BY `importance` DESC, `id` DESC LIMIT 20
13. GET request:http://api.pcs-api.localhost/v1/plans?sort=id, which generates SQL, the ORDER BY clause in the SQL statement is arranged based on the ascending order, as shown in Figure 10
SELECT `pa_plan`.* FROM `pa_plan` GROUP BY `pa_plan`.`id` ORDER BY `id` LIMIT 20
14. GET request:http://api.pcs-api.localhost/v1/plans?sort=-id, generate SQL, the ORDER BY clause in the SQL statement is arranged in descending order, as shown in Figure 11
SELECT `pa_plan`.* FROM `pa_plan` GROUP BY `pa_plan`.`id` ORDER BY `id` DESC LIMIT 20
15. GET request:http://api.pcs-api.localhost/v1/plans?sort=create_user_id, which generates sql , the order by clause in the sql statement is based on the id descending order (default sort), and is not ascending on the ascending order of create_user_id (because create_user_id properties are not defined), as shown in Figure 12
SELECT `pa_plan`.* FROM `pa_plan` GROUP BY `pa_plan`.`id` ORDER BY `id` DESC LIMIT 20











