bySelect.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <el-select
  3. v-model="value"
  4. :placeholder="attrs.placeholder?attrs.placeholder:'请选择'"
  5. :disabled="attrs.disabled"
  6. :size="attrs.size"
  7. :clearable="attrs.clearable"
  8. @clear="onChange"
  9. @change="onChange"
  10. >
  11. <el-option
  12. v-for="item in options"
  13. :key="item[itemValue]"
  14. :label="item[itemLabel]"
  15. :value="item[itemValue]">
  16. </el-option>
  17. </el-select>
  18. </template>
  19. <script lang="ts">
  20. /*
  21. 配置说明
  22. config:{
  23. attrs:{
  24. itemLabel:'' //选项的标签
  25. itemValue:'' //选项的值
  26. disabled:true/false //是否禁用
  27. size:medium/small/mini //尺寸
  28. clearable:true/false //是否清空
  29. placeholder:'' //占位符
  30. },
  31. request:{}
  32. }
  33. */
  34. import { Component, Prop, Vue, Watch } from "vue-property-decorator";
  35. @Component
  36. export default class BySelect extends Vue {
  37. config:any={};
  38. value:any='';
  39. options:Array<any>=[]
  40. @Prop()
  41. propConfig: any
  42. @Prop()
  43. propValue:any
  44. get attrs(){
  45. return this.config?.attr ? this.config.attr : {};
  46. }
  47. get requestConfig(){
  48. return this.config?.request ? this.config.request : null;
  49. }
  50. get itemLabel(){
  51. let n = 'label';
  52. if(this.attrs.label){
  53. n = this.attrs.label
  54. }
  55. return n;
  56. }
  57. get itemValue(){
  58. let n = 'value';
  59. if(this.attrs.value){
  60. n = this.attrs.value
  61. }
  62. return n;
  63. }
  64. created(){
  65. if(this.propConfig){
  66. this.setConfig(this.propConfig)
  67. }
  68. if(this.propValue){
  69. this.setValue(this.propConfig)
  70. }
  71. }
  72. mounted(){
  73. this.$nextTick(()=>{
  74. if(this.requestConfig){
  75. this.request();
  76. }else if(this.attrs.data){
  77. this.setOptions(this.attrs.data)
  78. }
  79. })
  80. }
  81. setConfig(c:any){
  82. if(c){
  83. this.config = c;
  84. }
  85. }
  86. getConfig(){
  87. return (this as any).$lodash.cloneDeep(this.config)
  88. }
  89. setValue(data:any){
  90. if(data){
  91. this.value = (this as any).$lodash.cloneDeep(data);
  92. }else{
  93. this.value = {};
  94. }
  95. }
  96. getValue(){
  97. return (this as any).$lodash.cloneDeep(this.value);
  98. }
  99. setOptions(data:Array<any>){
  100. this.options = data;
  101. }
  102. onChange(){
  103. this.$emit('onChange',this.value);
  104. }
  105. // 清空数据
  106. clearValue(){
  107. this.value = ''
  108. }
  109. request(){
  110. if(!this.requestConfig || !this.requestConfig.url){
  111. return
  112. }
  113. (this as any).$request(this.requestConfig).then((res:any) => {
  114. if(res.data){
  115. this.setOptions(res.data);
  116. }
  117. })
  118. }
  119. }
  120. </script>
  121. <style lang="scss" scoped>
  122. </style>