index.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <div class="form-drawing-table" :class="{'is-active':isActive}">
  3. <i class="el-icon-rank show-focus handle-icon move-icon"/>
  4. <span class="right-top item-field-key">{{fieldKey}}</span>
  5. <div class="right-bottom show-focus">
  6. <i class="el-icon-document-copy handle-icon" @click.stop="copyItem"></i>
  7. <i class="el-icon-delete handle-icon" @click.stop="deleteItem"></i>
  8. </div>
  9. <div>
  10. <table-design :config="tableConfig" :hide-delete="true">
  11. <vue-draggable :model-value="list"
  12. @update:modelValue="updateOptions"
  13. :group="{name: 'components',put: judgePut}"
  14. handle=".move-icon"
  15. ghost-class="table-ghost"
  16. @add="addOptions"
  17. :animation="200"
  18. item-key="id"
  19. :component-data="{ class:'form-table-options', style: {height: '100%',border:'1px dashed #ddd',width: '100%',display: 'flex',boxSizing:'border-box'}}">
  20. <template #item="{element, index}">
  21. <form-drawing-item :field-key="element.key"
  22. :style="itemStyle(element)"
  23. :selectId="selectId"
  24. :is-active="selectId === element.id"
  25. :config="element.config"
  26. :show-copy="false"
  27. @click.stop="selectOption(element)"
  28. @onDelete="deleteOption(index)"></form-drawing-item>
  29. </template>
  30. </vue-draggable>
  31. </table-design>
  32. </div>
  33. </div>
  34. </template>
  35. <script>
  36. import { computed, ref } from 'vue'
  37. import FormDrawingItem from './form-drawing-item'
  38. import TableDesign from './form-drawing-table/design'
  39. import { handleFormConfig } from './handle-config'
  40. import { getCopyRow } from '../util'
  41. import VueDraggable from 'vuedraggable'
  42. import { getNextItem } from '@cip/utils/util'
  43. import './form-drawing-table/index.less'
  44. export default {
  45. name: 'FormDrawingTable',
  46. components: {
  47. VueDraggable, FormDrawingItem, TableDesign
  48. },
  49. props: {
  50. isActive: Boolean,
  51. fieldKey: String,
  52. config: Object,
  53. selectId: [String, Number]
  54. },
  55. emits: ['onDelete', 'onCopy', 'onSelectItem', 'update:config'],
  56. setup (props, { emit }) {
  57. const computedConfig = computed(() => {
  58. return handleFormConfig(props.config, [], {
  59. }, { })
  60. })
  61. const tableConfig = computed(() => {
  62. // 表格中的组件全部变为可读
  63. return list.value?.map(i => {
  64. i.config.writable = false
  65. i.config.readable = true
  66. return i
  67. })
  68. })
  69. const list = computed(() => {
  70. return props.config.list || []
  71. })
  72. // 列表布局样式
  73. const itemStyle = computed(() => {
  74. return row => {
  75. if (row.config.width) {
  76. return { flexShrink: 0, flexBasis: row.config.width }
  77. } else {
  78. return { flexShrink: 0, flexGrow: 1 }
  79. }
  80. }
  81. })
  82. const deleteItem = () => {
  83. emit('onDelete')
  84. }
  85. const copyItem = () => {
  86. emit('onCopy')
  87. }
  88. const updateConfig = (val) => {
  89. emit('update:config', val)
  90. }
  91. const selectItem = (element) => {
  92. emit('onSelectItem', element)
  93. }
  94. const unique = (array) => {
  95. const result = []; const sign = {}
  96. // eslint-disable-next-line
  97. for (const i of array) {
  98. if (!sign[i.key]) {
  99. result.push(i)
  100. sign[i.key] = i
  101. }
  102. }
  103. return result
  104. }
  105. // 修改list配置
  106. const updateOptions = (list) => {
  107. const config = ref(props.config || {})
  108. list = unique(list)
  109. config.value.list = list.map(option => {
  110. const width = option.config.width !== undefined ? option.config.width : '200px'
  111. return {
  112. ...option,
  113. config: {
  114. ...option.config,
  115. width,
  116. writable: true
  117. }
  118. }
  119. }
  120. )
  121. updateConfig(config.value)
  122. }
  123. // 添加一个字段
  124. const addOptions = ({ newIndex }) => {
  125. const newItem = list.value[newIndex]
  126. if (newItem) {
  127. emit('onSelectItem', newItem)
  128. }
  129. }
  130. const selectOption = (element) => {
  131. emit('onSelectItem', element)
  132. }
  133. const deleteOption = (index) => {
  134. const config = props.config
  135. const nextItem = getNextItem(config.list, index)
  136. if (nextItem) selectOption(nextItem)
  137. config.list.splice(index, 1)
  138. updateConfig(config)
  139. }
  140. const copyOption = (index) => {
  141. const config = props.config
  142. const newRow = getCopyRow(config.list[index])
  143. config.list.splice(index + 1, 0, newRow)
  144. updateConfig(config)
  145. selectOption(newRow)
  146. }
  147. const judgePut = (...val) => {
  148. const dom = val?.[2] ?? {}
  149. return !dom.classList.contains('disabled-table')
  150. }
  151. return {
  152. list,
  153. tableConfig,
  154. computedConfig,
  155. deleteItem,
  156. copyItem,
  157. updateConfig,
  158. selectItem,
  159. updateOptions,
  160. addOptions,
  161. selectOption,
  162. deleteOption,
  163. copyOption,
  164. judgePut,
  165. itemStyle
  166. }
  167. }
  168. }
  169. </script>