1313use yii \base \InvalidConfigException ;
1414use yii \data \ActiveDataProvider ;
1515use yii \db \ActiveQuery ;
16+ use yii \web \Response ;
1617
1718/**
1819 * Action for processing ajax requests from DataTables.
@@ -67,53 +68,21 @@ public function run()
6768 $ filterQuery
6869 ->offset (Yii::$ app ->request ->getQueryParam ('start ' , 0 ))
6970 ->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 ;
7673 try {
7774 $ response = [
7875 'draw ' => (int )$ draw ,
7976 'recordsTotal ' => (int )$ originalQuery ->count (),
8077 'recordsFiltered ' => (int )$ dataProvider ->getTotalCount (),
81- /* Begin of fix - get actual data from server according to filters, offset and limit
82- 'data' => $dataProvider->getModels(),
83- */
8478 'data ' => $ filterQuery ->all (),
85- // End of fix - get actual data from server according to filters, offset and limit
8679 ];
8780 } catch (\Exception $ e ) {
8881 return ['error ' => $ e ->getMessage ()];
8982 }
9083 return $ response ;
9184 }
9285
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-
11786 /**
11887 * @param ActiveQuery $query
11988 * @param array $columns
@@ -138,4 +107,26 @@ public function applyFilter(ActiveQuery $query, $columns, $search)
138107 }
139108 return $ query ;
140109 }
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+ }
141132}
0 commit comments