Browse Source

'select' elements now respond to changes in the 'value' attribute

Michael Ragazzon 5 years ago
parent
commit
e8611f302c

+ 3 - 0
Include/RmlUi/Core/Elements/ElementFormControlSelect.h

@@ -105,6 +105,9 @@ protected:
 	/// @return True.
 	bool GetIntrinsicDimensions(Vector2f& intrinsic_dimensions, float& intrinsic_ratio) override;
 
+	/// Respond to changed value attribute.
+	void OnAttributeChange(const ElementAttributes& changed_attributes) override;
+
 	WidgetDropDown* widget;
 };
 

+ 8 - 7
Samples/basic/databinding/data/databinding.rml

@@ -240,15 +240,16 @@ li {
 		</div>
 		<h2>Subject</h2>
 		<div>
-			<!--<select name="subject" data-event-change="selected_subject = ev.value">
+			<!--<select name="subject" data-value="selected_subject">
                 <option data-for="s : subjects" data-value="it_index">{{s}}</option>
-            </select>
-            <select name="subject" data-value="selected_subject">
-                <option value="0" selected>{{ subjects[0] }}</option>
-                <option value="1">{{ subjects[1] }}</option>
-                <option value="2">{{ subjects[2] }}</option>
-                <option value="3">{{ subjects[3] }}</option>
             </select>-->
+            <select name="subject" data-value="selected_subject">
+                <option value="0">Choose your subject</option>
+                <option value="1">Feature request</option>
+                <option value="2">Bug report</option>
+                <option value="3">Praise</option>
+                <option value="4">Criticism</option>
+            </select>
 			<div class="picker">
 				<p data-for="s : subjects" data-style-color="it_index == selected_subject ? 'red' : 'black'" data-event-click="selected_subject = it_index">{{it_index + 1 + ': ' + s}}</p>
 			</div>

+ 1 - 1
Samples/basic/databinding/src/main.cpp

@@ -283,7 +283,7 @@ namespace FormsExample {
 		bool lasagne = false;
 		Rml::String animal = "dog";
 		Rml::Vector<Rml::String> subjects = { "Choose your subject", "Feature request", "Bug report", "Praise", "Criticism" };
-		int selected_subject = 1;
+		int selected_subject = 0;
 	} my_data;
 
 	bool Initialize(Rml::Context* context)

+ 7 - 0
Source/Core/Elements/ElementFormControlSelect.cpp

@@ -166,4 +166,11 @@ bool ElementFormControlSelect::GetIntrinsicDimensions(Vector2f& intrinsic_dimens
 	return true;
 }
 
+void ElementFormControlSelect::OnAttributeChange(const ElementAttributes& changed_attributes)
+{
+	auto it = changed_attributes.find("value");
+	if (it != changed_attributes.end())
+		SetValue(it->second.Get<String>());
+}
+
 } // namespace Rml