13
13
use yii \base \InvalidConfigException ;
14
14
use yii \data \ActiveDataProvider ;
15
15
use yii \db \ActiveQuery ;
16
+ use yii \web \Response ;
16
17
17
18
/**
18
19
* Action for processing ajax requests from DataTables.
@@ -67,53 +68,21 @@ public function run()
67
68
$ filterQuery
68
69
->offset (Yii::$ app ->request ->getQueryParam ('start ' , 0 ))
69
70
->limit (Yii::$ app ->request ->getQueryParam ('length ' , -1 ));
70
- /* Begin of fix - serverSide pagination - get pagination from server side - Yii
71
- $dataProvider = new ActiveDataProvider(['query' => $filterQuery, 'pagination' => false]);
72
- */
73
- $ dataProvider = new ActiveDataProvider (['query ' => $ filterQuery , 'pagination ' => ['pageSize ' => Yii::$ app ->request ->getQueryParam ('length ' , 10 )] ]);
74
- // End of fix - serverSide pagination - get pagination from server side - Yii
75
- Yii::$ app ->response ->format = \yii \web \Response::FORMAT_JSON ;
71
+ $ dataProvider = new ActiveDataProvider (['query ' => $ filterQuery , 'pagination ' => ['pageSize ' => Yii::$ app ->request ->getQueryParam ('length ' , 10 )]]);
72
+ Yii::$ app ->response ->format = Response::FORMAT_JSON ;
76
73
try {
77
74
$ response = [
78
75
'draw ' => (int )$ draw ,
79
76
'recordsTotal ' => (int )$ originalQuery ->count (),
80
77
'recordsFiltered ' => (int )$ dataProvider ->getTotalCount (),
81
- /* Begin of fix - get actual data from server according to filters, offset and limit
82
- 'data' => $dataProvider->getModels(),
83
- */
84
78
'data ' => $ filterQuery ->all (),
85
- // End of fix - get actual data from server according to filters, offset and limit
86
79
];
87
80
} catch (\Exception $ e ) {
88
81
return ['error ' => $ e ->getMessage ()];
89
82
}
90
83
return $ response ;
91
84
}
92
85
93
- /**
94
- * @param ActiveQuery $query
95
- * @param array $columns
96
- * @param array $order
97
- * @return ActiveQuery
98
- */
99
- public function applyOrder (ActiveQuery $ query , $ columns , $ order )
100
- {
101
- if ($ this ->applyOrder !== null ) {
102
- return call_user_func ($ this ->applyOrder , $ query , $ columns , $ order );
103
- }
104
-
105
- foreach ($ order as $ key => $ item ) {
106
- // Begin of fix - avoid failure on columns not being orderable
107
- if (array_key_exists ('orderable ' , $ columns [$ item ['column ' ]]) && $ columns [$ item ['column ' ]]['orderable ' ] === 'false ' ) {
108
- continue ;
109
- }
110
- // End of fix - avoid failure on columns not being orderable
111
- $ sort = $ item ['dir ' ] == 'desc ' ? SORT_DESC : SORT_ASC ;
112
- $ query ->addOrderBy ([$ columns [$ item ['column ' ]]['data ' ] => $ sort ]);
113
- }
114
- return $ query ;
115
- }
116
-
117
86
/**
118
87
* @param ActiveQuery $query
119
88
* @param array $columns
@@ -138,4 +107,26 @@ public function applyFilter(ActiveQuery $query, $columns, $search)
138
107
}
139
108
return $ query ;
140
109
}
110
+
111
+ /**
112
+ * @param ActiveQuery $query
113
+ * @param array $columns
114
+ * @param array $order
115
+ * @return ActiveQuery
116
+ */
117
+ public function applyOrder (ActiveQuery $ query , $ columns , $ order )
118
+ {
119
+ if ($ this ->applyOrder !== null ) {
120
+ return call_user_func ($ this ->applyOrder , $ query , $ columns , $ order );
121
+ }
122
+
123
+ foreach ($ order as $ key => $ item ) {
124
+ if (array_key_exists ('orderable ' , $ columns [$ item ['column ' ]]) && $ columns [$ item ['column ' ]]['orderable ' ] === 'false ' ) {
125
+ continue ;
126
+ }
127
+ $ sort = $ item ['dir ' ] == 'desc ' ? SORT_DESC : SORT_ASC ;
128
+ $ query ->addOrderBy ([$ columns [$ item ['column ' ]]['data ' ] => $ sort ]);
129
+ }
130
+ return $ query ;
131
+ }
141
132
}
0 commit comments