Fix bug on select2 in modal filters, add filters by tags and shelves on articles

This commit is contained in:
Ludovic CANDELLIER
2022-01-30 15:04:08 +01:00
parent ba04007f86
commit 88d87ceaab
6 changed files with 64 additions and 15 deletions

View File

@@ -17,6 +17,8 @@ class ArticlesDataTable extends DataTable
{ {
$model = $model::with(['article_nature', 'image'])->withCount(['categories', 'images', 'offers', 'tags']); $model = $model::with(['article_nature', 'image'])->withCount(['categories', 'images', 'offers', 'tags']);
$model = self::filterByArticleNature($model); $model = self::filterByArticleNature($model);
$model = self::filterByCategory($model);
$model = self::filterByTag($model);
return $this->buildQuery($model); return $this->buildQuery($model);
} }
@@ -26,6 +28,18 @@ class ArticlesDataTable extends DataTable
return $article_nature_id ? $model->byArticleNature($article_nature_id) : $model; return $article_nature_id ? $model->byArticleNature($article_nature_id) : $model;
} }
public static function filterByCategory($model, $category_id = false)
{
$category_id = $category_id ? $category_id : self::isFilteredByField('category_id');
return $category_id ? $model->byCategory($category_id) : $model;
}
public static function filterByTag($model, $tag_id = false)
{
$tag_id = $tag_id ? $tag_id : self::isFilteredByField('tag_id');
return $tag_id ? $model->byTag($tag_id) : $model;
}
public function modifier($datatables) public function modifier($datatables)
{ {
$datatables $datatables

View File

@@ -6,6 +6,9 @@ use Illuminate\Http\Request;
use App\Repositories\Shop\Articles; use App\Repositories\Shop\Articles;
use App\Repositories\Shop\ArticleNatures; use App\Repositories\Shop\ArticleNatures;
use App\Repositories\Shop\Categories;
use App\Repositories\Shop\Tags;
use App\Datatables\Shop\ArticlesDataTable; use App\Datatables\Shop\ArticlesDataTable;
class ArticleController extends Controller class ArticleController extends Controller
@@ -19,6 +22,8 @@ class ArticleController extends Controller
public function index(ArticlesDataTable $dataTable) public function index(ArticlesDataTable $dataTable)
{ {
$data['article_natures'] = ArticleNatures::getOptions(); $data['article_natures'] = ArticleNatures::getOptions();
$data['categories'] = Categories::getOptions();
$data['tags'] = Tags::getOptionsFullName();
return $dataTable->render('Admin.Shop.Articles.list', $data); return $dataTable->render('Admin.Shop.Articles.list', $data);
} }

View File

@@ -27,9 +27,9 @@ class Tags
public static function getOptionsFullName() public static function getOptionsFullName()
{ {
$tags = Tag::with('group')->get(); $tags = Tag::with('group')->get()->toArray();
foreach ($tags as $tag) { foreach ($tags as $tag) {
$data[$tag->id] = $tag->group->name . '-' . $tag->name; $data[$tag['id']] = $tag['group']['name'] . '-' . $tag['name'];
} }
return $data; return $data;
} }

View File

@@ -1,8 +1,20 @@
<form id="{{ $model }}-filters"> <form id="{{ $model }}-filters">
<div class="row"> <div class="row mb-3">
<label class="col-4">{{ __('shop.article_natures.title') }}</label> <label class="col-4">{{ __('shop.article_natures.title') }}</label>
<div class="col-8"> <div class="col-8">
@include('components.form.select', ['name' => 'article_nature_id', 'list' => $article_natures ?? [], 'value' => $filters['article_nature_id'] ?? null, 'class' => 'form-control-sm select2', 'with_empty' => ' ']) @include('components.form.select', ['name' => 'article_nature_id', 'list' => $article_natures ?? [], 'value' => $filters['article_nature_id'] ?? null, 'class' => 'form-control-sm select2', 'with_empty' => ' '])
</div> </div>
</div> </div>
<div class="row mb-3">
<label class="col-4">{{ __('shop.shelves.title') }}</label>
<div class="col-8">
@include('components.form.select', ['name' => 'category_id', 'list' => $categories ?? [], 'value' => $filters['category_id'] ?? null, 'class' => 'form-control-sm select2', 'with_empty' => ' '])
</div>
</div>
<div class="row mb-3">
<label class="col-4">{{ __('shop.tags.title') }}</label>
<div class="col-8">
@include('components.form.select', ['name' => 'tag_id', 'list' => $tags ?? [], 'value' => $filters['tag_id'] ?? null, 'class' => 'form-control-sm select2', 'with_empty' => ' '])
</div>
</div>
</form> </form>

View File

@@ -9,7 +9,7 @@
var $filter = $('#{{ $model ?? null }}-table-header .btn-filter'); var $filter = $('#{{ $model ?? null }}-table-header .btn-filter');
$('#modal-{{ $model ?? null }}-filters').on('shown.bs.modal', function () { $('#modal-{{ $model ?? null }}-filters').on('shown.bs.modal', function () {
initSelect2(); initSelect2('.select2', '#modal-{{ $model ?? null }}-filters');
}); });
$('#modal-{{ $model ?? null }}-filters .apply').click(function() { $('#modal-{{ $model ?? null }}-filters .apply').click(function() {

View File

@@ -4,8 +4,13 @@
<script src="/assets/plugins/select2/js/i18n/{{ App::getLocale() }}.js"></script> <script src="/assets/plugins/select2/js/i18n/{{ App::getLocale() }}.js"></script>
<script> <script>
function initSelect2(sel) { function initSelect2(sel, parent) {
console.log('initSelect2');
// $.fn.modal.Constructor.prototype.enforceFocus = function() {};
var selector = (typeof(sel) == 'undefined') ? '.select2' : sel; var selector = (typeof(sel) == 'undefined') ? '.select2' : sel;
if (typeof(parent) == 'undefined') {
$(selector).select2({ $(selector).select2({
placeholder: "{{ __('select_a_value') }}", placeholder: "{{ __('select_a_value') }}",
allowClear: false, allowClear: false,
@@ -13,16 +18,29 @@
value: '100%' value: '100%'
} }
}); });
} else {
$(document).on('select2:open',(e) => { $(selector).select2({
placeholder: "{{ __('select_a_value') }}",
allowClear: true,
dropdownParent: $(parent),
width: {
value: '100%'
}
});
}
/*
$(selector).on('select2:open',(e) => {
let t = $(e.target); let t = $(e.target);
console.log(t); console.log(t);
if (t && t.length) { if (t && t.length) {
let id = t[0].id || t[0].name; let id = t[0].id || t[0].name;
let item = document.querySelector(`input[aria-controls*='${id}']`); console.log(id);
let item = $(`input[aria-controls*='${id}']`);
console.log(item);
return item ? item.focus() : false; return item ? item.focus() : false;
} }
}); });
*/
} }
</script> </script>
@endpush @endpush