bySelect.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. requestFormat:''
  31. },
  32. request:{}
  33. }
  34. */
  35. import { Component, Prop, Vue, Watch } from "vue-property-decorator";
  36. import VueViews from '@/benyun/compVue/VueViews'
  37. @Component
  38. export default class BySelect extends VueViews {
  39. value:any='';
  40. options:Array<any>=[]
  41. @Watch('propValue')
  42. propValueChange(v:any){
  43. this.setValue(v);
  44. }
  45. get itemLabel(){
  46. let n = 'label';
  47. if(this.attrs.label){
  48. n = this.attrs.label
  49. }
  50. return n;
  51. }
  52. get itemValue(){
  53. let n = 'value';
  54. if(this.attrs.value){
  55. n = this.attrs.value
  56. }
  57. return n;
  58. }
  59. created(){
  60. if(this.propConfig){
  61. this.setConfig(this.propConfig)
  62. }
  63. if(this.propValue){
  64. this.setValue(this.propValue)
  65. }
  66. }
  67. mounted(){
  68. this.$nextTick(()=>{
  69. if(this.requestConfig){
  70. this.request();
  71. }else if(this.attrs.data){
  72. this.setOptions(this.attrs.data)
  73. }
  74. })
  75. }
  76. setValue(data:any){
  77. if(data){
  78. this.value = (this as any).$lodash.cloneDeep(data);
  79. }else{
  80. this.value = '';
  81. }
  82. }
  83. getValue(){
  84. return (this as any).$lodash.cloneDeep(this.value);
  85. }
  86. setOptions(data:Array<any>){
  87. this.options = data;
  88. if(!this.value && this.attrs.defaultIndex >= 0 && this.options[this.attrs.defaultIndex]){
  89. this.value = this.options[this.attrs.defaultIndex][this.itemValue];
  90. this.onChange();
  91. }
  92. }
  93. onChange(){
  94. this.$emit('onChange',this.value);
  95. }
  96. // 清空数据
  97. clearValue(){
  98. this.value = ''
  99. }
  100. //请求url
  101. request(){
  102. if(!this.requestConfig || !this.requestConfig.url){
  103. return
  104. }
  105. let parame = (this as any).$lodash.cloneDeep(this.requestConfig);
  106. parame.success = (res:any) => {
  107. let f = this.attrs.requestFormat?this.attrs.requestFormat:'data'
  108. if(res.data && res.data[f]){
  109. this.setOptions(res.data[f]);
  110. }
  111. }
  112. parame.fail = (err:any) => {}
  113. this.requestHandle(parame);
  114. }
  115. }
  116. </script>
  117. <style lang="scss" scoped>
  118. </style>