pagination fixes

This commit is contained in:
Prashant Singh 2019-01-11 16:55:10 +05:30
parent 9b85622533
commit 128e316265
6 changed files with 57 additions and 47 deletions

View File

@ -13,8 +13,9 @@ return [
* * Accepted Values = Array
*/
'order' => [
'column' => 'id',
'direction' => 'desc'
'default' => 'descending',
'descending' => 'desc',
'ascending' => 'asc'
],
/**
@ -84,6 +85,6 @@ return [
7 => "orders",
8 => "limit",
9 => "offset",
10 => "lock",
10 => "lock"
]
];

View File

@ -15,6 +15,10 @@ class AttributeDataGrid extends DataGrid
{
public $allColumns = [];
public function __construct() {
$this->itemsPerPage = 5;
}
public function prepareQueryBuilder()
{
$queryBuilder = DB::table('attributes')->select('id')->addSelect('id', 'code', 'admin_name', 'type', 'is_required', 'is_unique', 'value_per_locale', 'value_per_channel');
@ -22,7 +26,8 @@ class AttributeDataGrid extends DataGrid
$this->setQueryBuilder($queryBuilder);
}
public function setIndex() {
public function setIndex()
{
$this->index = 'id'; //the column that needs to be treated as index column
}

View File

@ -3,6 +3,7 @@
namespace Webkul\Ui\DataGrid;
use Illuminate\Http\Request;
/**
* Product Data Grid class
*
@ -21,7 +22,7 @@ abstract class DataGrid
protected $request;
protected $parse;
protected $enableMassAction = false;
// protected $gridName = null;
protected $itemsPerPage = 0;
abstract public function prepareMassActions();
abstract public function prepareActions();
@ -32,19 +33,19 @@ abstract class DataGrid
/**
* Parse the URL and get it ready to be used.
*/
private function parse()
private function parseUrl()
{
$parsed = [];
$parsedUrl = [];
$unparsed = url()->full();
if (count(explode('?', $unparsed)) > 1) {
$to_be_parsed = explode('?', $unparsed)[1];
parse_str($to_be_parsed, $parsed);
unset($parsed['page']);
parse_str($to_be_parsed, $parsedUrl);
unset($parsedUrl['page']);
}
return $parsed;
return $parsedUrl;
}
public function addColumn($column)
@ -72,35 +73,31 @@ abstract class DataGrid
public function addMassAction($massAction)
{
array_push($this->massActions, $massAction);
$this->enableMassAction = true;
}
public function getCollection()
{
$p = $this->parse();
$parsedUrl = $this->parseUrl();
if(count($p)) {
$filteredOrSortedCollection = $this->sortOrFilterCollection($this->collection = $this->queryBuilder, $p);
if(count($parsedUrl)) {
$filteredOrSortedCollection = $this->sortOrFilterCollection($this->collection = $this->queryBuilder, $parsedUrl);
// return $filteredOrSortedCollection->get();
if (config()->has('datagrid.pagination')) {
return $filteredOrSortedCollection->paginate(config('datagrid.pagination'));
if($this->itemsPerPage > 0) {
return $filteredOrSortedCollection->paginate($this->itemsPerPage);
} else {
return $filteredOrSortedCollection->get();
}
}
if (config()->has('datagrid.pagination')) {
$this->collection = $this->queryBuilder->paginate(config('datagrid.pagination'));
if ($this->itemsPerPage > 0) {
$this->collection = $this->queryBuilder->paginate($this->itemsPerPage);
} else {
$this->collection = $this->queryBuilder->get();
}
if ($this->collection) {
return $this->collection;
} else {
dd('no records found');
}
return $this->collection;
}
/**
@ -108,7 +105,8 @@ abstract class DataGrid
*
* @return string
*/
public function findColumnType($columnAlias) {
public function findColumnType($columnAlias)
{
foreach($this->allColumns as $column) {
if($column['alias'] == $columnAlias) {
return [$column['type'], $column['index']];
@ -116,8 +114,8 @@ abstract class DataGrid
}
}
public function sortOrFilterCollection($collection, $parseInfo) {
public function sortOrFilterCollection($collection, $parseInfo)
{
foreach($parseInfo as $key => $info) {
$columnType = $this->findColumnType($key)[0];
$columnName = $this->findColumnType($key)[1];
@ -192,6 +190,6 @@ abstract class DataGrid
$this->prepareQueryBuilder();
return view('ui::datagrid.table')->with('results', ['records' => $this->getCollection(), 'columns' => $this->allColumns, 'actions' => $this->actions, 'massactions' => $this->massActions, 'index' => $this->index]);
return view('ui::datagrid.table')->with('results', ['records' => $this->getCollection(), 'columns' => $this->allColumns, 'actions' => $this->actions, 'massactions' => $this->massActions, 'index' => $this->index, 'enableMassActions' => $this->enableMassAction, 'norecords' => trans('ui::datagrid.no-records')]);
}
}

View File

@ -2,12 +2,14 @@
return [
'datagrid' => [
'actions' => 'Actions',
'massaction' => [
'mass-delete-confirm' => 'Do you really want to delete these selected :resource?',
'mass-update-status' => 'Do you really want to update status of these selected :resource?',
'delete' => 'Do you really want to delete this :resource?',
'edit' => 'Do you really want to edit this :resource?',
],
'no-records' => 'No Records Found',
'filter-fields-missing' => 'Some of the required field is null, please check column, condition and value properly'
]

View File

@ -1,29 +1,31 @@
<tbody>
@foreach($records as $key => $record)
<tr>
<td>
<span class="checkbox">
<input type="checkbox" v-model="dataIds" @change="select" value="{{ $record->{$index} }}">
@if($enableMassAction)
<td>
<span class="checkbox">
<input type="checkbox" v-model="dataIds" @change="select" value="{{ $record->{$index} }}">
<label class="checkbox-view" for="checkbox1"></label>
</span>
</td>
<label class="checkbox-view" for="checkbox"></label>
</span>
</td>
@endif
@foreach($columns as $column)
@php
$tableIndex = explode('.', $column['index']);
$columnIndex = explode('.', $column['index']);
$tableIndex = end($tableIndex);
$columnIndex = end($columnIndex);
@endphp
@if(isset($column['wrapper']))
@if(isset($column['closure']) && $column['closure'] == true)
<td>{!! $column['wrapper']($record->{$tableIndex}) !!}</td>
<td>{!! $column['wrapper']($record->{$columnIndex}) !!}</td>
@else
<td>{{ $column['wrapper']($record->{$tableIndex}) }}</td>
<td>{{ $column['wrapper']($record->{$columnIndex}) }}</td>
@endif
@else
<td>{{ $record->{$tableIndex} }}</td>
<td>{{ $record->{$columnIndex} }}</td>
@endif
@endforeach

View File

@ -2,7 +2,7 @@
<testgrid-filters></testgrid-filters>
@if(config('datagrid.pagination'))
@include('ui::testgrid.pagination', ['results' => $results['records']])
@include('ui::datagrid.pagination', ['results' => $results['records']])
@endif
@push('scripts')
@ -185,13 +185,15 @@
<thead v-if="massActionsToggle == false">
<tr>
<th class="grid_head" id="mastercheckbox" style="width: 50px;">
<span class="checkbox">
<input type="checkbox" v-model="allSelected" v-on:change="selectAll">
@if($results['enableMassActions'])
<th class="grid_head" id="mastercheckbox" style="width: 50px;">
<span class="checkbox">
<input type="checkbox" v-model="allSelected" v-on:change="selectAll">
<label class="checkbox-view" for="checkbox"></label>
</span>
</th>
<label class="checkbox-view" for="checkbox"></label>
</span>
</th>
@endif
@foreach($results['columns'] as $key => $column)
<th class="grid_head" style="width: {{ $column['width'] }}" v-on:click="sortCollection('{{ $column['alias'] }}')">
@ -205,7 +207,7 @@
</tr>
</thead>
@include('ui::datagrid.body', ['records' => $results['records'], 'actions' => $results['actions'], 'index' => $results['index'], 'columns' => $results['columns']])
@include('ui::datagrid.body', ['records' => $results['records'], 'actions' => $results['actions'], 'index' => $results['index'], 'columns' => $results['columns'],'enableMassAction' => $results['enableMassActions']])
</table>
</div>
</script>