vue2.0和quasarUI封装多功能下拉框可以点击显示表格,也可以搜索过滤下拉框的数据

子组件完整代码:

<template>
  <div style="position: relative">
    <div @click="clickTable" ref="Getclick">
      <q-btn outline color="white" text-color="black" class="collect_icon"
        ><i class="material-icons Down_icon" style="font-size:20px">{{
          showOptions ? "arrow_drop_up" : "arrow_drop_down"
        }}</i></q-btn
      >
      <div class="my-table" v-if="showOptions" style="position: absolute; top: 32px">
        <q-table
          :data="data"
          :columns="columns"
          row-key="name"
          @row-click.stop="handleRowClick"
        />
      </div>
    </div>
    <div class="q-pa-md" style="position: absolute; top: 0">
      <div class="row">
        <!-- clearable -->
        <q-select
          outlined
          :disable="Whether"
          :value="model"
          use-input
          hide-selected
          fill-input
          input-debounce="0"
          :options="options"
          @filter="filterFn"
          @input-value="setModel"
          @focus="showOptions = false"
        >
          <template v-slot:no-option>
            <q-item>
              <q-item-section class="text-grey"> 暂无数据 </q-item-section>
            </q-item>
          </template>
          <template v-slot:append>
            <q-icon
              v-if="model !== null"
              style="margin-top: 5px; float: right"
              class="cursor-pointer"
              name="clear"
              @click.stop="model = null"
            />
          </template>
        </q-select>
      </div>
    </div>
  </div>
</template>
<script>
const stringOptions = ["Google", "Facebook"].reduce((acc, opt) => {
  for (let i = 1; i <= 5; i++) {
    acc.push(opt + " " + i);
  }
  return acc;
}, []);
export default {
  props: {
    value: {
      type: String,
      default: null,
    },
    columns: {
      type: Array,
      required: true,
    },
    Whether: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  data() {
    return {
      showOptions: false,
     
      data: [
        {
          name: "Gingerbread",
          calories: 356,
          fat: 16.0,
          carbs: 49,
          protein: 3.9,
          sodium: 327,
          calcium: "7%",
          iron: "16%",
        },

        {
          name: "Lollipop",
          calories: 392,
          fat: 0.2,
          carbs: 98,
          protein: 0,
          sodium: 38,
          calcium: "0%",
          iron: "2%",
        },
      ],
      Showor: false,
      model: null,
      options: stringOptions,
    };
  },
  watch: {
    value(newValue) {
      this.model = newValue;
    },
    model(newModel) {
      this.$emit("input", newModel);
    },
  },
  mounted() {
    document.addEventListener(
      "click",
      (e) => {
        let variable = this.$refs.Getclick;
        if (!variable?.contains(e.target)) {
          this.showOptions = false;
        }
      },
      true
    );
  },
  methods: {
    clickTable() {
      this.showOptions = !this.showOptions;
    },
    handleRowClick(row, column, event) {
      console.log(column, ">>");
      this.model = column.name;
      this.showOptions = false
    },
    filterFn(val, update, abort) {
      update(() => {
        const needle = val.toLocaleLowerCase();
        this.options = stringOptions.filter(
          (v) => v.toLocaleLowerCase().indexOf(needle) > -1
        );
      });
    },
    setModel(val) {
      this.model = val;
    },
  },
};
</script>
<style lang="scss" scoped>
.collect_icon {
  position: absolute;
  z-index: 99;
  top: 0;
  height: 32px;
  width: 30px;
}
::v-deep .q-field--outlined .q-field__control {
  padding: 0 0 0 35px;
  width: 150px;
}
::v-deep .q-select__dropdown-icon {
  display: none;
}
::v-deep .q-btn__wrapper {
  padding: 4px;
}
::v-deep .q-field__marginal {
  height: 32px;
}
::v-deep .q-field--auto-height .q-field__native {
  min-height: 16px;
  height: 32px;
}
::v-deep .q-field--auto-height .q-field__control,
.q-field--auto-height .q-field__native {
  min-height: 16px;
  height: 32px;
}
.q-btn {
  padding: 0;
}
.my-table {
  position: absolute;
  z-index: 999;
  width: 400px;
}
.my-table th {
  background-color: #fafafa;
}
.my-table td,
.my-table th {
  color: #000;
  text-align: center;
  border: 1px solid #e8e8e8;
}
::v-deep .q-field__append {
  padding-left: 0;
}
::v-deep .q-pa-md {
  padding: 0;
}
</style>

效果图:

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里是一个 Vue3 下框组件的 SCSS 和 setup 封装示例: ```vue <template> <div class="select-wrapper" :class="{ open: isOpen }"> <div class="select-header" @click="toggleOpen"> <span>{{ selected }}</span> <i class="arrow" :class="{ 'arrow-up': isOpen, 'arrow-down': !isOpen }"></i> </div> <div class="select-options"> <div class="option" v-for="(option, index) in options" :key="index" @click="selectOption(option)"> {{ option }} </div> </div> </div> </template> <script> import { ref } from 'vue'; export default { props: { options: { type: Array, required: true, }, }, setup(props) { const isOpen = ref(false); const selected = ref(props.options[0]); const toggleOpen = () => { isOpen.value = !isOpen.value; }; const selectOption = (option) => { selected.value = option; isOpen.value = false; }; return { isOpen, selected, toggleOpen, selectOption, }; }, }; </script> <style lang="scss" scoped> .select-wrapper { position: relative; width: 200px; .select-header { display: flex; align-items: center; justify-content: space-between; padding: 10px; border: 1px solid #ccc; cursor: pointer; span { flex: 1; margin-right: 10px; } .arrow { width: 0; height: 0; border-style: solid; border-width: 5px 4px 0 4px; } .arrow-down { border-color: #999 transparent transparent transparent; } .arrow-up { border-color: transparent transparent #999 transparent; } } .select-options { position: absolute; top: 100%; left: 0; width: 100%; z-index: 999; background-color: #fff; border: 1px solid #ccc; border-top: none; max-height: 200px; overflow-y: auto; .option { padding: 10px; cursor: pointer; &:hover { background-color: #f2f2f2; } } } &.open { .select-header { border-bottom: none; .arrow { transform: rotate(180deg); } } .select-options { border-top: 1px solid #ccc; } } } </style> ``` 这个下框组件的 SCSS 样式使用了嵌套规则和变量,使得样式更加清晰和易于维护。在组件内部,使用 `isOpen` 和 `selected` 两个响应式变量来控制下框的状态和选中的选项。当用户点框头部时,调用 `toggleOpen` 方法来切换下框的状态;当用户点框中的某个选项时,调用 `selectOption` 方法来更新选中的选项,并关闭下框。 你可以在父组件中使用这个下框组件,如下所示: ```vue <template> <div> <Select :options="options" /> </div> </template> <script> import Select from './Select.vue'; export default { components: { Select, }, data() { return { options: ['Option 1', 'Option 2', 'Option 3'], }; }, }; </script> <style lang="scss"> // 全局样式 $primary-color: #409eff; button { color: #fff; background-color: $primary-color; border: none; padding: 10px; border-radius: 4px; cursor: pointer; &:hover { background-color: darken($primary-color, 10%); } } </style> ``` 这个父组件将一个包含三个选项的数组传递给下框组件,然后将下框组件渲染到页面中。你可以根据需要调整下框的样式和功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值