123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <template>
- <el-select
- v-model="selected"
- class="version-select"
- :placeholder="placeholder"
- filterable
- :clearable="clearable"
- :disabled="disabled"
- @change="select"
- >
- <el-option
- v-for="item in optionList"
- :key="item.id"
- :value="item.id"
- :label="item.name"
- :disabled="disabledOptions.includes(item.id)"
- >
- </el-option>
- </el-select>
- </template>
- <script>
- import { appVersionList } from "../modules/admin/api";
- export default {
- name: "version-select",
- props: {
- disabled: { type: Boolean, default: false },
- placeholder: { type: String, default: "请选择版本" },
- value: { type: [Number, String], default: "" },
- clearable: { type: Boolean, default: true },
- appId: { type: [String, Number], default: "" },
- disabledOptions: {
- type: Array,
- default() {
- return [];
- },
- },
- manualFetch: {
- type: Boolean,
- default: false,
- },
- selectDefault: {
- type: Boolean,
- default: false,
- },
- filterHandle: {
- type: Function,
- default: null,
- },
- },
- data() {
- return {
- optionList: [],
- selected: "",
- };
- },
- watch: {
- value: {
- immediate: true,
- handler(val) {
- this.selected = val;
- },
- },
- appId(val, oldval) {
- if (this.manualFetch) return;
- if (val !== oldval) {
- this.search();
- this.$emit("input", "");
- this.$emit("change", {});
- }
- },
- },
- created() {
- if (!this.manualFetch) this.search();
- },
- methods: {
- async search() {
- this.optionList = [];
- // if (!this.appId) return;
- const res = await appVersionList({
- appId: this.appId,
- pageNumber: 1,
- pageSize: 100,
- });
- this.optionList = res.records;
- if (this.filterHandle) {
- this.optionList = this.filterHandle(this.optionList);
- }
- if (!this.selectDefault) return;
- if (this.value) {
- const option = this.optionList.find((item) => item.id === this.value);
- if (option) {
- this.$emit("input", this.value);
- this.$emit("change", option);
- } else {
- this.selectFirst();
- }
- } else {
- this.selectFirst();
- }
- },
- selectFirst() {
- if (!this.optionList.length) {
- this.selected = "";
- this.$emit("input", this.selected);
- this.$emit("change", {});
- return;
- }
- this.selected = this.optionList[0].id;
- this.$emit("input", this.selected);
- this.$emit("change", this.optionList[0]);
- },
- select() {
- this.$emit("input", this.selected);
- this.$emit(
- "change",
- this.optionList.find((item) => item.id === this.selected)
- );
- },
- },
- };
- </script>
|