Skip to content

Commit

Permalink
Query in Datatable
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSantiago82 committed Nov 30, 2021
1 parent a02e6fa commit a5a962f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 56 deletions.
7 changes: 7 additions & 0 deletions src/Classes/DatatableClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Chrissantiago82\Datatable\Classes;

use Laravie\SerializesQuery\Eloquent;
use Illuminate\Support\Facades\Crypt;

class DatatableClass
{
Expand Down Expand Up @@ -149,4 +151,9 @@ public function getDatatableStruct()
return $this->Result;
}

public function addQuery($query)
{
$this->Result['Query'] = Crypt::encrypt(Eloquent::serialize($query));
}

}
128 changes: 72 additions & 56 deletions src/Http/Livewire/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Livewire\Component;
use Livewire\WithPagination;
use Maatwebsite\Excel\Facades\Excel;
use Laravie\SerializesQuery\Eloquent;
use Illuminate\Support\Facades\Crypt;

class Main extends Component
{
Expand Down Expand Up @@ -50,55 +52,44 @@ public function mount($tableArr = null, $query = null, $filter = null)

public function tableStruct()
{
foreach ($this->tableArr as $itemKey => $itemArr)
{
if($itemKey == 'Columns')
{
foreach ($this->tableArr as $itemKey => $itemArr) {
if ($itemKey == 'Columns') {
$this->Data = $itemArr;
}

if($itemKey == 'Buttons')
{
if ($itemKey == 'Buttons') {
$this->ExtraData = $itemArr;
}

if($itemKey == 'Status')
{
if ($itemKey == 'Status') {
$this->StatusData = $itemArr;
}

if($itemKey == 'ExcelButton')
{
if ($itemKey == 'ExcelButton') {
$this->showExcel = $itemArr;
}

if($itemKey == 'ExcelExceptions')
{
if ($itemKey == 'ExcelExceptions') {
$this->Exceptions = $itemArr;
}

if($itemKey == 'Options')
{
if ($itemKey == 'Options') {
$this->showOptions = $itemArr;
}

if($itemKey == 'Position')
{
if ($itemKey == 'Position') {
$this->OptionsPosition = $itemArr;
}

if($itemKey == 'Counter')
{
if ($itemKey == 'Counter') {
$this->loadDataCounter();
}

if($itemKey == 'ExcelFormat')
{
if ($itemKey == 'ExcelFormat') {
$this->excelFormat = $itemArr;
}

if($itemKey == 'DefaultSort' And $this->sortDirection == '' And $this->sortBy == '')
{
if ($itemKey == 'DefaultSort' and $this->sortDirection == '' and $this->sortBy == '') {
$firstKey = array_key_first($itemArr);
$this->sortDirection = reset($itemArr);

Expand All @@ -110,20 +101,22 @@ public function tableStruct()

public function queryStruct()
{
return;

$newQuery = $this->queryResult;

if($this->sortBy !== ''){
if($this->sortDirection == 'asc'){
if ($this->sortBy !== '') {
if ($this->sortDirection == 'asc') {
$newQuery = $this->query->sortBy($this->sortBy, SORT_NATURAL | SORT_FLAG_CASE);

}else{
} else {
$newQuery = $this->query->sortByDesc($this->sortBy, SORT_NATURAL | SORT_FLAG_CASE);
}
}else{
} else {
$newQuery = $this->query->sortBy('id');
}

if ($this->search !== null AND $this->search != '') {
if ($this->search !== null and $this->search != '') {

$newQuery = $newQuery->filter(function ($value, $key) {

Expand Down Expand Up @@ -155,13 +148,11 @@ public function queryStruct()
});
}

if($this->filter != null)
{
if ($this->filter != null) {
foreach ($this->filter as $itemKey => $itemValue) {
if($itemValue !== null)
{
if ($itemValue !== null) {
$newQuery = $newQuery->where($itemKey, '=', $itemValue);
}else{
} else {
$newQuery = $newQuery->where($itemKey, '!=', $itemValue);
}

Expand All @@ -180,14 +171,11 @@ public function queryStruct()

public function loadDataCounter()
{
foreach ($this->Data as $itemKey => $dataValue)
{
if($dataValue['type'] == 'counter')
{
$counterData= $dataValue['value'];

foreach ($this->query as $query)
{
foreach ($this->Data as $itemKey => $dataValue) {
if ($dataValue['type'] == 'counter') {
$counterData = $dataValue['value'];

foreach ($this->query as $query) {
$this->counterResult[$query->$itemKey] = $query->$counterData->count();
}
}
Expand All @@ -203,9 +191,9 @@ public function Pagination($page)

public function sortBy($field)
{
if($this->sortDirection == 'asc'){
if ($this->sortDirection == 'asc') {
$this->sortDirection = 'desc';
}else{
} else {
$this->sortDirection = 'asc';
}

Expand All @@ -220,13 +208,11 @@ public function sortBy($field)
public function updatedSearch()
{
$this->saveFilterToSession();
$this->queryStruct();
}

public function loadDataTable($filterData)
{
$this->filter = $filterData;
$this->queryStruct();
}

public function emitOptions($method, $id)
Expand All @@ -245,46 +231,76 @@ public function downloadExcel()

//Full Collection

return Excel::download(new ExportExcelClass('full_collection', $query, $this->Data, $this->Exceptions, $this->excelFormat),now()->toDateString() . ' excel.xlsx');
return Excel::download(new ExportExcelClass('full_collection', $query, $this->Data, $this->Exceptions, $this->excelFormat), now()->toDateString() . ' excel.xlsx');
}

public function saveFilterToSession()
{
Session::put('tb_'.$this->tableArr['key'], $this->search);
Session::put('tb_' . $this->tableArr['key'], $this->search);
}

public function getFilterFromSession()
{
$this->search = Session::get('tb_'.$this->tableArr['key'], '');
$this->search = Session::get('tb_' . $this->tableArr['key'], '');
}

public function saveFilterSortToSession()
{
Session::put('sort_'.$this->tableArr['key'], $this->sortBy);
Session::put('sort_d'.$this->tableArr['key'], $this->sortDirection);
Session::put('sort_' . $this->tableArr['key'], $this->sortBy);
Session::put('sort_d' . $this->tableArr['key'], $this->sortDirection);

}

public function getFilterSortFromSession()
{
$this->sortBy = Session::get('sort_'.$this->tableArr['key'], '');
$this->sortDirection = Session::get('sort_d'.$this->tableArr['key'], '');
$this->sortBy = Session::get('sort_' . $this->tableArr['key'], '');
$this->sortDirection = Session::get('sort_d' . $this->tableArr['key'], '');

}


protected function buildQuery()
{
$query = Eloquent::unserialize(Crypt::decrypt($this->tableArr['Query']));

if ($this->sortBy !== '') {
$query->orderBy($this->sortBy, $this->sortDirection);
} else {
$query->orderBy('id');
}


$query->where(function($q) {
foreach ($this->tableArr['Columns'] as $key => $column) {

if (Str::contains($key, '.') === false) {
$q->orWhere($key, 'LIKE', '%'.$this->search.'%');
} else {
$exploted = explode('.', $key);
$q->orWhereHas($exploted[0], function ($re) use ($exploted){
$re->where($exploted[1], 'LIKE', '%'.$this->search.'%');
});
}
}
});



return $query;
}


public function render()
{
/*
$perPage = $this->perPage;

$collection = $this->queryStruct();

$items = $collection->forPage($this->page, $perPage);

$paginator = new LengthAwarePaginator($items, $collection->count(), $perPage, $this->page);

$this->resetPage();
*/

return view('datatable::main', ['dataResult' => $paginator] );
return view('datatable::main', ['dataResult' => $this->buildQuery()->paginate($this->perPage)]);

}
}

0 comments on commit a5a962f

Please sign in to comment.