vue-vben-admin/src/views/demo/page/form/high/PersonTable.vue

148 lines
3.3 KiB
Vue
Raw Normal View History

2020-11-17 17:02:42 +08:00
<template>
<div>
<BasicTable @register="registerTable" @edit-change="handleEditChange">
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'action'">
2023-10-12 14:50:32 +08:00
<TableAction :actions="createActions(record)" />
</template>
2020-11-17 17:02:42 +08:00
</template>
</BasicTable>
2021-01-28 23:28:50 +08:00
<a-button block class="mt-5" type="dashed" @click="handleAdd"> 新增成员 </a-button>
2020-11-17 17:02:42 +08:00
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import {
BasicTable,
useTable,
TableAction,
BasicColumn,
ActionItem,
EditRecordRow,
} from '/@/components/Table';
const columns: BasicColumn[] = [
{
title: '成员姓名',
dataIndex: 'name',
2020-12-31 22:44:32 +08:00
editRow: true,
2020-11-17 17:02:42 +08:00
},
{
title: '工号',
dataIndex: 'no',
2020-12-31 22:44:32 +08:00
editRow: true,
2020-11-17 17:02:42 +08:00
},
{
title: '所属部门',
dataIndex: 'dept',
2020-12-31 22:44:32 +08:00
editRow: true,
2020-11-17 17:02:42 +08:00
},
];
const data: any[] = [
{
name: 'John Brown',
no: '00001',
dept: 'New York No. 1 Lake Park',
},
{
name: 'John Brown2',
no: '00002',
dept: 'New York No. 2 Lake Park',
},
{
name: 'John Brown3',
no: '00003',
dept: 'New York No. 3Lake Park',
},
];
export default defineComponent({
2021-01-28 23:28:50 +08:00
components: { BasicTable, TableAction },
2020-11-17 17:02:42 +08:00
setup() {
const [registerTable, { getDataSource }] = useTable({
columns: columns,
showIndexColumn: false,
dataSource: data,
actionColumn: {
width: 160,
title: '操作',
dataIndex: 'action',
// slots: { customRender: 'action' },
2020-11-17 17:02:42 +08:00
},
2023-02-22 18:32:07 +08:00
scroll: { y: '100%' },
2020-11-17 17:02:42 +08:00
pagination: false,
});
function handleEdit(record: EditRecordRow) {
2020-12-31 22:44:32 +08:00
record.onEdit?.(true);
2020-11-17 17:02:42 +08:00
}
function handleCancel(record: EditRecordRow) {
2020-12-31 22:44:32 +08:00
record.onEdit?.(false);
2020-11-17 17:02:42 +08:00
if (record.isNew) {
const data = getDataSource();
const index = data.findIndex((item) => item.key === record.key);
data.splice(index, 1);
}
}
function handleSave(record: EditRecordRow) {
2020-12-31 22:44:32 +08:00
record.onEdit?.(false, true);
2020-11-17 17:02:42 +08:00
}
function handleEditChange(data: Recordable) {
console.log(data);
}
2020-11-17 17:02:42 +08:00
function handleAdd() {
const data = getDataSource();
const addRow: EditRecordRow = {
name: '',
no: '',
dept: '',
editable: true,
isNew: true,
2021-02-07 23:03:59 +08:00
key: `${Date.now()}`,
2020-11-17 17:02:42 +08:00
};
data.push(addRow);
}
2023-10-12 14:50:32 +08:00
function createActions(record: EditRecordRow): ActionItem[] {
2020-11-17 17:02:42 +08:00
if (!record.editable) {
return [
{
label: '编辑',
onClick: handleEdit.bind(null, record),
},
{
label: '删除',
},
];
}
return [
{
label: '保存',
2023-10-12 14:50:32 +08:00
onClick: handleSave.bind(null, record),
2020-11-17 17:02:42 +08:00
},
{
label: '取消',
popConfirm: {
title: '是否取消编辑',
2023-10-12 14:50:32 +08:00
confirm: handleCancel.bind(null, record),
2020-11-17 17:02:42 +08:00
},
},
];
}
return {
registerTable,
handleEdit,
createActions,
handleAdd,
getDataSource,
handleEditChange,
2020-11-17 17:02:42 +08:00
};
},
});
</script>