From aaeb81c0879acef800151cc6faec25087edd8565 Mon Sep 17 00:00:00 2001 From: Alexander Sagen Date: Sat, 12 Mar 2022 22:53:11 +0100 Subject: [PATCH] Fix product picker default value when not required --- public/viewjs/components/productpicker.js | 21 +++++++++++++++++---- public/viewjs/stockentries.js | 2 +- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/public/viewjs/components/productpicker.js b/public/viewjs/components/productpicker.js index 594f3315..021a2d4b 100644 --- a/public/viewjs/components/productpicker.js +++ b/public/viewjs/components/productpicker.js @@ -91,6 +91,10 @@ Grocy.Components.ProductPicker.Enable = function() { return this; } +Grocy.Components.ProductPicker.IsRequired = function() { + return this.GetPicker().prop("required"); +} + Grocy.Components.ProductPicker.Require = function() { this.GetPicker().prop("required", true); return this; @@ -147,7 +151,10 @@ Grocy.Components.ProductPicker.IsOpen = function() { // initialize Select2 product picker var lastProductSearchTerm = ''; Grocy.Components.ProductPicker.GetPicker().select2({ - selectOnClose: true, + placeholder: Grocy.Components.ProductPicker.IsRequired() ? null : __t('All'), + placeholderOption: 'all', + selectOnClose: Grocy.Components.ProductPicker.IsRequired(), + allowClear: !Grocy.Components.ProductPicker.IsRequired(), ajax: { delay: 150, transport: function(params, success, failure) { @@ -189,7 +196,10 @@ Grocy.Components.ProductPicker.GetPicker().select2({ var handleEmptyResponse = function() { if (responded || complete < xhrs.length) return; success({ - results: [], + results: Grocy.Components.ProductPicker.IsRequired() ? [] : [{ + id: 'all', + text: __t('All'), + }], pagination: { more: false } @@ -221,12 +231,15 @@ Grocy.Components.ProductPicker.GetPicker().select2({ Grocy.Components.ProductPicker.SetState('barcode', term); success({ - results: results.map(function(result) { + results: (Grocy.Components.ProductPicker.IsRequired() ? [] : [{ + id: 'all', + text: __t('All'), + }]).concat(results.map(function(result) { return { id: result.id, text: result.name }; - }), + })), pagination: { more: page * results_per_page < meta.recordsFiltered } diff --git a/public/viewjs/stockentries.js b/public/viewjs/stockentries.js index edfa1086..778ec54e 100644 --- a/public/viewjs/stockentries.js +++ b/public/viewjs/stockentries.js @@ -46,7 +46,7 @@ stockEntriesTable.columns.adjust().draw(); $.fn.dataTable.ext.search.push(function(settings, data, dataIndex) { var productId = Grocy.Components.ProductPicker.GetValue(); - if ((isNaN(productId) || productId == "" || productId == data[1])) { + if ((isNaN(productId) || productId == "" || productId == data[1] || productId === null || productId === 'all')) { return true; }