222
schangxiang@126.com
2025-04-30 9bec4dcae002f36aa23231da11cb03a156b40110
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { computed, defineComponent, nextTick, onMounted, ref, watch } from 'vue'
import styles from './TableArray.module.scss'
import CommonTable from '@/components/CommonTable/CommonTable'
import { debounce } from 'lodash'
 
interface Category {
  name: string
  description: string
  propertyKey: string
  propertyType: string
  propertyValue: boolean
  sort: number
  visible: boolean
  elementAttributes: any[]
  elementType: string | null
  propertyData: any[] | null
  pattern: string
  required: string
  ruleMessage: string
}
const elTypeMap: Record<string, string> = {
  String: 'input',
  Boolean: 'switch',
  Enum: 'select',
  Int32: 'inputNumber',
  Int64: 'inputNumber',
  Double: 'inputNumber',
  Float: 'inputNumber',
  Object: 'input',
  Variable: 'variable',
  FlowItemKey: 'flowItemKey',
}
export default defineComponent({
  name: 'TableArray',
  props: {
    elementAttributes: {
      type: Array,
      default: () => [],
    },
    propertyType: {
      type: String,
      default: 'ArrayList',
    },
    elementType: {
      type: String,
      default: '',
    },
    modelValue: {
      type: [Array, Object, String],
      default: () => [],
    },
  },
  emits: ['update:modelValue'],
  setup(props, { emit, expose }) {
    const commonRef = ref()
    const data = computed<any>({
      get() {
        return props.modelValue
      },
      set(v) {
        emit('update:modelValue', v)
      },
    })
 
    const validator = (
      rule: {
        field: string
        fullField: string
        value: any
        pattern: string
        message: string
      },
      value: any,
      callback: Function
    ) => {
      const pattern = rule.pattern
      const result = new RegExp(pattern).test(value)
      if (pattern && !result) {
        callback(rule.message)
      }
    }
 
    const valid = () => {
      return !!commonRef.value?.getData()
    }
 
    const columns = computed(() => {
      return props.elementAttributes
        .filter((element: any) => element.visible)
        .map((element) => {
          const category = element as Category
          return {
            title: category.name,
            field: category.propertyKey,
            el: elTypeMap[category.propertyType],
            defaultValue: category.propertyValue,
            customRequired: !!category.pattern,
            pattern: category.pattern,
            ruleMessage: category.ruleMessage,
            validator,
            required: category.required,
            options: category.propertyData?.map((item: any) => {
              return {
                label: item.name,
                tip: item.description,
                value: item.value,
              }
            }),
            props: {
              controlsPosition: 'right',
              title: category.description,
              type: 'select',
            },
          }
        })
    })
 
    const onCreateRow = () => {
      const defaultRow: Record<string, any> = {}
      columns.value.forEach((column: Record<string, any>) => {
        defaultRow[column.field] = column.defaultValue
      })
      return defaultRow
    }
 
    expose({ valid })
 
    return () => {
      return (
        <div class={styles.tableArray}>
          <CommonTable
            ref={commonRef}
            isContextMenu={true}
            v-model:dataSource={data.value[props.elementType]}
            columns={columns.value}
            isFooter={true}
            isDrag={false}
            isChecked={false}
            isSeq={false}
            autoHeight="auto"
            maxHeight="250px"
            create={onCreateRow}
          />
        </div>
      )
    }
  },
})