f44c933793432b415055dd1a4ce0bb15c4fa5d90.svn-base 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import JVxeCellMixins from '@/components/jeecg/JVxeTable/mixins/JVxeCellMixins'
  2. // tags 组件的显示组件
  3. export const TagsSpanCell = {
  4. name: 'JVxeTagsCell',
  5. mixins: [JVxeCellMixins],
  6. data() {
  7. return {
  8. innerTags: [],
  9. }
  10. },
  11. watch: {
  12. innerValue: {
  13. immediate: true,
  14. handler(value) {
  15. if (value !== this.innerTags.join(';')) {
  16. let rv = replaceValue(value)
  17. this.innerTags = rv.split(';')
  18. this.handleChangeCommon(rv)
  19. }
  20. },
  21. },
  22. },
  23. methods: {
  24. renderTags(h) {
  25. let tags = []
  26. for (let tag of this.innerTags) {
  27. if (tag) {
  28. let tagProps = {}
  29. let tagStyle = {}
  30. let setTagColor = this.originColumn.setTagColor
  31. if (typeof setTagColor === 'function') {
  32. /**
  33. * 设置 tag 颜色
  34. *
  35. * @param event 包含的字段:
  36. * event.tagValue 当前tag的值
  37. * event.value 当前原始值
  38. * event.row 当前行的所有值
  39. * event.column 当前列的配置
  40. * event.column.own 当前列的原始配置
  41. * @return Array | String 可以返回一个数组,数据第一项是tag背景颜色,第二项是字体颜色。也可以返回一个字符串,即tag背景颜色
  42. */
  43. let color = setTagColor({
  44. tagValue: tag,
  45. value: this.innerValue,
  46. row: this.row,
  47. column: this.column,
  48. })
  49. if (Array.isArray(color)) {
  50. tagProps.color = color[0]
  51. tagStyle.color = color[1]
  52. } else if (color && typeof color === 'string') {
  53. tagProps.color = color
  54. }
  55. }
  56. tags.push(h('a-tag', {
  57. props: tagProps,
  58. style: tagStyle,
  59. }, [tag]))
  60. }
  61. }
  62. return tags
  63. },
  64. },
  65. render(h) {
  66. return h('div', {}, [
  67. this.renderTags(h)
  68. ])
  69. },
  70. }
  71. // tags 组件的输入框
  72. export const TagsInputCell = {
  73. name: 'JVxeTagsInputCell',
  74. mixins: [JVxeCellMixins],
  75. data() {
  76. return {
  77. innerTagValue: '',
  78. }
  79. },
  80. watch: {
  81. innerValue: {
  82. immediate: true,
  83. handler(value) {
  84. if (value !== this.innerTagValue) {
  85. this.handleInputChange(value)
  86. }
  87. },
  88. },
  89. },
  90. methods: {
  91. handleInputChange(value, event) {
  92. this.innerTagValue = replaceValue(value, event)
  93. this.handleChangeCommon(this.innerTagValue)
  94. return this.innerTagValue
  95. },
  96. },
  97. render(h) {
  98. return h('a-input', {
  99. props: {
  100. value: this.innerValue,
  101. ...this.cellProps
  102. },
  103. on: {
  104. change: (event) => {
  105. let {target, target: {value}} = event
  106. let newValue = this.handleInputChange(value, event)
  107. if (newValue !== value) {
  108. target.value = newValue
  109. }
  110. }
  111. },
  112. })
  113. },
  114. }
  115. // 将值每隔两位加上一个分号
  116. function replaceValue(value, event) {
  117. if (value) {
  118. // 首先去掉现有的分号
  119. value = value.replace(/;/g, '')
  120. // 然后再遍历添加分号
  121. let rv = ''
  122. let splitArr = value.split('')
  123. let count = 0
  124. splitArr.forEach((val, index) => {
  125. rv += val
  126. let position = index + 1
  127. if (position % 2 === 0 && position < splitArr.length) {
  128. count++
  129. rv += ';'
  130. }
  131. })
  132. if (event && count > 0) {
  133. let {target, target: {selectionStart}} = event
  134. target.selectionStart = selectionStart + count
  135. target.selectionEnd = selectionStart + count
  136. }
  137. return rv
  138. }
  139. return ''
  140. }