Datagrid working without dots in the URL, next is to put the validation for valid aliasing if aliasing is used and set true

This commit is contained in:
prashant-webkul 2018-08-08 11:03:23 +05:30
parent 7ddcba62f9
commit 2c90d713af
6 changed files with 58 additions and 136 deletions

View File

@ -27,122 +27,11 @@ class UserComposer
public function compose(View $view)
{
// $datagrid = DataGrid::make([
// 'name' => 'Admins',
// 'table' => 'admins',
// 'select' => 'id',
// 'aliased' => false, //use this with false as default and true in case of joins
// 'perpage' => 2,
// //don't use aliasing in case of filters
// 'filterable' => [
// [
// 'column' => 'name',
// 'type' => 'string',
// 'label' => 'Admin Name'
// ]
// ],
// //don't use aliasing in case of searchables
// 'searchable' => [
// [
// 'column' => 'email',
// 'type' => 'string',
// 'label' => 'Admin E-Mail'
// ], [
// 'column' => 'name',
// 'type' => 'string',
// 'label' => 'Admin Name'
// ]
// ],
// 'massoperations' =>[
// [
// 'route' => route('admin.datagrid.delete'),
// 'method' => 'DELETE',
// 'label' => 'Delete',
// 'type' => 'button',
// ],
// // [
// // 'route' => route('admin.datagrid.index'),
// // 'method' => 'POST',
// // 'label' => 'View Grid',
// // 'type' => 'select',
// // 'options' =>[
// // 1 => 'Edit',
// // 2 => 'Set',
// // 3 => 'Change Status'
// // ]
// // ],
// ],
// 'join' => [
// // [
// // 'join' => 'leftjoin',
// // 'table' => 'roles as r',
// // 'primaryKey' => 'u.role_id',
// // 'condition' => '=',
// // 'secondaryKey' => 'r.id',
// // ]
// ],
// //use aliasing on secodary columns if join is performed
// 'columns' => [
// [
// 'name' => 'id',
// 'type' => 'string',
// 'label' => 'Admin ID',
// 'sortable' => true,
// ],
// [
// 'name' => 'name',
// 'type' => 'string',
// 'label' => 'Admin Name',
// 'sortable' => true,
// ],
// [
// 'name' => 'email',
// 'type' => 'string',
// 'label' => 'Admin E-Mail',
// 'sortable' => true,
// ],
// // [
// // 'name' => 'a.first_name',
// // 'type' => 'string',
// // 'label' => 'Admin Name',
// // 'sortable' => true,
// // 'filterable' => true,
// // // will create on run time query
// // // 'filter' => [
// // // 'function' => 'where', // orwhere
// // // 'condition' => ['name', '=', 'Admin'] // multiarray
// // // ],
// // 'attributes' => [
// // 'class' => 'class-a class-b',
// // 'data-attr' => 'whatever you want',
// // 'onclick' => "window.alert('alert from datagrid column')"
// // ],
// // 'wrapper' => function ($value, $object) {
// // return '<a href="'.$value.'">' . $object->first_name . '</a>';
// // },
// // ],
// ],
// 'operators' => [
// 'eq' => "=",
// 'lt' => "<",
// 'gt' => ">",
// 'lte' => "<=",
// 'gte' => ">=",
// 'neqs' => "<>",
// 'neqn' => "!=",
// 'like' => "like",
// 'nlike' => "not like",
// ],
// // 'css' => []
// ]);
$datagrid = DataGrid::make([
'name' => 'Admins',
'table' => 'admins as u',
'select' => 'u.id',
'perpage' => 2,
'perpage' => 5,
'aliased' => true, //use this with false as default and true in case of joins
'massoperations' =>[
@ -168,10 +57,12 @@ class UserComposer
[
'type' => 'Delete',
'route' => route('admin.datagrid.delete'),
'confirm_text' => 'Do you really want to do this?',
'icon' => 'icon trash-icon',
], [
'type' => 'Edit',
'route' => route('admin.datagrid.delete'),
'confirm_text' => 'Do you really want to do this?',
'icon' => 'icon pencil-lg-icon',
],
],
@ -254,14 +145,17 @@ class UserComposer
'filterable' => [
[
'column' => 'u.name',
'alias' => 'Name',
'type' => 'string',
'label' => 'Admin Name'
], [
'column' => 'u.id',
'alias' => 'ID',
'type' => 'number',
'label' => 'Admin ID'
], [
'column' => 'r.id',
'alias' => 'Role_ID',
'type' => 'number',
'label' => 'Role ID'
]
@ -296,4 +190,4 @@ class UserComposer
$view->with('datagrid', $datagrid);
// $view->with('count', $this->users->count());
}
}
}

View File

@ -452,6 +452,22 @@ class DataGrid
// dd($this->query);
}
/**
* To find the alias
* of the column and
* by taking the column
* name.
* @return string
*/
public function findAlias($column_alias) {
foreach($this->columns as $column) {
if($column->alias == $column_alias) {
return $column->name;
}
}
}
/**
* Parse the URL
* and get it ready
@ -528,7 +544,7 @@ class DataGrid
if ($key=="sort") {
//resolve the case with the column helper class
if(substr_count($key,'_') >= 1)
$column_name = str_replace_first('_', '.', $key);
$column_name = $this->findAlias($key);
//case that don't need any resolving
$count_keys = count(array_keys($value));
@ -550,8 +566,7 @@ class DataGrid
}
});
} else {
$column_name = str_replace_first('_', '.', $key);
$column_name = $this->findAlias($key);
if (array_keys($value)[0]=="like" || array_keys($value)[0]=="nlike") {
foreach ($value as $condition => $filter_value) {
$this->query->where(
@ -602,9 +617,7 @@ class DataGrid
throw new \Exception('Multiple Search keys Found, Please Resolve the URL Manually.');
} else {
$column_name = $key;
if (array_keys($value)[0]=="like" || array_keys($value)[0]=="nlike") {
foreach ($value as $condition => $filter_value) {
$this->query->where(
@ -707,13 +720,15 @@ class DataGrid
//Check for column filter bags and resolve aliasing
$this->getQueryWithColumnFilters();
if (!empty($parsed)) {
$this->getQueryWithFilters();
}
// $this->results = $this->query->get();
// return $this->results;
$this->results = $this->query->get();
$this->results = $this->query->paginate($this->perpage)->appends(request()->except('page'));
return $this->results;
} else {
@ -721,12 +736,17 @@ class DataGrid
$this->query = DB::table($this->table);
$this->getSelect();
$this->getQueryWithColumnFilters();
if (!empty($parsed)) {
$this->getQueryWithFilters();
}
$this->results = $this->query->get();
$this->results = $this->query->paginate($this->perpage)->appends(request()->except('page'));
return $this->results;
}
}
@ -748,4 +768,4 @@ class DataGrid
'actions' => $this->actions,
]);
}
}
}

View File

@ -832,7 +832,7 @@ h2 {
}
.table {
thead {
.xyz {
.mass-action-wrapper {
display: flex;
flex-direction: row;
justify-content: flex-start;
@ -852,10 +852,12 @@ h2 {
}
}
tr {
th.sort-head {
th.grid_head {
cursor:pointer;
.sort-down-icon {
margin-left: 8px;
margin-top: -5px;
margin-left: 5px;
margin-top: -3px;
vertical-align: middle;
cursor: pointer;
}

View File

@ -13,7 +13,7 @@
<div class="dropdown-container">
<ul>
@foreach($columns as $column)
<li data-name="{{ $column->name }}">
<li data-name="{{ $column->alias }}">
{{ $column->label }}
</li>
@endforeach
@ -35,7 +35,7 @@
<select class="filter-column-select">
<option selected disabled>Select Column</option>
@foreach($filterable as $fcol)
<option value="{{ $fcol['column'] }}" data-type="{{ $fcol['type'] }}" data-label="{{ $fcol['label'] }}">{{ $fcol['label'] }}</option>
<option value="{{ $fcol['alias'] }}" data-type="{{ $fcol['type'] }}" data-label="{{ $fcol['label'] }}">{{ $fcol['label'] }}</option>
@endforeach
</select>
</li>
@ -113,4 +113,3 @@
</span> --}}
</div>
</div>

View File

@ -391,6 +391,13 @@
}
}
}
function confirm_click(x){
if (confirm(x)) {
} else {
return false;
}
}
</script>
@endsection

View File

@ -4,11 +4,11 @@
<thead>
<tr class="mass-action" style="display: none;">
<tr class="mass-action" style="display: none; height:63px;">
<th colspan="{{ count($columns)+1 }}">
<div class="xyz">
<div class="mass-action-wrapper">
<span class="massaction-remove">
<span class="icon checkbox-dash-icon"></span>
@ -90,19 +90,19 @@
</span>
</th>
@foreach ($columns as $column) @if($column->sortable == "true")
<th class="labelled-col grid_head sort-head"
@if(strpos($column->name, ' as '))
<th class="grid_head"
@if(strpos($column->alias, ' as '))
<?php $exploded_name = explode(' as ',$column->name); ?>
data-column-name="{{ $exploded_name[0] }}"
@else
data-column-name="{{ $column->name }}"
data-column-name="{{ $column->alias }}"
@endif
data-column-label="{{ $column->label }}"
data-column-sort="asc">{!! $column->sorting() !!}<span class="icon sort-down-icon"></span>
</th>
@else
<th class="labelled-col grid_head" data-column-name="{{ $column->name }}" data-column-label="{{ $column->label }}">{!! $column->sorting() !!}</th>
<th class="labelled-col grid_head" data-column-name="{{ $column->alias }}" data-column-label="{{ $column->label }}">{!! $column->sorting() !!}</th>
@endif @endforeach
@foreach($actions as $action)
<th>
@ -125,9 +125,9 @@
@endforeach
@foreach($actions as $action)
<td class="action">
<span class="action-{{ $action['type'] }}" id="{{ $result->id }}">
<a @if($action['type']=="Edit") href="{{ url()->current().'/edit/'.$result->id }}" @elseif($action['type']=="Delete") href="{{ url()->current().'/delete/'.$result->id }}" @endif class="Action-{{ $action['type'] }}" id="{{ $result->id }}" onclick="return confirm_click('{{ $action['confirm_text'] }}');">
<i class="{{ $action['icon'] }}"></i>
</span>
</a>
</td>
@endforeach
</tr>