From a447518c7034e3399ac5b4a06dcafabaacd2e6c2 Mon Sep 17 00:00:00 2001 From: Hieromon Ikasamo Date: Sun, 28 Apr 2019 03:37:37 +0900 Subject: [PATCH] Supports saving and loading of the selected option in the AutoConnectSelect element. (Issue #64) --- src/AutoConnectAux.cpp | 8 +++++--- src/AutoConnectElementBasis.h | 5 ++++- src/AutoConnectElementBasisImpl.h | 32 ++++++++++++++++++++++++++++--- src/AutoConnectElementJson.h | 4 +++- src/AutoConnectElementJsonImpl.h | 9 +++++++-- 5 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/AutoConnectAux.cpp b/src/AutoConnectAux.cpp index 9fb16a0..5b16f83 100644 --- a/src/AutoConnectAux.cpp +++ b/src/AutoConnectAux.cpp @@ -145,7 +145,11 @@ bool AutoConnectAux::release(const String& name) { bool AutoConnectAux::setElementValue(const String& name, const String value) { AutoConnectElement* elm = getElement(name); if (elm) { - if (elm->typeOf() != AC_Select) { + if (elm->typeOf() == AC_Select) { + AutoConnectSelect* elmSelect = reinterpret_cast(elm); + elmSelect->select(value); + } + else { if (elm->typeOf() == AC_Checkbox) { if (value == "checked") { AutoConnectCheckbox* elmCheckbox = reinterpret_cast(elm); @@ -160,8 +164,6 @@ bool AutoConnectAux::setElementValue(const String& name, const String value) { elm->value = value; return true; } - else - AC_DBG("Element<%s> value type mismatch\n", name.c_str()); } return false; } diff --git a/src/AutoConnectElementBasis.h b/src/AutoConnectElementBasis.h index 5eac66f..6022534 100644 --- a/src/AutoConnectElementBasis.h +++ b/src/AutoConnectElementBasis.h @@ -194,7 +194,7 @@ class AutoConnectRadioBasis : virtual public AutoConnectElementBasis { */ class AutoConnectSelectBasis : virtual public AutoConnectElementBasis { public: - explicit AutoConnectSelectBasis(const char* name = "", std::vector const& options = {}, const char* label = "") : AutoConnectElementBasis(name, ""), label(String(label)), _options(options) { + explicit AutoConnectSelectBasis(const char* name = "", std::vector const& options = {}, const char* label = "", const uint8_t selected = 0) : AutoConnectElementBasis(name, ""), label(String(label)), selected(selected), _options(options) { _type = AC_Select; } virtual ~AutoConnectSelectBasis() {} @@ -203,9 +203,12 @@ class AutoConnectSelectBasis : virtual public AutoConnectElementBasis { void add(const String& option) { _options.push_back(String(option)); } size_t size(void) const { return _options.size(); } const String& at(const std::size_t n) const { return _options.at(n); } + void select(const String& value); void empty(const size_t reserve = 0); + const String& value(void) const; String label; /**< A label for a subsequent input box */ + uint8_t selected; /**< Index of checked value (1-based) */ protected: std::vector _options; /**< List options array */ diff --git a/src/AutoConnectElementBasisImpl.h b/src/AutoConnectElementBasisImpl.h index c551d1c..2f5e22a 100644 --- a/src/AutoConnectElementBasisImpl.h +++ b/src/AutoConnectElementBasisImpl.h @@ -198,7 +198,7 @@ const String AutoConnectRadioBasis::toHTML(void) const { /** * Returns current selected value in the radio same group */ -const String& AutoConnectRadioBasis::value() const { +const String& AutoConnectRadioBasis::value(void) const { static const String _nullString = String(); return checked ? _values.at(checked - 1) : _nullString; } @@ -216,6 +216,19 @@ void AutoConnectSelectBasis::empty(const size_t reserve) { _options.reserve(reserve); } +/** +* Indicate an entry with the specified value in the value's collection. +* @param value The value to indicates in the collection. +*/ +void AutoConnectSelectBasis::select(const String& value) { + for (std::size_t n = 0; n < _options.size(); n++) { + if (at(n).equalsIgnoreCase(value)) { + selected = n + 1; + break; + } + } +} + /** * Generate an HTML "); - for (const String option : _options) - html += String(F("")); + uint8_t n = 1; + for (const String option : _options) { + html += String(F("