bySelect.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. get itemLabel(){
  42. let n = 'label';
  43. if(this.attrs.label){
  44. n = this.attrs.label
  45. }
  46. return n;
  47. }
  48. get itemValue(){
  49. let n = 'value';
  50. if(this.attrs.value){
  51. n = this.attrs.value
  52. }
  53. return n;
  54. }
  55. created(){
  56. if(this.propConfig){
  57. this.setConfig(this.propConfig)
  58. }
  59. if(this.propValue){
  60. this.setValue(this.propConfig)
  61. }
  62. }
  63. mounted(){
  64. this.$nextTick(()=>{
  65. if(this.requestConfig){
  66. this.request();
  67. }else if(this.attrs.data){
  68. this.setOptions(this.attrs.data)
  69. }
  70. })
  71. }
  72. setValue(data:any){
  73. if(data){
  74. this.value = (this as any).$lodash.cloneDeep(data);
  75. }else{
  76. this.value = '';
  77. }
  78. }
  79. getValue(){
  80. return (this as any).$lodash.cloneDeep(this.value);
  81. }
  82. setOptions(data:Array<any>){
  83. this.options = data;
  84. }
  85. onChange(){
  86. this.$emit('onChange',this.value);
  87. }
  88. // 清空数据
  89. clearValue(){
  90. this.value = ''
  91. }
  92. //请求url
  93. request(){
  94. if(!this.requestConfig || !this.requestConfig.url){
  95. return
  96. }
  97. let parame = (this as any).$lodash.cloneDeep(this.requestConfig);
  98. parame.success = (res:any) => {
  99. let f = this.attrs.requestFormat?this.attrs.requestFormat:'data'
  100. if(res.data && res.data[f]){
  101. this.setOptions(res.data[f]);
  102. }
  103. }
  104. parame.fail = (err:any) => {}
  105. this.requestHandle(parame);
  106. }
  107. }
  108. </script>
  109. <style lang="scss" scoped>
  110. </style>