Fix product picker default value when not required

This commit is contained in:
Alexander Sagen 2022-03-12 22:53:11 +01:00
parent 94b6bf8eef
commit aaeb81c087
No known key found for this signature in database
GPG Key ID: C1B6BD677756F470
2 changed files with 18 additions and 5 deletions

View File

@ -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
}

View File

@ -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;
}