Skip to content
This repository has been archived by the owner on Dec 17, 2018. It is now read-only.

Commit

Permalink
Немного оптимизирована скорость загрузки данных при перегрузке метода…
Browse files Browse the repository at this point in the history
… transform в дата-провайдере (#38)
  • Loading branch information
xxxcoltxxx authored Aug 24, 2016
1 parent d180d2b commit 9413b37
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 37 deletions.
5 changes: 4 additions & 1 deletion resources/assets/js/ngGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ angular.module('ngGrid', ['ui.bootstrap', 'daterangepicker', 'ngCookies', 'ngSan
$scope.loading = false;
$scope.loading_opacity = 0;

$scope.default_colums = null;
$scope.default_columns = null;

var ajaxDelayTimeout = 300,
ajaxDelay = false,
Expand Down Expand Up @@ -223,6 +223,9 @@ angular.module('ngGrid', ['ui.bootstrap', 'daterangepicker', 'ngCookies', 'ngSan

$scope.setCookie('allGridColumns', angular.toJson(params));

if ($scope.default_columns == null && params) {
$scope.default_columns = params;
}
};

$timeout(function () {
Expand Down
2 changes: 1 addition & 1 deletion resources/views/components/column_hider.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
data-dropdown-align-right="true"
selectpicker
ng-model="columns_hider"
ng-change="columns_hider = clearSelect(columns_hider, default_colums)"
ng-change="columns_hider = clearSelect(columns_hider, default_columns)"
>

@foreach($columns as $field => $column)
Expand Down
36 changes: 36 additions & 0 deletions src/GridDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
namespace Paramonov\Grid;


use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;


abstract class GridDataProvider
Expand Down Expand Up @@ -287,6 +289,40 @@ private function getDefaultDates()
return $filters;
}

/**
* Метод для конвертации полученных из БД данных в данные, которые должны вернуться для грида
*
* @param $item
*
* @return array
*/
public function transform($item)
{
$data = [];
if ($item instanceof Model) {
$item = $item->toArray();
}
foreach ($item as $key => $value) {
$key = $this->decodeField($key);
if ($this->getDateFormat() && in_array($key, $this->getDates())) {
$value = $value ? Carbon::parse($value)->format($this->getDateFormat()) : null;
}
$pairs = explode('.', $key);
if (count($pairs) > 1) {
$data [$pairs[0]] [$pairs[1]] = $value;
} else {
$data [$key] = $value;
}
}

return $data;
}

private function decodeField($field)
{
return str_replace(':', '.', $field);
}

final public function setName($name)
{
$this->name = $name;
Expand Down
49 changes: 14 additions & 35 deletions src/GridTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

class GridTable
{
Expand Down Expand Up @@ -86,7 +86,7 @@ private function makeQuery($sorting = null, $limit = null, $page = null)

private function mapDataWithTemplates($data, $templates)
{
$result = [];
$result = collect();
$data->map(function ($item) use ($templates, &$result) {

$item_ = !is_array($item) ? $item->toArray() : $item;
Expand All @@ -100,7 +100,7 @@ private function mapDataWithTemplates($data, $templates)

$item_[$cell_name] = $viewFunc($item);
}
$result[] = $item_;
$result->push($item_);

});

Expand All @@ -125,7 +125,6 @@ public function getData($template = null)

$total = $this->data_provider->getCount();
$data = $this->makeQuery($sorting, $limit, $page);

if ($template) {
$templates = [];
// TODO: Избавиться от foreach
Expand All @@ -148,11 +147,12 @@ public function getData($template = null)

private function makeCsvOutput($data)
{
$output = [];
$output = collect();
foreach ($data as $cells) {
$output[] = '"' . implode('";"', $cells) . '"';
$output->push('"' . implode('";"', $cells) . '"');
}
return implode("\n", $output);

return $output->implode(PHP_EOL);
}

private function encodeForCsv($string)
Expand Down Expand Up @@ -240,9 +240,9 @@ private function getHeaders($columns)

public function getSorting()
{
if (isset($_COOKIE['data_provider']) && \Request::input('use_cookie', 0)) {
if (isset($_COOKIE['data_provider']) && request('use_cookie', 0)) {
$data_provider = json_decode($_COOKIE['data_provider'], true);
if (!empty($data_provider['sorting'])) {
if (! empty($data_provider['sorting'])) {
return $data_provider['sorting'];
} else {
return $this->data_provider->getDefaultSorting();
Expand All @@ -253,39 +253,18 @@ public function getSorting()
}

/**
* @param $data
* @param Collection $data
* @return mixed
*/
private function formatData($data)
private function formatData(Collection $data)
{
$grid_data = [];
foreach ($data as $i => $item) {
if ($item instanceof Model) {
$item = $item->toArray();
}
foreach ($item as $key => $value) {
$key = $this->decodeField($key);
if ($this->data_provider->getDates() && $this->data_provider->getDateFormat() && in_array($key, $this->data_provider->getDates())) {
$value = $value ? Carbon::parse($value)->format($this->data_provider->getDateFormat()) : null;
}
$pairs = explode('.', $key);
if (count($pairs) > 1) {
$grid_data [$i] [$pairs[0]] [$pairs[1]] = $value;
} else {
$grid_data [$i] [$key] = $value;
}
}
}
return $grid_data;
return $data->transform(function($item) {
return $this->data_provider->transform($item);
});
}

private function encodeField($field)
{
return str_replace('.', ':', $field);
}

private function decodeField($field)
{
return str_replace(':', '.', $field);
}
}

0 comments on commit 9413b37

Please sign in to comment.