Skip to content

Commit

Permalink
Fix: in rendering of Filter section
Browse files Browse the repository at this point in the history
  • Loading branch information
apphp committed Dec 30, 2020
1 parent 57a6a4d commit 50f81d9
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 132 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Version 0.4.1 - Dec 30, 2020
----------------------------
- Fix: in rendering of Filter section


Version 0.4.0 - Dec 30, 2020
----------------------------
- Enh: code refactoring in DataGridServiceProvider
Expand Down
207 changes: 85 additions & 122 deletions src/Apphp/DataGrid/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,137 +538,100 @@ public static function renderFields()

$errors = self::renderErrors();

// $output = $errors;
// $output .= '
// <div id="filter" class="card mb-3">
// <div class="card-header">
// <a class="pointer" onclick="$(\'#filter .card-body\').toggleClass(\'collapse\');$(this).find(\'i\').toggleClass(\'fa-minus-square\');$(\'#id\').focus()">
// Filter
// <div class="float-right">
// <i class="fa fa-plus-square'.(self::$mode == 'opened' || $filterFields['act'] === 'search' ? ' fa-minus-square' : '').'" aria-hidden="true"></i>
// </div>
// </a>
// </div>
// <div class="card-body py-1'.(self::$mode == 'opened' || $filterFields['act'] === 'search' ? '' : ' collapse').'">
// <form action="'.self::getSubmitRoute().'" method="GET">
// <input type="hidden" name="act" value="search">'.PHP_EOL;

$count = 0;
$filterFieldsContent = '<div class="row mb-n2">'.PHP_EOL;
foreach ($filters as $key => $filter) {

// Skip act field
if ($key === 'act') {
continue;
}
$count = 0;
$filterFieldsContent = '<div class="row mb-n2">'.PHP_EOL;
foreach ($filters as $key => $filter) {
// Skip act field
if ($key === 'act') {
continue;
}

// Don't show disabled filter options
if (!empty($filter['disabled'])) {
continue;
}
// Don't show disabled filter options
if (!empty($filter['disabled'])) {
continue;
}

// Specify html options
$htmlOptions = ['id' => $key, 'name' => $key, 'class' => 'form-control form-control-sm'];
if (! empty($filter['htmlOptions'])) {
$htmlOptions = array_merge($htmlOptions, $filter['htmlOptions']);
}
// Specify html options
$htmlOptions = ['id' => $key, 'name' => $key, 'class' => 'form-control form-control-sm'];
if (! empty($filter['htmlOptions'])) {
$htmlOptions = array_merge($htmlOptions, $filter['htmlOptions']);
}

// Split rows in filter after each 4 fields
if ($count != 0 && $count % self::$fieldsInRow == 0) {
$filterFieldsContent .= '</div>'.PHP_EOL;
$filterFieldsContent .= '<div class="row mb-n2">'.PHP_EOL;
}
// Split rows in filter after each 4 fields
if ($count != 0 && $count % self::$fieldsInRow == 0) {
$filterFieldsContent .= '</div>'.PHP_EOL;
$filterFieldsContent .= '<div class="row mb-n2">'.PHP_EOL;
}

$title = $filter['title'] ?? $key;
$type = $filter['type'] ?? 'string';
$source = $filter['source'] ?? [];
$value = isset($filterFields[$key]) ? $filterFields[$key] : '';

$filterFieldsContent .= '<div class="col-md-'.(self::$fieldsInRow == 6 ? 2 : 3).'">'.PHP_EOL;
$filterFieldsContent .= '<div class="form-group">'.PHP_EOL;
$filterFieldsContent .= '<label for="'.$key.'" class="col-form-label">'.$title.'</label>'.PHP_EOL;

switch ($type) {
case 'int':
case 'integer':
$filterFieldsContent .= '<input type="number" min="1" id="'.$htmlOptions['id'].'" name="'.$htmlOptions['name'].'" class="'.$htmlOptions['class'].'" value="'.$value.'">'.PHP_EOL;
break;

case 'date':
$filterFieldsContent .= '<input type="date" id="'.$htmlOptions['id'].'" name="'.$htmlOptions['name'].'" class="'.$htmlOptions['class'].'" value="'.$value.'">'.PHP_EOL;
break;

case 'set':
case 'user_status':
case 'user_active':
$filterFieldsContent .= '<select id="'.$htmlOptions['id'].'" name="'.$htmlOptions['name'].'" class="'.$htmlOptions['class'].'">'.PHP_EOL;
$filterFieldsContent .= '<option value=""></option>'.PHP_EOL;
foreach ($source as $sourceValue => $sourceLabel) {
$filterFieldsContent .= '<option value="'.$sourceValue.'"'.($value != '' && $sourceValue == $value ? ' selected' : '').'>'.$sourceLabel.'</option>'.PHP_EOL;
}
$filterFieldsContent .= '</select>'.PHP_EOL;
break;

case 'url_parameter':
$filterFieldsContent .= '<input type="hidden" name="'.$htmlOptions['name'].'" value="'.$value.'">'.PHP_EOL;
break;

case 'user_role':
$rolesList = [];
foreach ($source as $vVal => $vLabel) {
$rolesList[] = ['value' => $vVal, 'label' => $vLabel];
}

// Selected roles for milti-select
$role = [];
if ( ! empty($value)) {
if (is_string($value)) {
$value = explode(',', $value);
}
$values = array_filter($value);
foreach ($values as $vLabel) {
if (isset($source[$vLabel])) {
$role[] = ['value' => $vLabel, 'label' => $source[$vLabel]];
}
}
}
$title = $filter['title'] ?? $key;
$type = $filter['type'] ?? 'string';
$source = $filter['source'] ?? [];
$value = isset($filterFields[$key]) ? $filterFields[$key] : '';

$filterFieldsContent .= '<div class="col-md-'.(self::$fieldsInRow == 6 ? 2 : 3).'">'.PHP_EOL;
$filterFieldsContent .= '<div class="form-group">'.PHP_EOL;
$filterFieldsContent .= '<label for="'.$key.'" class="col-form-label">'.$title.'</label>'.PHP_EOL;

switch ($type) {
case 'int':
case 'integer':
$filterFieldsContent .= '<input type="number" min="1" id="'.$htmlOptions['id'].'" name="'.$htmlOptions['name'].'" class="'.$htmlOptions['class'].'" value="'.$value.'">'.PHP_EOL;
break;

case 'date':
$filterFieldsContent .= '<input type="date" id="'.$htmlOptions['id'].'" name="'.$htmlOptions['name'].'" class="'.$htmlOptions['class'].'" value="'.$value.'">'.PHP_EOL;
break;

case 'set':
case 'user_status':
case 'user_active':
$filterFieldsContent .= '<select id="'.$htmlOptions['id'].'" name="'.$htmlOptions['name'].'" class="'.$htmlOptions['class'].'">'.PHP_EOL;
$filterFieldsContent .= '<option value=""></option>'.PHP_EOL;
foreach ($source as $sourceValue => $sourceLabel) {
$filterFieldsContent .= '<option value="'.$sourceValue.'"'.($value != '' && $sourceValue == $value ? ' selected' : '').'>'.$sourceLabel.'</option>'.PHP_EOL;
}
$filterFieldsContent .= '</select>'.PHP_EOL;
break;

$filterFieldsContent .= '<vue-multiselect :id="\''.$htmlOptions['id'].'\'" :options=\''.json_encode($rolesList).'\' :values=\''.json_encode($role).'\' :placeholder-text="\'Select Role\'"></vue-multiselect>'.PHP_EOL;
break;
case 'url_parameter':
$filterFieldsContent .= '<input type="hidden" name="'.$htmlOptions['name'].'" value="'.$value.'">'.PHP_EOL;
break;

case 'user_role':
$rolesList = [];
foreach ($source as $vVal => $vLabel) {
$rolesList[] = ['value' => $vVal, 'label' => $vLabel];
}

case 'string':
default:
$filterFieldsContent .= '<input type="text" maxlength="255" id="'.$htmlOptions['id'].'" name="'.$htmlOptions['name'].'" class="'.$htmlOptions['class'].'" value="'.$value.'" autocomplete="off" />'.PHP_EOL;
break;
// Selected roles for milti-select
$role = [];
if ( ! empty($value)) {
if (is_string($value)) {
$value = explode(',', $value);
}
$values = array_filter($value);
foreach ($values as $vLabel) {
if (isset($source[$vLabel])) {
$role[] = ['value' => $vLabel, 'label' => $source[$vLabel]];
}
}
}

$filterFieldsContent .= '</div>'.PHP_EOL;
$filterFieldsContent .= '</div>'.PHP_EOL;
$filterFieldsContent .= '<vue-multiselect :id="\''.$htmlOptions['id'].'\'" :options=\''.json_encode($rolesList).'\' :values=\''.json_encode($role).'\' :placeholder-text="\'Select Role\'"></vue-multiselect>'.PHP_EOL;
break;

$count++;
}
$filterFieldsContent .= '</div>'.PHP_EOL;

// $output .= $filterFieldsContent;
//
// $output .= '<div class="row">
// <div class="col">
// <div class="form-group">
// <div class="mt-1">
// <button type="submit" class="btn btn-primary btn-sm" name="search" data-remove-empties="true">
// <i class="fa fa-search-'.(!empty($filterFields['act']) && $filterFields['act'] === 'search' ? 'minus' : 'plus').' mr-1" aria-hidden="true"></i> Search
// </button>
// '.(!empty($filterFields['act']) && $filterFields['act'] === 'search' ? '<a class="btn btn-sm" href="'.self::getCancelRoute().'">Cancel</a>' : '').'
// </div>
// </div>
// </div>
// </div>
// </form>
// </div>
// </div>';
// return $output;

// ===============================
case 'string':
default:
$filterFieldsContent .= '<input type="text" maxlength="255" id="'.$htmlOptions['id'].'" name="'.$htmlOptions['name'].'" class="'.$htmlOptions['class'].'" value="'.$value.'" autocomplete="off" />'.PHP_EOL;
break;
}

$filterFieldsContent .= '</div>'.PHP_EOL;
$filterFieldsContent .= '</div>'.PHP_EOL;

$count++;
}
$filterFieldsContent .= '</div>'.PHP_EOL;

return view(
'datagrid::filter',
Expand Down
15 changes: 8 additions & 7 deletions src/Apphp/DataGrid/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Jenssegers\Agent\Agent;
use Illuminate\Database\Eloquent\Builder;


class Pagination
Expand All @@ -41,15 +42,15 @@ class Pagination
/**
* Pagination constructor
*
* @param Illuminate\Database\Query\Builder $query
* @param Illuminate\Database\Query\Builder $query
* @param int $pageSize
* @param string $sort
* @param string $direction
* @param string $filterFields
* @param string|null $sort
* @param string|null $direction
* @param array|null $filterFields
*
* @return Pagination
*/
public static function init($query, int $pageSize = 20, string $sort = '', string $direction = '', string $filterFields = ''): Pagination
public static function init(Builder $query, int $pageSize = 20, ?string $sort = null, ?string $direction = '', ?array $filterFields = []): Pagination
{
if ( ! empty($query)) {
self::setQuery($query);
Expand Down Expand Up @@ -78,10 +79,10 @@ public static function init($query, int $pageSize = 20, string $sort = '', strin
/**
* Set query
*
* @param string $query
* @param Illuminate\Database\Query\Builder $query
* @return void
*/
public static function setQuery(string $query): void
public static function setQuery(Builder $query): void
{
self::$query = $query;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Apphp/views/filter.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{ $errors }}
{!! $errors !!}
<div id="filter" class="card mb-3">
<div class="card-header">
<a class="pointer" onclick="$('#filter .card-body').toggleClass('collapse');$(this).find('i').toggleClass('fa-minus-square');$('#id').focus()">
Expand All @@ -12,7 +12,7 @@
<form action="{{$submitRoute}}" method="GET">
<input type="hidden" name="act" value="search">

{{ $filterFieldsContent }}
{!! $filterFieldsContent !!}

<div class="row">
<div class="col">
Expand All @@ -21,7 +21,7 @@
<button type="submit" class="btn btn-primary btn-sm" name="search" data-remove-empties="true">
<i class="fa fa-search-{{(!empty($filterFields['act']) && $filterFields['act'] === 'search' ? 'minus' : 'plus')}} mr-1" aria-hidden="true"></i> Search
</button>
{{(!empty($filterFields['act']) && $filterFields['act'] === 'search' ? '<a class="btn btn-sm" href="'.$cancelRoute.'">Cancel</a>' : '')}}
{!! (!empty($filterFields['act']) && $filterFields['act'] === 'search' ? '<a class="btn btn-sm" href="'.$cancelRoute.'">Cancel</a>' : '') !!}
</div>
</div>
</div>
Expand Down

0 comments on commit 50f81d9

Please sign in to comment.