I would like to ask some help. I have a project which is to create a widget for a smart home wall panel and to do so I started to use f7-swiper in the code. The question is that is it possible to send the f7-swiper to the init slider with a item state or widget variable?
Yes, the swiper has an extensive list of properties you can configure:
The initialSlide property will start the swiper at the slide number (zero-indexed) that you need. And because the swiper is re-rendered when a configuration expression changes, it will even change the slide when dynamic factor (item state or variable) changes.
I tried to use that with a ,homeā button and a variable, but I was not able to set it to the first swiper.
the variable is vars.display which can have home, showSecurity etc value.
Your values never changes so the expression doesnāt change so the component never re-renders.
I guess I misunderstood the question initially. If it is just a matter of being able to return the user to the initial slide all the time and not actually changing what the initial slide is, then you donāt need the initialSlide property (or you can just set it to the constant index that you wish to use).
You basically want to refresh the component so that it thinks itās being displayed for the first time and therefore starts at its set initial slide. What you want instead is the ākeyā property. Hereās some explanation and example:
You donāt want a rule that causes the item state to change automatically, but the first part about refreshing the component is whatās important.
In your case you can use a variable instead of a item state in the key expression, BUT you have to make sure that variable actually changes whenever you want to move back to the home slide. So, instead of just setting the variable to a static value, you have to set it to something like:
Just like it does in the example link. You need to make it unique. So, if thereās any chance at all of there being more than one of these components active at a time then you need the Math.random() or some other way to make it unique. And then you need to combine that with whatever is changing: your variable value, item state, or whatever. That second part has to actually change, however, not just get sent the same value it already has. String concatenation is probably a little safer than numerical summation. So, if you want to make sure that it is always a string concatenation use a string template:
key: =`${unique value here}-${variable value here}`
Without (...) around the boolean test, your expression is testing whether a random number + the variable value will equal the string 'home'. This of course will always be false no matter what you change the variable to, so, your key expression is never changing and therefore never resetting the swiper.
Itās not 100% clear from the structure of the overall widget what your intention is, but the home button is always visible. This means that a user could press it more than once expecting it to work each time. As I mentioned above, however, if the value of the variable doesnāt change you wonāt get a reset of based on the key property.
Hereās an example: The first widget just uses the static value home when the home button is pressed. The first time that is pressed, the swiper does reset because the variable value is going from undefined ā 'home'. If you swipe the cards and then press home again. nothing happens (variable goes from 'home' ā 'home'). But, now if you press the not home button, the swipper resets to home (variable goes from 'home' ā 'nothome').