Blame view

ant-design-vue-jeecg/src/views/jeecg/JVxeDemo/JVxeDemo1.vue 8.73 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
37
38
<template>
  <j-vxe-table
    ref="vTable"
    toolbar
    row-number
    row-selection
    drag-sort
    keep-source
    :height="580"
    :loading="loading"
    :dataSource="dataSource"
    :columns="columns"
    style="margin-top: 8px;"
    @valueChange="handleValueChange"
  >

    <template v-slot:toolbarSuffix>
      <a-button @click="handleTableCheck">表单验证</a-button>
      <a-tooltip placement="top" title="获取值,忽略表单验证" :autoAdjustOverflow="true">
        <a-button @click="handleTableGet">获取值</a-button>
      </a-tooltip>
      <a-tooltip placement="top" title="模拟加载1000条数据" :autoAdjustOverflow="true">
        <a-button @click="handleTableSet">设置值</a-button>
      </a-tooltip>
    </template>

    <template v-slot:action="props">
      <a @click="handleCK(props)">查看</a>
      <a-divider type="vertical"/>
      <a-popconfirm title="确定删除吗?" @confirm="handleDL(props)">
        <a>删除</a>
      </a-popconfirm>
    </template>

  </j-vxe-table>
</template>

<script>
肖超群 authored
39
40
41
import moment from 'moment'
import {pushIfNotExist, randomNumber, randomUUID} from '@/utils/util'
import {JVXETypes} from '@/components/jeecg/JVxeTable'
肖超群 authored
42
肖超群 authored
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
export default {
  name: 'JVxeDemo1',
  data() {
    return {
      loading: false,
      columns: [
        {
          title: '不可编辑',
          key: 'normal',
          type: JVXETypes.normal,
          width: '180px',
          fixed: 'left',
          defaultValue: 'normal-new',
        },
        {
          title: '单行文本',
          key: 'input',
          type: JVXETypes.input,
          width: '180px',
          defaultValue: '',
          placeholder: '请输入${title}',
          validateRules: [
            {
              required: true, // 必填
              message: '请输入${title}' // 显示的文本
            },
            {
              pattern: /^[a-z|A-Z][a-z|A-Z\d_-]*$/, // 正则
              message: '${title}必须以字母开头,可包含数字、下划线、横杠'
            },
            {
              unique: true,
              message: '${title}不能重复'
            },
            {
              handler({cellValue, row, column}, callback, target) {
                // cellValue 当前校验的值
                // callback(flag, message) 方法必须执行且只能执行一次
                //          flag = 是否通过了校验,不填写或者填写 null 代表不进行任何操作
                //          message = 提示的类型,默认使用配置的 message
                // target 行编辑的实例对象
                if (cellValue === 'abc') {
                  callback(false, '${title}不能是abc')  // false = 未通过校验
                } else {
                  callback(true) // true = 通过验证
                }
肖超群 authored
89
              },
肖超群 authored
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
              message: '${title}默认提示'
            }
          ]
        },
        {
          title: '多行文本',
          key: 'textarea',
          type: JVXETypes.textarea,
          width: '200px',
        },
        {
          title: '数字',
          key: 'number',
          type: JVXETypes.inputNumber,
          width: '80px',
          defaultValue: 32,
          // 【统计列】sum = 求和、average = 平均值
          statistics: ['sum', 'average'],
        },
        {
          title: '下拉框',
          key: 'select',
          type: JVXETypes.select,
          width: '180px',
          // 下拉选项
          options: [
            {title: 'String', value: 'string'},
            {title: 'Integer', value: 'int'},
            {title: 'Double', value: 'double'},
            {title: 'Boolean', value: 'boolean'}
          ],
          allowInput: true,
          placeholder: '请选择'
        },
        {
          title: '下拉框_字典',
          key: 'select_dict',
          type: JVXETypes.select,
          width: '180px',
          options: [],
          dictCode: 'sex',
          placeholder: '请选择',
        },
        {
          title: '下拉框_多选',
          key: 'select_multiple',
          type: JVXETypes.selectMultiple,
          width: '205px',
          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: '多选',
        },
肖超群 authored
148
肖超群 authored
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
        {
          title: '下拉框_搜索',
          key: 'select_search',
          type: JVXETypes.selectSearch,
          width: '180px',
          options: [
            {title: 'String', value: 'string'},
            {title: 'Integer', value: 'int'},
            {title: 'Double', value: 'double'},
            {title: 'Boolean', value: 'boolean'}
          ],
        },
        {
          title: '日期时间',
          key: 'datetime',
          type: JVXETypes.datetime,
          width: '200px',
          defaultValue: '2019-4-30 14:52:22',
          placeholder: '请选择',
        },
        {
          title: '复选框',
          key: 'checkbox',
          type: JVXETypes.checkbox,
          width: '100px',
          customValue: ['Y', 'N'], // true ,false
          defaultChecked: false,
        },
        {
          title: '操作',
          key: 'action',
          type: JVXETypes.slot,
          fixed: 'right',
          minWidth: '100px',
          align: 'center',
          slotName: 'action',
        }
      ],
      dataSource: [],
    }
肖超群 authored
189
肖超群 authored
190
  },
肖超群 authored
191
肖超群 authored
192
193
194
195
  created() {
    this.randomPage(0, 20, true)
  },
  methods: {
肖超群 authored
196
肖超群 authored
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
    handleCK(props) {
      this.$message.success('请在控制台查看输出')
      // 参数介绍:
      // props.value          当前单元格的值
      // props.row            当前行的数据
      // props.rowId          当前行ID
      // props.rowIndex       当前行下标
      // props.column         当前列的配置
      // props.columnIndex    当前列下标
      // props.$table         vxe实例,可以调用vxe内置方法
      // props.target         JVXE实例,可以调用JVXE内置方法
      // props.caseId         JVXE实例唯一ID
      // props.scrolling      是否正在滚动
      // props.triggerChange  触发change事件,用于更改slot的值
      console.log('查看: ', {props})
    },
肖超群 authored
213
肖超群 authored
214
215
216
217
    handleDL(props) {
      // 调用删除方法
      props.target.removeRows(props.row)
    },
肖超群 authored
218
肖超群 authored
219
220
221
    handleValueChange(event) {
      console.log('handleValueChange.event: ', event)
    },
肖超群 authored
222
肖超群 authored
223
224
225
226
227
228
229
230
231
232
233
    /** 表单验证 */
    handleTableCheck() {
      this.$refs.vTable.validateTable().then(errMap => {
        if (errMap) {
          console.log('表单验证未通过:', {errMap})
          this.$message.error('验证未通过,请在控制台查看详细')
        } else {
          this.$message.success('验证通过')
        }
      })
    },
肖超群 authored
234
肖超群 authored
235
236
237
238
239
240
    /** 获取值,忽略表单验证 */
    handleTableGet() {
      const values = this.$refs.vTable.getTableData()
      console.log('获取值:', {values})
      this.$message.success('获取值成功,请看控制台输出')
    },
肖超群 authored
241
肖超群 authored
242
243
244
245
    /** 模拟加载1000条数据 */
    handleTableSet() {
      this.randomPage(1, 1000, true)
    },
肖超群 authored
246
肖超群 authored
247
248
249
250
251
    /* 随机生成数据 */
    randomPage(current, pageSize, loading = false) {
      if (loading) {
        this.loading = true
      }
肖超群 authored
252
肖超群 authored
253
254
255
256
      let randomDatetime = () => {
        let time = randomNumber(1000, 9999999999999)
        return moment(new Date(time)).format('YYYY-MM-DD HH:mm:ss')
      }
肖超群 authored
257
肖超群 authored
258
      let limit = (current - 1) * pageSize
肖超群 authored
259
肖超群 authored
260
      let options = ['string', 'int', 'double', 'boolean']
肖超群 authored
261
肖超群 authored
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
      let begin = Date.now()
      let values = []
      for (let i = 0; i < pageSize; i++) {
        values.push({
          id: randomUUID(),
          normal: `normal-${(limit + i) + 1}`,
          input: `text-${(limit + i) + 1}`,
          textarea: `textarea-${(limit + i) + 1}`,
          number: randomNumber(0, 233),
          select: options[randomNumber(0, 3)],
          select_dict: randomNumber(1, 2).toString(),
          select_multiple: (() => {
            let length = randomNumber(1, 4)
            let arr = []
            for (let j = 0; j < length; j++) {
              pushIfNotExist(arr, options[randomNumber(0, 3)])
            }
            return arr
          })(),
          select_search: options[randomNumber(0, 3)],
          datetime: randomDatetime(),
          checkbox: ['Y', 'N'][randomNumber(0, 1)]
        })
      }
肖超群 authored
286
肖超群 authored
287
288
289
      this.dataSource = values
      let end = Date.now()
      let diff = end - begin
肖超群 authored
290
肖超群 authored
291
292
293
294
      if (loading && diff < pageSize) {
        setTimeout(() => {
          this.loading = false
        }, pageSize - diff)
肖超群 authored
295
      }
肖超群 authored
296
肖超群 authored
297
298
    }
  }
肖超群 authored
299
}
肖超群 authored
300
301
302
303
304
</script>

<style scoped>

</style>