<template>
<a-card :bordered="false">
<a-button @click="handleTableGet" type="primary" style="margin-bottom: 10px;">获取值a-button>
<a-button @click="handleTableCheck" style="margin-bottom: 10px;margin-left: 10px">表单验证a-button>
<j-vxe-table
ref="xTable"
toolbar
bordered
height="auto"
:maxHeight="300"
:columns="columns"
:dataSource="dataSource"
keep-source
:pagination="pagination"
@pageChange="handlePageChange"
@edit-closed="handleEditClosed"
>
<template v-slot:action="props">
<a-button type="danger" @click="handleDL(props)">
删除
a-button>
template>
j-vxe-table>
a-card>
template>
<script>
import { JVXETypes } from '@/components/jeecg/JVxeTable'
import { getAction, postJson } from '../../api/manage'
export default {
name: 'List',
data() {
return {
pagination: {
current: 1,
pageSize: 10,
pageSizeOptions: ['10', '20', '30', '50'],
total: 1
},
columns: [
{
title: 'id',
key: 'id',
type: JVXETypes.hidden
},
{
title: '用户名',
key: 'usernme',
type: JVXETypes.input,
placeholder: '请输入${title}',
allowClear: true,
validateRules: [
{
required: true,
message: '${title}不能为空'
}]
},
{
title: '年龄',
key: 'age',
type: JVXETypes.inputNumber,
placeholder: '请输入${title}',
defaultValue: 18,
allowClear: true,
validateRules: [{ required: true, message: '${title}不能为空' }]
},
{
title: 'JP-性别',
key: 'sex',
type: JVXETypes.select,
dictCode: 'sex',
width: '100px',
validateRules: [
{
required: true,
message: '${title}不能为空'
}],
allowClear: true
},
{
title: '操作',
key: 'action',
type: JVXETypes.slot,
fixed: 'right',
minWidth: '100px',
align: 'center',
slotName: 'action'
}
],
dataSource: []
}
},
created() {
this.loadData(1)
},
mounted() {
},
methods: {
handleTableCheck() {
this.$refs.xTable.validateTable().then(errMap => {
if (errMap) {
console.log('表单验证未通过:', { errMap })
this.$message.error('验证未通过,请在控制台查看详细')
} else {
this.$message.success('验证通过')
}
})
},
handleTableGet() {
const values = this.$refs.xTable.getTableData()
console.log('获取值:', { values })
this.$message.success('获取值成功,请看控制台输出')
},
handlePageChange(event) {
this.pagination.current = event.current
this.pagination.pageSize = event.pageSize
this.loadData()
},
loadData(arg) {
if (arg == 1) {
this.pagination.current = 1
}
let param = {}
param.pageNo = this.pagination.current
param.pageSize = this.pagination.pageSize
getAction('/test/studyTest/list', param).then((res) => {
if (res.success) {
this.dataSource = res.result.records
this.pagination.total = res.result.total
}
})
},
handleDL(props) {
props.target.removeRows(props.row)
},
handleEditClosed(event) {
let { $table, row, column } = event
let field = column.property
let cellValue = row[field]
delete row.action
$table.validate(row).then((errMap) => {
if (!errMap) {
if (row.hasOwnProperty('id')) {
postJson('/test/studyTest/add', JSON.stringify(row), 'post').then((res) => {
if (res.success) {
this.loadData()
}
})
} else {
postJson('/test/studyTest/edit', JSON.stringify(row), 'put').then((res) => {
if (res.success) {
$table.reloadRow(row, null, field)
}
})
}
}
}).catch(err => {
})
}
}
}
script>
<style scoped>
style>