Datagrid format for url and filter by url query has changed and it is working decently

This commit is contained in:
prashant-webkul 2018-07-13 18:18:59 +05:30
parent 636062c481
commit 9e0cf41e0c
4 changed files with 197 additions and 41 deletions

View File

@ -69,33 +69,76 @@ class DataGridController extends Controller
// ]); // ]);
//Make case without any aliasing or joins //Make case without any aliasing or joins
/*
operations list <,>,<=,>=,!=,=,like, IN.
contains will get resolved by like after where and ranges can be resolved
by using IN or where in (1,2,3)
verbs => [
'eq' => '=',
'lt' => '<',
'gt' => '>',
'lte' => '<=',
'gte' => '>=',
'neq' => '!=',
'inc_range' => '>x AND <y',
'exc_range' => '>=x AND <=y',
'not_inc_range' => '!>x AND <y',
'not_exc_range' => '!>=x AND <=y',
]
*/
$verbs = [
'eq' => '=',
'lt' => '<',
'gt' => '>',
'lte' => '<=',
'gte' => '>=',
'neq' => 'not =',
'inc_range' => '>x AND <y', //cummutative
'exc_range' => '>=x AND <=y',
'not_inc_range' => 'not >x AND <y',
'not_exc_range' => 'not >=x AND <=y',
];
DataGrid::make([ DataGrid::make([
'name' => 'admins', 'name' => 'posts',
'table' => 'admins as a', 'table' => 'authors',
'select' => 'a.id', 'select' => 'id',
'join' => [ 'filterable' => [
[ [
'join' => 'rightjoin', 'column' => 'id',
'table' => 'roles as r', 'type' => 'integer'
'primaryKey' => 'a.role_id', ], [
'condition' => '=', 'column' => 'email',
'secondaryKey' => 'r.id', 'type' => 'string'
], [
'column' => 'first_name',
'type' => 'string'
] ]
], ],
'join' => [
// [
// 'join' => 'rightjoin',
// 'table' => 'roles as r',
// 'primaryKey' => 'a.role_id',
// 'condition' => '=',
// 'secondaryKey' => 'r.id',
// ]
],
'columns' => [ 'columns' => [
[ [
'name' => 'a.id', 'name' => 'id',
'type' => 'string', 'type' => 'string',
'label' => 'Admin ID', 'label' => 'Admin ID',
'sortable' => true, 'sortable' => true,
'filterable' => true,
], ],
[ [
'name' => 'a.email', 'name' => 'email',
'type' => 'string', 'type' => 'string',
'label' => 'Admin E-Mail', 'label' => 'Admin E-Mail',
'sortable' => true, 'sortable' => true,
'filterable' => true,
], ],
// [ // [
// 'name' => 'r.name', // 'name' => 'r.name',
@ -105,11 +148,10 @@ class DataGridController extends Controller
// 'filterable' => true, // 'filterable' => true,
// ], // ],
[ [
'name' => 'r.name', 'name' => 'first_name',
'type' => 'string', 'type' => 'string',
'label' => 'Admin Name', 'label' => 'Admin Name',
'sortable' => true, 'sortable' => true,
'filterable' => false,
// will create on run time query // will create on run time query
// 'filter' => [ // 'filter' => [
// 'function' => 'where', // orwhere // 'function' => 'where', // orwhere
@ -121,11 +163,24 @@ class DataGridController extends Controller
'onclick' => "window.alert('alert from datagrid column')" 'onclick' => "window.alert('alert from datagrid column')"
], ],
'wrapper' => function ($value, $object) { 'wrapper' => function ($value, $object) {
return '<a href="'.$value.'">' . $object->name . '</a>'; return '<a href="'.$value.'">' . $object->first_name . '</a>';
}, },
], ],
], ],
'verbs' => [
'eq' => '=',
'lt' => '<',
'gt' => '>',
'lte' => '<=',
'gte' => '>=',
'neq' => 'not =',
'contains' => 'like',
'inc_range' => '>x AND <y',
'exc_range' => '>=x AND <=y',
'not_inc_range' => 'not >x AND <y',
'not_exc_range' => 'not >=x AND <=y',
]
// 'css' => [] // 'css' => []
]); ]);

View File

@ -87,10 +87,10 @@ class DataGrid
public function make($args) public function make($args)
{ {
// list($name, $select, $table, $join, $columns) = array_values($args); // list($name, $select, $table, $join, $columns) = array_values($args);
$name = $select = $table = false; $name = $select = $filterable = $table = false;
$join = $columns = $css = []; $join = $columns = $css = $verbs = [];
extract($args); extract($args);
return $this->build($name, $select, $table, $join, $columns, $css); return $this->build($name, $select, $filterable, $table, $join, $columns, $css, $verbs);
} }
//starts buikding the queries on the basis of selects, joins and filter with //starts buikding the queries on the basis of selects, joins and filter with
@ -99,20 +99,24 @@ class DataGrid
public function build( public function build(
$name = null, $name = null,
$select = false, $select = false,
array $filterable = [],
$table = null, $table = null,
array $join = [], array $join = [],
array $columns = null, array $columns = null,
array $css = [], array $css = [],
array $verbs = [],
Pagination $pagination = null Pagination $pagination = null
) { ) {
$this->request = Request::capture(); $this->request = Request::capture();
$this->setName($name); $this->setName($name);
$this->setSelect($select); $this->setSelect($select);
$this->setFilterable($filterable);
$this->setTable($table); $this->setTable($table);
$this->setJoin($join); $this->setJoin($join);
$this->addColumns($columns, true); $this->addColumns($columns, true);
$this->setCss($css); $this->setCss($css);
$this->setVerbs($verbs);
// $this->addPagination($pagination); // $this->addPagination($pagination);
return $this; return $this;
} }
@ -141,6 +145,17 @@ class DataGrid
return $this; return $this;
} }
/**
* Set Filterable
* @return $this
*/
public function setFilterable(array $filterable)
{
$this->filterable = $filterable ?: [];
return $this;
}
/** /**
* Add Columns. * Add Columns.
* *
@ -165,12 +180,28 @@ class DataGrid
return $this; return $this;
} }
/**
* Adds the custom css rules
* @retun $this
*/
private function setCss($css = []) private function setCss($css = [])
{ {
$this->css = new Css($css); $this->css = new Css($css);
return $this->css; return $this->css;
} }
/**
* setFilterableColumns
* @return $this
*/
// public function setFilterableColumns($filterable_columns = [])
// {
// $this->join = $filterable_columns ?: [];
// return $this;
// }
/** /**
* Add Columns. * Add Columns.
* *
@ -243,6 +274,17 @@ class DataGrid
return $this; return $this;
} }
/**
* Adds expressional verbs to be used
* @return $this
*/
public function setVerbs(array $verbs)
{
$this->verbs = $verbs ?: [];
return $this;
}
/** /**
* Add Pagination. * Add Pagination.
* *
@ -314,24 +356,57 @@ class DataGrid
private function getQueryWithFilters() private function getQueryWithFilters()
{ {
//solve aliasing for table when as is used with table name //solve aliasing for table when as is used with table name
//refer https://recursiveiterator.wordpress.com/2015/02/28/laravel-query-builder-dynamic-queries/
//refer https://m.dotdev.co/writing-advanced-eloquent-search-query-filters-de8b6c2598db
//New API Structure change
//No kind of aliasing at all //No kind of aliasing at all
foreach ($this->columns as $column) { $filterable_columns = [];
if ($column->filterable) { //condition is required managing params from users i.e url or request foreach ($this->filterable as $on_column) {
if ($columnFromRequest = $this->request->offsetGet($column->correct())) { array_push($filterable_columns, $on_column['column']);
if ($filter = $columnFromRequest['filter']) { }
if ($condition = $columnFromRequest['condition']) { $queried_columns = [];
$this->query->where( $qr = $_SERVER['QUERY_STRING'];
$column->correct(), $parsed;
$condition, parse_str($qr, $parsed);
$filter foreach ($parsed as $key=>$value) {
); array_push($queried_columns, $key);
} }
foreach ($filterable_columns as $fkey=>$fvalue) {
//determines whether column is both filterable and query string is present for it.
foreach ($queried_columns as $key=>$value) {
if ($fvalue==$value) {
$conditions =$parsed[$value];
foreach ($conditions as $condition => $filter) {
$this->query->where(
$value,
$this->verbs[$condition],
$filter
);
} }
} }
} }
}
// if ($column->filterable) { //condition is required managing params from users i.e url or request
// $qr = $_SERVER['QUERY_STRING'];
// $col_name = $column->name;
// $col_name = str_replace(".", "_", $col_name);
// // dd($qr);
// dump($col_name);
// if ($columnFromfilterableRequest = $this->request->offsetGet($col_name)) {
// if ($filter = $columnFromRequest['filter']) {
// if ($condition = $columnFromRequest['condition']) {
// $this->query->where(
// $column->correctDotOnly(),
// $condition,
// $filter
// );
// }
// }
// }
// }
}
//follow a case where table is aliased and joins are not present //follow a case where table is aliased and joins are not present
} }
@ -349,7 +424,10 @@ class DataGrid
//explode if alias is available //explode if alias is available
$exploded = explode('as', $this->table); $exploded = explode('as', $this->table);
if (isset($exploded)) { // dd($exploded);
if ($exploded[0]==$this->table) {
$table_alias = false;
} else { // (isset($exploded))
$table_alias = true; $table_alias = true;
$table_name = trim($exploded[0]); $table_name = trim($exploded[0]);
$table_alias = trim($exploded[1]); $table_alias = trim($exploded[1]);
@ -404,13 +482,24 @@ class DataGrid
} }
//Check for column filter bags and resolve aliasing //Check for column filter bags and resolve aliasing
foreach ($this->columns as $column) { //run this if there are columns with filter bag
//run this if there are columns with filter bag $this->getQueryWithColumnFilters();
$this->getQueryWithColumnFilters();
}
//Run this if there are filters or sort params or range params in the urls //Run this if there are filters or sort params or range params in the urls
$this->getQueryWithFilters(); $qr = $_SERVER['QUERY_STRING'];
$parsed;
//parse_url($qr, PHP_URL_QUERY)
parse_str($qr, $parsed);
if (!empty($parsed)) {
dump('parsed url is not empty');
// $filterable_columns = [];
// foreach ($this->filterable as $on_column) {
// array_push($filterable_columns, $on_column['column']);
// }
$this->getQueryWithFilters();
} else {
dd('parsed url is empty');
}
// dump($this->query); // dump($this->query);
$this->results = $this->query->get(); $this->results = $this->query->get();

View File

@ -88,6 +88,15 @@ class Column extends AbstractFillable
return $return; return $return;
} }
public function correctDotOnly()
{
$col_name = explode('.', $this->name);
if (isset($col_name)) {
$col_name = trim($col_name[1]);
return $col_name;
}
}
public function correct($tillDot = true) public function correct($tillDot = true)
{ {
$as = explode('as', $this->name); $as = explode('as', $this->name);

View File

@ -61,6 +61,7 @@
<table class="{{ $css->table }}"> <table class="{{ $css->table }}">
<thead class="{{-- $css->thead --}}"> <thead class="{{-- $css->thead --}}">
<tr> <tr>
<th class="{{-- $css->thead_td --}}">Mass Action</th>
@foreach ($columns as $column) @foreach ($columns as $column)
<th class="{{-- $css->thead_td --}}">{!! $column->sorting() !!}</th> <th class="{{-- $css->thead_td --}}">{!! $column->sorting() !!}</th>
@endforeach @endforeach
@ -70,6 +71,7 @@
@foreach ($results as $result) @foreach ($results as $result)
<tr> <tr>
<td class="{{-- $css->tbody_td --}}"><input type="checkbox" /></td>
@foreach ($columns as $column) @foreach ($columns as $column)
<td class="{{-- $css->tbody_td --}}">{!! $column->render($result) !!}</td> <td class="{{-- $css->tbody_td --}}">{!! $column->render($result) !!}</td>
@endforeach @endforeach
@ -77,8 +79,9 @@
@endforeach @endforeach
</tbody> </tbody>
</table> </table>
<div class="{{ $css->pagination }}" style="margin-top:15px;"> {{-- @include('ui::partials.pagination') --}}
{{-- <div class="pagination"> {{-- <div class="{{ $css->pagination }}" style="margin-top:15px;">
<div class="pagination">
<a class="page-item previous"> <a class="page-item previous">
<i class="icon angle-right-icon"></i> <i class="icon angle-right-icon"></i>
</a> </a>
@ -90,8 +93,8 @@
<a href="#status/6/page/2" class="page-item next"> <a href="#status/6/page/2" class="page-item next">
<i class="icon angle-left-icon"></i> <i class="icon angle-left-icon"></i>
</a> </a>
</div> --}} </div>
</div> </div> --}}
</div> </div>
</div> </div>