Comments (4)
Hello,
When using state binding all children elements of the container (the stack panel) are removed and disposed on every state change. In your case you are trying to reuse the same instances of button and text. But after first click the Text
component is disposed and can't be used (it's still there but rendered as an empty div).
The correct way to work with state binding is using DSL. In this case the children components are disposed too, but they are also recreated (usually with a new state).
from kvision.
So, If i want to switch between panels via reference to the Component it should be like this. Thats works for me. So it is officialy correct?
data class StackPanelBindings(
val tx: Text,
val btn: Button
)
val index = ObservableValue(
StackPanelBindings(
Text(floating = true, label = "this is label"),
Button(text = "Click me!")
)
)
stackPanel().bind(
index
) {binding ->
add(binding.btn)
add(binding.tx)
binding.btn.onClick {
activeChild = binding.tx
}
activeChild = binding.btn
}
from kvision.
No. I wouldn't call it correct.
You need to ask yourself if (and why) you need instances of Text and Button components created manually. In general it should not be required, in fact you don't even need stack panel for your goal. You can just do this:
val index = ObservableValue(0) // 0 - show button first
simplePanel().bind(index) {
if (it == 0) {
button(text = "Click me!") {
onClick {
index.value = 1
}
}
} else {
text(floating = true, label = "this is label")
}
}
span().bind(index) {
+"${it}"
}
If you want to access internal instances from outside you can declare a variable:
val index = ObservableValue(0) // 0 - show button first
var text: Text? = null
simplePanel().bind(index) {
if (it == 0) {
button(text = "Click me!") {
onClick {
index.value = 1
}
}
} else {
text = text(floating = true, label = "this is label")
}
}
span().bind(index) {
+"${it}"
}
button("set new value") {
onClick {
text?.value = "new value"
}
}
Still there are cases when externally defined components can be useful. But for such cases you should not use state binding, because you don't want to dispose and recreate you components. You just want to switch which is visible, and StackPanel is the right tool for this - just change activeIndex
property and it will work.
val index = ObservableValue(0) // 0 - show button first
val txChild = Text(floating = true, label = "this is label")
val btn = Button(text = "Click me!") {
onClick {
index.value = 1
}
}
stackPanel {
add(btn)
add(txChild)
index.subscribe {
activeIndex = it
}
}
span().bind(index) {
+"${it}"
}
from kvision.
ok I think I got this
from kvision.
Related Issues (20)
- overrides of `SimplePanel.AddAll` call refresh outside of a `singleRender` wrapper. HOT 1
- Allow KVService to have arguments with GET HOT 6
- Allow modifying response headers for Spring Boot and Micronaut apps HOT 1
- remove id parameter on get requests HOT 1
- Can't get data from Tabulator.RowComponent HOT 2
- Expandable/collapsible fieldset or panel HOT 2
- How to attch files using the Richtext Component HOT 7
- duplicated different word in kvision docs HOT 1
- did you consider to integrate kvision with ktor client? HOT 5
- can't make tabulator selectable with kvision v7.4.4 HOT 2
- My tabulator not selecting data HOT 5
- How to add more than one bottomCalc rows? HOT 2
- kvision gradle plugin not works well with Spring-Web-Flux HOT 4
- DropDown - call method instead of loading a route HOT 2
- Page loader how to. HOT 4
- Gantt and Calendar view of tasks HOT 7
- Issue integrating react-big-schedule HOT 3
- Make Alert Button Text Customizable HOT 1
- Incorrect checkbox size HOT 1
- Issue with 8.0.0-RC HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kvision.