Blame view

ant-design-vue-jeecg/src/views/jeecg/modules/JEditableTable/DefaultTable.vue 8.76 KB
肖超群 authored
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
<template>

  <div>
    <a-button @click="handleTableCheck" type="primary">表单验证</a-button>
    <span style="padding-left:8px;"></span>
    <a-tooltip placement="top" title="获取值,忽略表单验证" :autoAdjustOverflow="true">
      <a-button @click="handleTableGet" type="primary">获取值</a-button>
    </a-tooltip>
    <span style="padding-left:8px;"></span>
    <a-tooltip placement="top" title="模拟加载1000条数据" :autoAdjustOverflow="true">
      <a-button @click="handleTableSet" type="primary">设置值</a-button>
    </a-tooltip>


    <j-editable-table
      ref="editableTable"
      :loading="loading"
      :columns="columns"
      :dataSource="dataSource"
      :rowNumber="true"
      :rowSelection="true"
      :actionButton="true"
      :dragSort="true"
      style="margin-top: 8px;"
      @selectRowChange="handleSelectRowChange">

      <template v-slot:action="props">
        <a @click="handleDelete(props)">删除</a>
      </template>

    </j-editable-table>
  </div>

</template>

<script>
肖超群 authored
37
38
39
40
import moment from 'moment'
import {FormTypes} from '@/utils/JEditableTableUtil'
import {randomUUID, randomNumber} from '@/utils/util'
import JEditableTable from '@/components/jeecg/JEditableTable'
肖超群 authored
41
肖超群 authored
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
export default {
  name: 'DefaultTable',
  components: {JEditableTable},
  data() {
    return {
      loading: false,
      columns: [
        {
          title: '字段名称',
          key: 'dbFieldName',
          // width: '19%',
          width: '300px',
          type: FormTypes.input,
          defaultValue: '',
          placeholder: '请输入${title}',
          validateRules: [
            {
              required: true, // 必填
              message: '请输入${title}' // 显示的文本
            },
            {
              pattern: /^[a-z|A-Z][a-z|A-Z\d_-]{0,}$/, // 正则
              message: '${title}必须以字母开头,可包含数字、下划线、横杠'
            },
            {
              unique: true,
              message: '${title}不能重复'
            },
            {
              handler(type, value, row, column, callback, target) {
                // type 触发校验的类型(input、change、blur)
                // value 当前校验的值
                // callback(flag, message) 方法必须执行且只能执行一次
                //          flag = 是否通过了校验,不填写或者填写 null 代表不进行任何操作
                //          message = 提示的类型,默认使用配置的 message
                // target 行编辑的实例对象
肖超群 authored
78
肖超群 authored
79
80
81
                if (type === 'blur') {
                  if (value === 'abc') {
                    callback(false, '${title}不能是abc')  // false = 未通过校验
肖超群 authored
82
                  } else {
肖超群 authored
83
                    callback(true) // true = 通过验证
肖超群 authored
84
                  }
肖超群 authored
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
                } else {
                  callback(true) // 不填写或者填写 null 代表不进行任何操作
                }
              },
              message: '${title}默认提示'
            }
          ]
        },
        {
          title: '文件域',
          key: 'upload',
          type: FormTypes.upload,
          // width: '19%',
          width: '300px',
          placeholder: '点击上传',
          token: true,
          responseName: 'message',
          action: window._CONFIG['domianURL'] + '/sys/common/upload',
          data: {
            biz: 'temp',
            // 更多扩展参数
肖超群 authored
106
          },
肖超群 authored
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
        },
        {
          title: '字段类型',
          key: 'dbFieldType',
          // width: '18%',
          width: '300px',
          type: FormTypes.select,
          options: [ // 下拉选项
            {title: 'String', value: 'string'},
            {title: 'Integer', value: 'int'},
            {title: 'Double', value: 'double'},
            {title: 'Boolean', value: 'boolean'}
          ],
          allowInput: true,
          defaultValue: '',
          placeholder: '请选择${title}',
          validateRules: [{required: true, message: '请选择${title}'}]
        },
        {
          title: '性别(字典)',
          key: 'sex_dict',
          width: '300px',
          type: FormTypes.select,
          options: [],
          dictCode: 'sex',
          placeholder: '请选择${title}',
          validateRules: [{required: true, message: '请选择${title}'}]
        },
        {
          title: '多选测试',
          key: 'multipleSelect',
          // width: '18%',
          width: '300px',
          type: FormTypes.select,
          props: {'mode': 'multiple'}, // 支持多选
          options: [
            {title: 'String', value: 'string'},
            {title: 'Integer', value: 'int'},
            {title: 'Double', value: 'double'},
            {title: 'Boolean', value: 'boolean'}
          ],
          defaultValue: ['int', 'boolean'], // 多个默认项
          // defaultValue: 'string,double,int', // 也可使用这种方式
          placeholder: '这里可以多选',
          validateRules: [{required: true, message: '请选择${title}'}]
        },
        {
          title: '字段长度',
          key: 'dbLength',
          // width: '8%',
          width: '100px',
          type: FormTypes.inputNumber,
          defaultValue: 32,
          placeholder: '${title}',
          // 是否是统计列,只有 inputNumber 才能设置统计列
          statistics: true,
          validateRules: [{required: true, message: '请输入${title}'}]
        },
        {
          title: '日期',
          key: 'datetime',
          // width: '22%',
          width: '320px',
          type: FormTypes.datetime,
          defaultValue: '2019-4-30 14:52:22',
          placeholder: '请选择${title}',
          validateRules: [{required: true, message: '请选择${title}'}]
        },
        {
          title: '数字',
          key: 'money',
          width: '320px',
          type: FormTypes.inputNumber,
          defaultValue: '100.32',
          placeholder: '请选择${title}',
          validateRules: [{required: true, message: '请选择${title}'}]
        },
        {
          title: '可以为空',
          key: 'isNull',
          // width: '8%',
          width: '100px',
          type: FormTypes.checkbox,
          customValue: ['Y', 'N'], // true ,false
          defaultChecked: false
        },
        {
          type: FormTypes.popup,
          key: 'popup',
          title: 'JPopup',
          width: '180px',
          popupCode: 'demo',
          field: 'name',
          orgFields: 'name',
          destFields: 'name'
        },
        {
          title: '操作',
          key: 'action',
          // width: '8%',
          width: '100px',
          type: FormTypes.slot,
          slotName: 'action',
        }
肖超群 authored
211
肖超群 authored
212
213
214
215
216
217
218
219
220
      ],
      dataSource: [],
      selectedRowIds: []
    }
  },
  mounted() {
    this.randomData(23, false)
  },
  methods: {
肖超群 authored
221
肖超群 authored
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
    /** 表单验证 */
    handleTableCheck() {
      this.$refs.editableTable.getValues((error) => {
        if (error === 0) {
          this.$message.success('验证通过')
        } else {
          this.$message.error('验证未通过')
        }
      })
    },
    /** 获取值,忽略表单验证 */
    handleTableGet() {
      this.$refs.editableTable.getValues((error, values) => {
        console.log('values:', values)
      }, false)
      console.log('deleteIds:', this.$refs.editableTable.getDeleteIds())
肖超群 authored
238
肖超群 authored
239
      this.$message.info('获取值成功,请看控制台输出')
肖超群 authored
240
肖超群 authored
241
242
243
244
245
    },
    /** 模拟加载1000条数据 */
    handleTableSet() {
      this.randomData(1000, true)
    },
肖超群 authored
246
肖超群 authored
247
248
249
    handleSelectRowChange(selectedRowIds) {
      this.selectedRowIds = selectedRowIds
    },
肖超群 authored
250
肖超群 authored
251
252
253
254
255
    /* 随机生成数据 */
    randomData(size, loading = false) {
      if (loading) {
        this.loading = true
      }
肖超群 authored
256
肖超群 authored
257
258
259
260
      let randomDatetime = () => {
        let time = parseInt(randomNumber(1000, 9999999999999))
        return moment(new Date(time)).format('YYYY-MM-DD HH:mm:ss')
      }
肖超群 authored
261
肖超群 authored
262
263
264
265
266
267
268
269
270
271
272
273
274
275
      let begin = Date.now()
      let values = []
      for (let i = 0; i < size; i++) {
        values.push({
          id: randomUUID(),
          dbFieldName: `name_${i + 1}`,
          // dbFieldTxt: randomString(10),
          multipleSelect: ['string', ['int', 'double', 'boolean'][randomNumber(0, 2)]],
          dbFieldType: ['string', 'int', 'double', 'boolean'][randomNumber(0, 3)],
          dbLength: randomNumber(0, 233),
          datetime: randomDatetime(),
          isNull: ['Y', 'N'][randomNumber(0, 1)]
        })
      }
肖超群 authored
276
肖超群 authored
277
278
279
      this.dataSource = values
      let end = Date.now()
      let diff = end - begin
肖超群 authored
280
肖超群 authored
281
282
283
284
285
      if (loading && diff < size) {
        setTimeout(() => {
          this.loading = false
        }, size - diff)
      }
肖超群 authored
286
肖超群 authored
287
    },
肖超群 authored
288
肖超群 authored
289
290
291
    handleDelete(props) {
      let {rowId, target} = props
      target.removeRows(rowId)
肖超群 authored
292
293
    }
  }
肖超群 authored
294
}
肖超群 authored
295
296
297
298
299
</script>

<style scoped>

</style>