index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import Select from 'ant-design-vue/es/select'
  2. import _ from 'lodash'
  3. import { getLookUpData, listLookUp } from '@/api/data'
  4. /**
  5. * loopUp 组件
  6. * params: code loopUp的code 值
  7. * event: change(选中的值, 选中的对象 )
  8. * ref: getOptionDatas 获取所有查询的
  9. */
  10. export default {
  11. name: 'VSelect',
  12. data () {
  13. return {
  14. dataList: [],
  15. placeholderText: '',
  16. origDataList: []
  17. }
  18. },
  19. props: Object.assign({}, Select.props, {
  20. code: {
  21. type: String,
  22. required: true
  23. },
  24. showType: {
  25. type: String,
  26. default: 'select'
  27. },
  28. isEnable: { // 是否只获取已启用的数据
  29. type: Boolean,
  30. default: true
  31. },
  32. notIn: {
  33. type: Array,
  34. default: function () {
  35. return []
  36. }
  37. }
  38. }),
  39. created () {
  40. this.getDataList()
  41. },
  42. watch: {
  43. notIn (newVal, oldVal) {
  44. this.getDataList()
  45. },
  46. code (newVal, oldVal) {
  47. this.getDataList()
  48. }
  49. },
  50. methods: {
  51. /**
  52. * 获取当前所有的option 数据
  53. * @returns options data
  54. */
  55. getOptionDatas () {
  56. return _.cloneDeep(this.dataList)
  57. },
  58. getDataList () {
  59. const _this = this
  60. getLookUpData({
  61. pageNo: 1,
  62. pageSize: 1000,
  63. lookupCode: _this.code,
  64. isEnable: _this.isEnable ? 1 : undefined
  65. }).then(res => {
  66. if (res.status == 200) {
  67. _this.origDataList = res.data.list
  68. _this.dataList = res.data.list.filter(item => _this.notIn.indexOf(item.code) < 0)
  69. _this.$emit('loaded',_this.dataList)
  70. }
  71. })
  72. },
  73. // 根据code 获取名称
  74. getNameByCode (code) {
  75. const a = this.dataList.find(item => {
  76. return item.code == code
  77. })
  78. return a ? a.dispName : '-'
  79. },
  80. // 根据名称 获取code
  81. getCodeByName (dispName) {
  82. return this.datalist.find(item => item.dispName == dispName)
  83. }
  84. },
  85. render () {
  86. const props = {}
  87. Object.keys(Select.props).forEach(k => {
  88. this[k] && (props[k] = this[k])
  89. return props[k]
  90. })
  91. if (props.placeholder === undefined) {
  92. props.placeholder = this.placeholderText
  93. }
  94. const on = {
  95. change: obj => {
  96. this.$emit('input', obj)
  97. this.$emit('change', obj, _.find(this.dataList, ['code', obj]))
  98. }
  99. }
  100. const radioOn = {
  101. change: obj => {
  102. this.$emit('input', obj.target.value)
  103. this.$emit('change', obj.target.value, _.find(this.dataList, ['code', obj.target.value]))
  104. }
  105. }
  106. if (this.showType === 'radio') {
  107. return (
  108. <a-radio-group vModel={props.value} {...{ props, on: radioOn }} >
  109. {
  110. this.dataList.map(item => {
  111. return (<a-radio value={item.code} key={item.code}>{item['dispName']}</a-radio>)
  112. })
  113. }
  114. </a-radio-group>
  115. )
  116. }
  117. if (this.showType === 'tabs') {
  118. return (
  119. <a-tabs vModel={props.value} {...{ props, on: on }}>
  120. {
  121. this.dataList.map(item => {
  122. return (<a-tab-pane tab={item.dispName} key={item.code}></a-tab-pane>)
  123. })
  124. }
  125. </a-tabs>
  126. )
  127. }
  128. return (
  129. <Select vModel={props.value} {...{ props, on: on }}>
  130. {
  131. this.dataList.map(item => {
  132. return (<a-select-option value={item.code} key={item.code}>{item['dispName']}</a-select-option>)
  133. })
  134. }
  135. </Select>
  136. )
  137. }
  138. }