切换按钮

QBtnToggle 组件也是一个基础的交互组件,有点相似于 QRadio,但是它是基于按钮的。您可以使用这个组件来充当一个开关或者用于真假值的收集。

QBtnToggle API

QBtnToggle API


name
: String
说明
用于指定控件的名称。理直表单提交时非常有用

用法

基础



<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>

外形设计

TIP

因为 QBtnToggle 是 QBtn 的封装,所以您可以将 QBtn 中与样式设计相关的属性用在 QBtnToggle 中 。



<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>



<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>



<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>

自定义内容

下面的第一个 QBtnToggle 在每个按钮上都有鼠标悬浮提示。第二个 QBtnToggle 定制了内容。请注意 options 对象中的 slot 属性。当您使用这个插槽属性时,您不需要在 options 中使用 label/icon 属性。



<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>

禁用和只读



<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>

原生表单提交

当处理一个带有 actionmethod 的原生表单时(如:使用 Quasar 和 ASP.NET 控制器时),您需要为 QBtnToggle 声明 name 属性,否则表单数据中不会包含它:



<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>