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 5799eb36fc
commit 5e5f12ddb2
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 = self::filterByArticleNature($model);
$model = self::filterByCategory($model);
$model = self::filterByTag($model);
return $this->buildQuery($model);
}
@@ -26,6 +28,18 @@ class ArticlesDataTable extends DataTable
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)
{
$datatables

View File

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

View File

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

View File

@@ -1,8 +1,20 @@
<form id="{{ $model }}-filters">
<div class="row">
<div class="row mb-3">
<label class="col-4">{{ __('shop.article_natures.title') }}</label>
<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' => ' '])
</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>

View File

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

View File

@@ -4,25 +4,43 @@
<script src="/assets/plugins/select2/js/i18n/{{ App::getLocale() }}.js"></script>
<script>
function initSelect2(sel) {
var selector = (typeof(sel) == 'undefined') ? '.select2' : sel;
$(selector).select2({
placeholder: "{{ __('select_a_value') }}",
allowClear: false,
width: {
value: '100%'
}
});
function initSelect2(sel, parent) {
console.log('initSelect2');
// $.fn.modal.Constructor.prototype.enforceFocus = function() {};
$(document).on('select2:open',(e) => {
var selector = (typeof(sel) == 'undefined') ? '.select2' : sel;
if (typeof(parent) == 'undefined') {
$(selector).select2({
placeholder: "{{ __('select_a_value') }}",
allowClear: false,
width: {
value: '100%'
}
});
} else {
$(selector).select2({
placeholder: "{{ __('select_a_value') }}",
allowClear: true,
dropdownParent: $(parent),
width: {
value: '100%'
}
});
}
/*
$(selector).on('select2:open',(e) => {
let t = $(e.target);
console.log(t);
if (t && t.length) {
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;
}
});
*/
}
</script>
@endpush