From b74e44ca6f1a1b80244f8ec77fefaf86b9d013e7 Mon Sep 17 00:00:00 2001 From: nathangray Date: Thu, 15 Oct 2020 11:22:23 -0600 Subject: [PATCH] Etemplate: Fix multi-select / tags value sometimes got lost if we had to fetch options from the server --- api/js/etemplate/et2_widget_selectbox.js | 13 ++++++------- api/js/etemplate/et2_widget_selectbox.ts | 13 ++++++------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/api/js/etemplate/et2_widget_selectbox.js b/api/js/etemplate/et2_widget_selectbox.js index bbc4dd167d..a1fbf48b6e 100644 --- a/api/js/etemplate/et2_widget_selectbox.js +++ b/api/js/etemplate/et2_widget_selectbox.js @@ -752,7 +752,7 @@ var et2_selectbox = /** @class */ (function (_super) { this.input.trigger("liszt:updated"); } // Sometimes value gets set before options - if (this.value || this.options.empty_label || this.value === '' && this.input && this.input.children('[value=""]').length === 1) { + if (this.value || (this.options.empty_label && !this.options.multiple) || this.value === '' && this.input && this.input.children('[value=""]').length === 1) { this.set_value(this.value, true); // true = dont try to set_options, to avoid an infinit recursion } else if (this.value === null && this.options.value) { @@ -761,18 +761,17 @@ var et2_selectbox = /** @class */ (function (_super) { } }; et2_selectbox.prototype.getValue = function () { + var value = []; if (this.input == null) { - var value = []; jQuery("input:checked", this.multiOptions).each(function () { value.push(this.value); }); // we need to return null for no value instead of empty array, which gets overwritten by preserved value on server-side - this.value = value; } else { - this.value = _super.prototype.getValue.call(this); - if (this.value === null) - this.value = this.options.multiple ? [] : ""; // do NOT return null, as it does not get transmitted to server + value = _super.prototype.getValue.call(this); + if (value === null) + value = this.options.multiple ? [] : ""; // do NOT return null, as it does not get transmitted to server } - return this.value; + return value; }; et2_selectbox.prototype.isDirty = function () { if (this.input == null) { diff --git a/api/js/etemplate/et2_widget_selectbox.ts b/api/js/etemplate/et2_widget_selectbox.ts index a22a953013..22798c65e7 100644 --- a/api/js/etemplate/et2_widget_selectbox.ts +++ b/api/js/etemplate/et2_widget_selectbox.ts @@ -1010,7 +1010,7 @@ export class et2_selectbox extends et2_inputWidget this.input.trigger("liszt:updated"); } // Sometimes value gets set before options - if(this.value || this.options.empty_label || this.value === '' && this.input && this.input.children('[value=""]').length === 1) + if(this.value || (this.options.empty_label && !this.options.multiple) || this.value === '' && this.input && this.input.children('[value=""]').length === 1) { this.set_value(this.value, true); // true = dont try to set_options, to avoid an infinit recursion } @@ -1021,21 +1021,20 @@ export class et2_selectbox extends et2_inputWidget } } - getValue() + getValue() : string[] | string { + let value: string[] | string = []; if(this.input == null) { - var value = []; jQuery("input:checked",this.multiOptions).each(function(){value.push(this.value);}); // we need to return null for no value instead of empty array, which gets overwritten by preserved value on server-side - this.value = value; } else { - this.value = super.getValue(); - if (this.value === null) this.value = this.options.multiple ? [] : ""; // do NOT return null, as it does not get transmitted to server + value = super.getValue(); + if (value === null) value = this.options.multiple ? [] : ""; // do NOT return null, as it does not get transmitted to server } - return this.value; + return value; } isDirty()