Vue UI Components Documentation
The Vue SwitchButton toggles between an on and an off state with a sliding switch. This guide adds the Vue SwitchButton (jqxSwitchButton) to an app, then shows how to set its options, handle its events and call its methods.
jqwidgets-scriptsjqwidgets-scripts/jqwidgets-vue3/vue_jqxswitchbutton.vue In a Vue 3 project (to start one, see Getting started with Vue), add the package:
npm install jqwidgets-scripts
import JqxSwitchButton from 'jqwidgets-scripts/jqwidgets-vue3/vue_jqxswitchbutton.vue';
import 'jqwidgets-scripts/jqwidgets/styles/jqx.base.css';
import 'jqwidgets-scripts/jqwidgets/styles/jqx.tailwind-blue-dark.css';
With <script setup>, an imported component is ready to use in the template. jqx.base.css is required; the second file is a theme, selected with the theme attribute.
The SwitchButton demo as a Single-File Component:
<template>
<div>
<div id="settings-panel">
<div class="sections-top-shadow"></div>
<div class="settings-section-top settings-section">
<div class="settings-label">Ringtone</div>
<div class="settings-setter">
<div class="settings-melody">Marimba</div>
</div>
</div>
<div class="settings-section">
<div class="settings-label">New Mail</div>
<div class="settings-setter">
<JqxSwitchButton
@checked="onChecked(1)"
@unchecked="onUnchecked(1)"
:width="81"
:height="27"
:checked="true"
>
</JqxSwitchButton>
</div>
</div>
<div class="settings-section">
<div class="settings-label">Sent Mail</div>
<div class="settings-setter">
<JqxSwitchButton
@checked="onChecked(2)"
@unchecked="onUnchecked(2)"
:width="81"
:height="27"
:checked="true"
>
</JqxSwitchButton>
</div>
</div>
<div class="settings-section">
<div class="settings-label">Calendar Alerts</div>
<div class="settings-setter">
<JqxSwitchButton
@checked="onChecked(3)"
@unchecked="onUnchecked(3)"
:width="81"
:height="27"
:checked="true"
>
</JqxSwitchButton>
</div>
</div>
<div class="settings-section">
<div class="settings-label">Lock Sounds</div>
<div class="settings-setter">
<JqxSwitchButton
@checked="onChecked(4)"
@unchecked="onUnchecked(4)"
:width="81"
:height="27"
:checked="true"
>
</JqxSwitchButton>
</div>
</div>
<div class="sections-section-bottom settings-section">
<div class="settings-label">Keyboard Clicks</div>
<div class="settings-setter">
<JqxSwitchButton
@checked="onChecked(5)"
@unchecked="onUnchecked(5)"
:width="81"
:height="27"
:checked="false"
>
</JqxSwitchButton>
</div>
</div>
</div>
<div class="events-container">
<div>Events:</div>
<div ref="events"></div>
</div>
</div>
</template>
<script setup>
import { ref } from 'vue';
import JqxSwitchButton from 'jqwidgets-scripts/jqwidgets-vue3/vue_jqxswitchbutton.vue';
const events = ref(null);
const label = ref({
1: 'New Mail',
2: 'Sent Mail',
3: 'Calendar Alerts',
4: 'Lock Sounds',
5: 'Keyboard clicks'
});
function onChecked(buttonNumber) {
events.value.innerHTML = label.value[buttonNumber] + ': Unchecked';
}
function onUnchecked(buttonNumber) {
events.value.innerHTML = label.value[buttonNumber] + ': Checked';
}
</script>
<style>
#settings-panel {
background-color: #fff;
padding: 20px;
display: inline-block;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
position: relative;
}
.settings-section {
background-color: #f7f7f7;
height: 45px;
width: 500px;
border: 1px solid #b4b7bc;
border-bottom-width: 0px;
}
.settings-section-top {
border-bottom-width: 0px;
-moz-border-radius-topleft: 10px;
-webkit-border-top-left-radius: 10px;
border-top-left-radius: 10px;
-moz-border-radius-topright: 10px;
-webkit-border-top-right-radius: 10px;
border-top-right-radius: 10px;
}
.sections-section-bottom {
border-bottom-width: 1px;
-moz-border-radius-bottomleft: 10px;
-webkit-border-bottom-left-radius: 10px;
border-bottom-left-radius: 10px;
-moz-border-radius-bottomright: 10px;
-webkit-border-bottom-right-radius: 10px;
border-bottom-right-radius: 10px;
}
.sections-top-shadow {
width: 500px;
height: 1px;
position: absolute;
top: 21px;
left: 21px;
height: 30px;
border-top: 1px solid #e4e4e4;
-moz-border-radius-topleft: 10px;
-webkit-border-top-left-radius: 10px;
border-top-left-radius: 10px;
-moz-border-radius-topright: 10px;
-webkit-border-top-right-radius: 10px;
border-top-right-radius: 10px;
}
.settings-label {
font-weight: bold;
font-family: Sans-Serif;
font-size: 14px;
margin-left: 14px;
margin-top: 15px;
float: left;
}
.settings-melody {
color: #385487;
font-family: Sans-Serif;
font-size: 14px;
display: inline-block;
margin-top: 7px;
}
.settings-setter {
float: right;
margin-right: 14px;
margin-top: 8px;
}
.events-container {
margin-left: 20px;
}
#theme {
margin-left: 20px;
margin-bottom: 20px;
}
</style>Options are attributes. Bind values from the script with :, for example :width="850".
Listen to an event with @ and its name. For example, change: This event is triggered when the switch button's state changes from one state to another.
<script setup>
function onChange(event) {
console.log(event.args);
}
</script>
<template>
<JqxSwitchButton @change="onChange" />
</template>
Call methods through a template ref. For example, val: Sets or gets the value.
<script setup>
import { ref, onMounted } from 'vue';
const mySwitchButton = ref(null);
onMounted(() => {
const value = mySwitchButton.value.val();
console.log(value);
});
</script>
<template>
<JqxSwitchButton ref="mySwitchButton" />
</template>
Every component also has setOptions({ … }) and getOptions('name').