Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ZAYEC77 committed Aug 10, 2016
1 parent 9665976 commit 4d3cf9e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 42 deletions.
8 changes: 2 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
assets/*
!assets/.gitignore
protected/runtime/*
!protected/runtime/.gitignore
protected/data/*.db
themes/classic/views/
/vendor/
composer.lock
.idea/

13 changes: 11 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
## v1.0.0
### Changed
- Move DataTable options to protected array. Add __set and __get methods.
- Move DataTable options to protected array. Add __set and __get methods.

## v1.0.1
### Changed
- Improve README


## v1.0.2
### Fixed
- Server-side pagination
59 changes: 25 additions & 34 deletions src/DataTableAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use yii\base\InvalidConfigException;
use yii\data\ActiveDataProvider;
use yii\db\ActiveQuery;
use yii\web\Response;

/**
* Action for processing ajax requests from DataTables.
Expand Down Expand Up @@ -67,53 +68,21 @@ public function run()
$filterQuery
->offset(Yii::$app->request->getQueryParam('start', 0))
->limit(Yii::$app->request->getQueryParam('length', -1));
/* Begin of fix - serverSide pagination - get pagination from server side - Yii
$dataProvider = new ActiveDataProvider(['query' => $filterQuery, 'pagination' => false]);
*/
$dataProvider = new ActiveDataProvider(['query' => $filterQuery, 'pagination' => ['pageSize' => Yii::$app->request->getQueryParam('length', 10)] ]);
// End of fix - serverSide pagination - get pagination from server side - Yii
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$dataProvider = new ActiveDataProvider(['query' => $filterQuery, 'pagination' => ['pageSize' => Yii::$app->request->getQueryParam('length', 10)]]);
Yii::$app->response->format = Response::FORMAT_JSON;
try {
$response = [
'draw' => (int)$draw,
'recordsTotal' => (int)$originalQuery->count(),
'recordsFiltered' => (int)$dataProvider->getTotalCount(),
/* Begin of fix - get actual data from server according to filters, offset and limit
'data' => $dataProvider->getModels(),
*/
'data' => $filterQuery->all(),
// End of fix - get actual data from server according to filters, offset and limit
];
} catch (\Exception $e) {
return ['error' => $e->getMessage()];
}
return $response;
}

/**
* @param ActiveQuery $query
* @param array $columns
* @param array $order
* @return ActiveQuery
*/
public function applyOrder(ActiveQuery $query, $columns, $order)
{
if ($this->applyOrder !== null) {
return call_user_func($this->applyOrder, $query, $columns, $order);
}

foreach ($order as $key => $item) {
// Begin of fix - avoid failure on columns not being orderable
if (array_key_exists('orderable', $columns[$item['column']]) && $columns[$item['column']]['orderable'] === 'false') {
continue;
}
// End of fix - avoid failure on columns not being orderable
$sort = $item['dir'] == 'desc' ? SORT_DESC : SORT_ASC;
$query->addOrderBy([$columns[$item['column']]['data'] => $sort]);
}
return $query;
}

/**
* @param ActiveQuery $query
* @param array $columns
Expand All @@ -138,4 +107,26 @@ public function applyFilter(ActiveQuery $query, $columns, $search)
}
return $query;
}

/**
* @param ActiveQuery $query
* @param array $columns
* @param array $order
* @return ActiveQuery
*/
public function applyOrder(ActiveQuery $query, $columns, $order)
{
if ($this->applyOrder !== null) {
return call_user_func($this->applyOrder, $query, $columns, $order);
}

foreach ($order as $key => $item) {
if (array_key_exists('orderable', $columns[$item['column']]) && $columns[$item['column']]['orderable'] === 'false') {
continue;
}
$sort = $item['dir'] == 'desc' ? SORT_DESC : SORT_ASC;
$query->addOrderBy([$columns[$item['column']]['data'] => $sort]);
}
return $query;
}
}

0 comments on commit 4d3cf9e

Please sign in to comment.