TrainingPlanSelect.vue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <template>
  2. <el-select
  3. v-model="selected"
  4. :placeholder="placeholder"
  5. filterable
  6. :clearable="clearable"
  7. :disabled="disabled"
  8. @change="select"
  9. >
  10. <el-option
  11. v-for="item in optionList"
  12. :key="item.id"
  13. :value="item.id"
  14. :label="item.name"
  15. >
  16. </el-option>
  17. </el-select>
  18. </template>
  19. <script>
  20. import { conditionTrainingPlanList } from "../../modules/base/api";
  21. export default {
  22. name: "training-plan-select",
  23. props: {
  24. disabled: { type: Boolean, default: false },
  25. placeholder: { type: String, default: "请选择" },
  26. value: { type: [Number, String], default: "" },
  27. clearable: { type: Boolean, default: true },
  28. professionalId: { type: String },
  29. professionalRequired: { type: Boolean, default: false },
  30. defaultSelect: { type: Boolean, default: false },
  31. },
  32. data() {
  33. return {
  34. optionList: [],
  35. selected: "",
  36. };
  37. },
  38. watch: {
  39. value: {
  40. immediate: true,
  41. handler(val) {
  42. this.selected = val;
  43. },
  44. },
  45. professionalId(val, oldval) {
  46. if (val !== oldval && this.professionalRequired) {
  47. this.search();
  48. this.$emit("input", "");
  49. this.$emit("change", {});
  50. }
  51. },
  52. },
  53. created() {
  54. this.search();
  55. },
  56. methods: {
  57. async search() {
  58. this.optionList = [];
  59. if (this.professionalRequired && !this.professionalId) return;
  60. const res = await conditionTrainingPlanList(this.professionalId);
  61. this.optionList = res;
  62. if (this.defaultSelect && !this.value) this.selectDefault();
  63. },
  64. select() {
  65. this.$emit("input", this.selected);
  66. this.$emit(
  67. "change",
  68. this.optionList.find((item) => item.id === this.selected)
  69. );
  70. },
  71. selectDefault() {
  72. const defaultData = this.optionList[0];
  73. if (defaultData) {
  74. this.selected = defaultData.id;
  75. this.$emit("input", this.selected);
  76. this.$emit("change", defaultData);
  77. this.$emit("default-selected", defaultData);
  78. }
  79. },
  80. },
  81. };
  82. </script>