xialaxuanz.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var i = 0
  2. export default function (option, placeholder = '请选择', errMsg = '请先选择数据') {
  3. return new Promise((r, j) => {
  4. if (option.length == 1) {
  5. r(option?.[0]?.value)
  6. return
  7. }
  8. const h = this.$createElement
  9. const refName = `selectView${++i}`
  10. var _this = this
  11. var done_ = null
  12. this.$msgbox({
  13. title: placeholder,
  14. message: h(
  15. 'el-select',
  16. {
  17. style: {
  18. width: '100%'
  19. },
  20. props: {
  21. value: '',
  22. filterable: true,
  23. clearable: true,
  24. size: 'mini'
  25. },
  26. ref: refName,
  27. on: {
  28. change: e => {
  29. _this.$refs[refName].value = e
  30. }
  31. }
  32. },
  33. [
  34. option.map(it => {
  35. return h('el-option', {
  36. props: {
  37. key: it.value,
  38. label: it.label,
  39. value: it.value
  40. }
  41. })
  42. })
  43. ]
  44. ),
  45. beforeClose: function (action, instance, done) {
  46. if (action === 'confirm') {
  47. if (_this?.$refs?.[refName]?.value) {
  48. done()
  49. } else {
  50. done_ = done
  51. this.$message({
  52. message: errMsg,
  53. type: 'warning'
  54. })
  55. }
  56. } else {
  57. done()
  58. }
  59. },
  60. showCancelButton: true,
  61. closeOnClickModal: false,
  62. confirmButtonText: '确定',
  63. cancelButtonText: '取消'
  64. })
  65. .then(_ => {
  66. if (_this?.$refs?.[refName]?.value) {
  67. r(_this?.$refs?.[refName]?.value)
  68. done_?.()
  69. } else {
  70. j()
  71. }
  72. })
  73. .catch(_ => {
  74. j()
  75. })
  76. })
  77. }