index.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <template>
  2. <div class="tips-wrapper">
  3. <span>
  4. <el-tag
  5. v-for="item in value"
  6. :key="item"
  7. closable
  8. size="small"
  9. type="info"
  10. @close="() => tabClose(item)">
  11. {{ item }}
  12. </el-tag>
  13. </span>
  14. <span class="add-tag" @click="addTag">添加</span>
  15. <Modal title="添加标签" :visible="visible" :bg="false">
  16. <el-select
  17. ref="select"
  18. v-model="tagSelectValue"
  19. style="width: 100%"
  20. multiple
  21. remote
  22. filterable
  23. allow-create
  24. default-first-option
  25. :placeholder="placeholder"
  26. :remote-method="remoteMethod"
  27. @click.stop
  28. @change="change"
  29. >
  30. <el-option
  31. v-for="(item, itemIndex) in options"
  32. :key="itemIndex"
  33. :label="item"
  34. :value="item"
  35. >
  36. {{ item }}
  37. </el-option>
  38. </el-select>
  39. <template slot="footer">
  40. <el-button @click="close">取消</el-button>
  41. <el-button @click="ok">添加</el-button>
  42. </template>
  43. </Modal>
  44. </div>
  45. </template>
  46. <script>
  47. import Clickoutside from 'element-ui/src/utils/clickoutside'
  48. import { desDecryptId } from '@/utils/crypto-js'
  49. import { taskGetTag } from '@/api/common'
  50. import Modal from '@/components/modal'
  51. export default {
  52. name: 'Tag',
  53. components: { Modal },
  54. directives: { Clickoutside },
  55. props: {
  56. value: {
  57. required: false,
  58. type: Array,
  59. default: () => []
  60. },
  61. type: {
  62. required: false,
  63. type: String,
  64. default: () => ''
  65. },
  66. placeholder: {
  67. required: false,
  68. type: String,
  69. default: () => '请选择标签'
  70. }
  71. },
  72. data() {
  73. return {
  74. visible: false,
  75. isAddTag: true,
  76. options: [],
  77. tagSelectValue: [],
  78. tagValue: []
  79. }
  80. },
  81. methods: {
  82. change() {
  83. this.tagSelectValue = Array.from(new Set(this.tagSelectValue))
  84. this.options = Array.from(new Set([...this.options, ...this.tagSelectValue]))
  85. },
  86. close() {
  87. this.visible = false
  88. },
  89. tabClose(item) {
  90. const newTagValue = [...this.value].filter(elm => elm !== item)
  91. this.$emit('input', newTagValue)
  92. this.$emit('change', newTagValue)
  93. },
  94. ok() {
  95. this.$emit('input', this.tagSelectValue)
  96. this.$emit('change', this.tagSelectValue)
  97. this.close()
  98. },
  99. addTag() {
  100. this.remoteMethod()
  101. this.tagSelectValue = Array.from(new Set(this.value))
  102. this.options = Array.from(new Set([...this.options, ...this.value]))
  103. this.visible = true
  104. },
  105. remoteMethod(searchTag = '') {
  106. // const bizId_id = window.localStorage.getItem('bizId')
  107. const bizId_id = desDecryptId(this.$route.query.bizId_id).replace(/_.*/, '')
  108. console.log(bizId_id)
  109. taskGetTag({
  110. type: this.type,
  111. searchTag,
  112. bizId: bizId_id
  113. }).then(res => {
  114. if (res.code === 200) {
  115. this.options = res.data.map(elm => elm.tag)
  116. }
  117. })
  118. }
  119. }
  120. }
  121. </script>
  122. <style scoped lang="less">
  123. .tips-wrapper {
  124. display: inline-block;
  125. }
  126. /deep/ .el-select__tags {
  127. max-width: calc(100% - 100px) !important;
  128. }
  129. .el-tag {
  130. margin-left: 5px;
  131. user-select: none;
  132. }
  133. .add-tag {
  134. color: #00a0ff;
  135. margin-left: 10px;
  136. cursor: pointer;
  137. }
  138. /deep/ .el-select {
  139. /deep/ .el-input__inner {
  140. border: 1px solid rgba(220, 223, 230) !important;
  141. }
  142. }
  143. </style>