Skip to content

Commit 4d3cf9e

Browse files
committed
refactoring
1 parent 9665976 commit 4d3cf9e

File tree

3 files changed

+38
-42
lines changed

3 files changed

+38
-42
lines changed

.gitignore

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
assets/*
2-
!assets/.gitignore
3-
protected/runtime/*
4-
!protected/runtime/.gitignore
5-
protected/data/*.db
6-
themes/classic/views/
71
/vendor/
82
composer.lock
3+
.idea/
4+

CHANGELOG.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5-
## [Unreleased]
5+
## v1.0.0
66
### Changed
7-
- Move DataTable options to protected array. Add __set and __get methods.
7+
- Move DataTable options to protected array. Add __set and __get methods.
8+
9+
## v1.0.1
10+
### Changed
11+
- Improve README
12+
13+
14+
## v1.0.2
15+
### Fixed
16+
- Server-side pagination

src/DataTableAction.php

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use yii\base\InvalidConfigException;
1414
use yii\data\ActiveDataProvider;
1515
use 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

Comments
 (0)