Vue 3.0 + Element Plus 下拉框表格組件

組件

<template>
  <div>
    <el-select
      style="width: 240px"
      id="select"
      ref="selectTable"
      v-model="selectShowValue"
      :placeholder="props.placeholder"
      :size="props.size"
      :clearable="props.clearable"
      :filterable="editable"
      @input="handleSelectInput"
    >
      <template #empty>
        <el-table
          height="250"
          ref="table"
          :data="props.tableData"
          :highlight-current-row="props.isHighlight"
          :size="props.size"
          :border="props.border"
          @current-change="handleCurrentChange"
          style="width: 100%;"
        >
          <el-table-column
            v-for="field in props.fields"
            :prop="field.prop"
            :label="field.label"
            :width="field.width"
            :show-overflow-tooltip="field.showTooltip"
          />
        </el-table>
      </template>
    </el-select>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch } from "vue";

interface Field {
  prop: string;
  label: string;
  width: number;
  showTooltip?: boolean;
}
interface Props {
  data: any;
  fields: Field[];
  tableData: object[];
  isHighlight?: boolean;
  border?: boolean;
  label?: string;
  value?: string;
  objKey?: string;
  placeholder?: string;
  clearable?: boolean;
  editable?: boolean;
  size?: string;
}
const props = withDefaults(defineProps<Props>(), {
  placeholder: "請選擇",
  size: "default",
  isHighlight: true,
  value: undefined,
  label: undefined,
  border: false,
  clearable: false,
  editable: false,
});

interface Emits {
  (e: "update:data", val: any): void;
  (e: "inputChange", val: string): void;
}
const emits = defineEmits<Emits>();
const selectTable = ref();
const table = ref();
const selectShowValue = ref(null);
const rowObj = ref(null);
const data = computed({
  get() {
    return props.data;
  },
  set(val) {
    emits("update:data", val);
  },
});
watch(data, (newVal) => {
  if (newVal) {
    if (typeof props.label !== "undefined") {
      if (typeof props.value !== "undefined") {
        let row = props.tableData.find(
          (item) => item[props.value] === props.data
        );
        selectShowValue.value =
          typeof row === "undefined" ? props.data : row[props.label];
      } else if (typeof props.objKey !== "undefined") {
        let row = props.tableData.find(
          (item) => item[props.objKey] === props.data[props.objKey]
        );
        selectShowValue.value =
          typeof row === "undefined" ? props.data : row[props.label];
      }
    } else {
      if (typeof props.value !== "undefined") {
        let row = props.tableData.find(
          (item) => item[props.value] === props.data
        );
        selectShowValue.value =
          typeof row === "undefined" ? props.data : row[props.value];
      } else if (typeof props.objKey !== "undefined") {
        let row = props.tableData.find(
          (item) => item[props.objKey] === props.data
        );
        selectShowValue.value =
          typeof row === "undefined" ? props.data : row[props.objKey];
      }
    }
  }
});
watch(selectShowValue, (newVal) => {
  if (newVal === "") {
    emits("update:data", newVal);
    table.value.setCurrentRow(null);
  }
});
const handleCurrentChange = (val: any) => {
  if (!val) {
    return;
  }
  rowObj.value = { ...val };
  setLabel(val);
  if (typeof props.value !== "undefined") {
    emits("update:data", val[props.value]);
  } else {
    emits("update:data", val);
  }
  selectTable.value.blur();
};
const handleSelectInput = () => {
  emits("inputChange", document.getElementById("select").value);
};
const setLabel = (val: any) => {
  if (typeof props.label === "undefined") {
    if (typeof props.value !== "undefined") {
      selectShowValue.value = val[props.value];
    } else if (typeof props.objKey !== "undefined") {
      selectShowValue.value = val[props.objKey];
    }
  } else {
    selectShowValue.value = val[props.label];
  }
};
</script>
<style scoped>
.paging {
  width: 100%;
  display: flex;
  justify-content: center;
}
</style>

引用

<template>
  <div>
    <SelectTable
      v-model:data="data"
      :fields="fields"
      :tableData="tableData"
      :label="label"
      :objKey="objKey"
      :border="true"
      clearable
      editable
      @inputChange="inputChange"
    ></SelectTable>
  </div>
</template>

<script setup lang="ts">
import SelectTable from "./components/SelectTable.vue";
import { ref } from "vue";
const data = ref();
const objKey = ref("itemid");
const label = ref("itemid");
const fields = [
  {
    prop: "itemid",
    label: "ID",
    width: 180,
  },
  {
    prop: "product",
    label: "產(chǎn)品",
    width: 180,
  },
  {
    prop: "price",
    label: "價格",
    width: 200,
  },
];
const tableData = ref([
{
    "itemid": "EST-11",
    "product": "Iguana",
    "price": 18.5
  },
  {
    "itemid": "EST-12",
    "product": "Rattlesnake",
    "price": 28.5
  },
  {
    "itemid": "EST-13",
    "product": "Manx",
    "price": 38.5
  },
  {
    "itemid": "EST-15",
    "product": "Rattlesnake",
    "price": 48.5
  },
  {
    "itemid": "EST-16",
    "product": "Amazon Parrot",
    "price": 58.5
  },
  {
    "itemid": "EST-17",
    "product": "Koi",
    "price": 68.5
  },
  {
    "itemid": "EST-19",
    "product": "Dalmation",
    "price": 78.5
  }
]);
const inputChange = (val: string) => {
  console.log(val);
};
</script>

<style scoped></style>

效果

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末镐牺,一起剝皮案震驚了整個濱河市惋戏,隨后出現(xiàn)的幾起案子甸各,更是在濱河造成了極大的恐慌痴腌,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件此疹,死亡現(xiàn)場離奇詭異甚颂,居然都是意外死亡蜜猾,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進(jìn)店門振诬,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人衍菱,你說我怎么就攤上這事赶么。” “怎么了脊串?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵胁镐,是天一觀的道長套像。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么谴仙? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮逆甜,結(jié)果婚禮上教藻,老公的妹妹穿的比我還像新娘。我一直安慰自己谜叹,他們只是感情好匾寝,可當(dāng)我...
    茶點故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著荷腊,像睡著了一般艳悔。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上女仰,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天猜年,我揣著相機與錄音,去河邊找鬼疾忍。 笑死乔外,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的锭碳。 我是一名探鬼主播袁稽,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼擒抛!你這毒婦竟也來了推汽?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤歧沪,失蹤者是張志新(化名)和其女友劉穎歹撒,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體诊胞,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡暖夭,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年锹杈,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片迈着。...
    茶點故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡竭望,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出裕菠,到底是詐尸還是另有隱情咬清,我是刑警寧澤,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布奴潘,位于F島的核電站旧烧,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏画髓。R本人自食惡果不足惜掘剪,卻給世界環(huán)境...
    茶點故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望奈虾。 院中可真熱鬧夺谁,春花似錦、人聲如沸愚墓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽浪册。三九已至扫腺,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間村象,已是汗流浹背笆环。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留厚者,地道東北人躁劣。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像库菲,于是被迫代替她去往敵國和親账忘。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,843評論 2 354

推薦閱讀更多精彩內(nèi)容