切换按钮
QBtnToggle 组件也是一个基础的交互组件,有点相似于 QRadio,但是它是基于按钮的。您可以使用这个组件来充当一个开关或者用于真假值的收集。
QBtnToggle API
QBtnToggle API
props
25
slots
2
events
2
behavior
1
content
5
model
3
state
2
style
14
name
: String
说明
用于指定控件的名称。理直表单提交时非常有用
用法
基础
基础
Template
Script
All
<template>
<div class="q-pa-md">
<q-btn-toggle
v-model="model"
toggle-color="primary"
:options="[
{label: 'One', value: 'one'},
{label: 'Two', value: 'two'},
{label: 'Three', value: 'three'}
]"
/>
</div>
</template>
Copied to clipboard
外形设计
TIP
因为 QBtnToggle 是 QBtn 的封装,所以您可以将 QBtn 中与样式设计相关的属性用在 QBtnToggle 中 。
一些外观设计示例
Template
Script
Style
All
<template>
<div class="q-pa-md q-gutter-md">
<div>
<q-btn-toggle
v-model="model"
push
glossy
toggle-color="primary"
:options="[
{label: 'One', value: 'one'},
{label: 'Two', value: 'two'},
{label: 'Three', value: 'three'}
]"
/>
</div>
<div>
<q-btn-toggle
v-model="model"
toggle-color="primary"
flat
:options="[
{label: 'One', value: 'one'},
{label: 'Two', value: 'two'},
{label: 'Three', value: 'three'}
]"
/>
</div>
<div>
<q-btn-toggle
v-model="model"
color="brown"
text-color="white"
toggle-color="orange"
toggle-text-color="black"
rounded
unelevated
glossy
:options="[
{label: 'One', value: 'one'},
{label: 'Two', value: 'two'},
{label: 'Three', value: 'three'},
{label: 'Four', value: 'four'}
]"
/>
</div>
<div>
<q-btn-toggle
v-model="model"
class="my-custom-toggle"
no-caps
rounded
unelevated
toggle-color="primary"
color="white"
text-color="primary"
:options="[
{label: 'Option 1', value: 'one'},
{label: 'Option 2', value: 'two'}
]"
/>
</div>
</div>
</template>
Copied to clipboard
水平铺开
Template
Script
Style
All
<template>
<div class="q-pa-md">
<div class="q-gutter-y-md">
<q-btn-toggle
v-model="model"
spread
no-caps
toggle-color="purple"
color="white"
text-color="black"
:options="[
{label: 'Option 1', value: 'one'},
{label: 'Option 2', value: 'two'}
]"
/>
<q-btn-toggle
v-model="secondModel"
spread
class="my-custom-toggle"
no-caps
rounded
unelevated
toggle-color="primary"
color="white"
text-color="primary"
:options="[
{label: 'Option 1', value: 'one'},
{label: 'Option 2', value: 'two'}
]"
/>
</div>
</div>
</template>
Copied to clipboard
黑色背景
Template
Script
All
<template>
<div class="q-pa-md bg-grey-10 text-white">
<q-btn-toggle
v-model="model"
push
glossy
toggle-color="primary"
:options="[
{label: 'Cake', value: 'cake'},
{label: 'Ice cream', value: 'ice-cream'}
]"
/>
</div>
</template>
Copied to clipboard
自定义内容
下面的第一个 QBtnToggle 在每个按钮上都有鼠标悬浮提示。第二个 QBtnToggle 定制了内容。请注意 options
对象中的 slot
属性。当您使用这个插槽属性时,您不需要在 options
中使用 label
/icon
属性。
自定义按钮内容
Template
Script
All
<template>
<div class="q-pa-md">
<div class="q-gutter-md">
<q-btn-toggle
v-model="model"
push
glossy
toggle-color="teal"
:options="[
{label: 'One', value: 'one', slot: 'one'},
{label: 'Two', value: 'two', slot: 'two'},
{label: 'Three', value: 'three', slot: 'three'}
]"
>
<template v-slot:one>
<q-tooltip>One!</q-tooltip>
</template>
<template v-slot:two>
<q-tooltip>Two!</q-tooltip>
</template>
<template v-slot:three>
<q-tooltip>Three!</q-tooltip>
</template>
</q-btn-toggle>
<q-btn-toggle
v-model="model"
push
rounded
glossy
toggle-color="purple"
:options="[
{value: 'one', slot: 'one'},
{value: 'two', slot: 'two'},
{value: 'three', slot: 'three'}
]"
>
<template v-slot:one>
<div class="row items-center no-wrap">
<div class="text-center">
Pick<br>boat
</div>
<q-icon right name="directions_boat" />
</div>
</template>
<template v-slot:two>
<div class="row items-center no-wrap">
<div class="text-center">
Pick<br>car
</div>
<q-icon right name="directions_car" />
</div>
</template>
<template v-slot:three>
<div class="row items-center no-wrap">
<div class="text-center">
Pick<br>railway
</div>
<q-icon right name="directions_railway" />
</div>
</template>
</q-btn-toggle>
</div>
</div>
</template>
Copied to clipboard
禁用和只读
禁用和只读
Template
Script
All
<template>
<div class="q-pa-md">
<q-btn-toggle
disable
v-model="model"
toggle-color="primary"
push
glossy
:options="[
{label: 'One', value: 'one'},
{label: 'Two', value: 'two'},
{label: 'Three', value: 'three'}
]"
/>
<q-btn-toggle
readonly
v-model="model"
toggle-color="primary"
push
glossy
class="q-ml-md"
:options="[
{label: 'One', value: 'one'},
{label: 'Two', value: 'two'},
{label: 'Three', value: 'three'}
]"
/>
</div>
</template>
Copied to clipboard
原生表单提交
当处理一个带有 action
和 method
的原生表单时(如:使用 Quasar 和 ASP.NET 控制器时),您需要为 QBtnToggle 声明 name
属性,否则表单数据中不会包含它:
Native form
Template
Script
All
<template>
<div class="q-pa-md">
<q-form @submit="onSubmit" class="q-gutter-md">
<q-btn-toggle
name="genre"
v-model="genre"
push
glossy
toggle-color="teal"
:options="[
{label: 'Rock', value: 'rock'},
{label: 'Funk', value: 'funk'},
{label: 'Pop', value: 'pop'}
]"
/>
<div>
<q-btn label="Submit" type="submit" color="primary"/>
</div>
</q-form>
<q-card v-if="submitEmpty" flat bordered class="q-mt-md bg-grey-2">
<q-card-section>
Submitted form contains empty formData.
</q-card-section>
</q-card>
<q-card v-else-if="submitResult.length > 0" flat bordered class="q-mt-md bg-grey-2">
<q-card-section>Submitted form contains the following formData (key = value):</q-card-section>
<q-separator />
<q-card-section class="row q-gutter-sm items-center">
<div
v-for="(item, index) in submitResult"
:key="index"
class="q-px-sm q-py-xs bg-grey-8 text-white rounded-borders text-center text-no-wrap"
>{{ item.name }} = {{ item.value }}</div>
</q-card-section>
</q-card>
</div>
</template>
Copied to clipboard
版权声明 本中文文档内容版权为站长个人所有,保留所有权利。
免责声明 本站点所包含的任何跳转链接均为第三方广告内容,站点对这些广告内容的准确性、合法性、真实性不承担任何责任。用户点击跳转链接进入第三方网站后,应自行判断广告内容的真实性和合法性,并自行承担因使用广告内容而产生的风险和责任。任何因使用第三方广告内容而导致的损失或损害,本站点概不负责。