|
@@ -0,0 +1,187 @@
|
|
|
+<template>
|
|
|
+ <div class="p-4">
|
|
|
+ <BasicTable
|
|
|
+ ref="tableRef"
|
|
|
+ @register="registerTable"
|
|
|
+ rowKey="id"
|
|
|
+ >
|
|
|
+ <template #action="{ record, column }">
|
|
|
+ <TableAction :actions="createActions(record, column)" />
|
|
|
+ </template>
|
|
|
+ <template #form-custom > custom-slot </template>
|
|
|
+
|
|
|
+ <template #toolbar>
|
|
|
+ <a-button type="primary" @click="addColumn" >
|
|
|
+ {{ '添加' }}
|
|
|
+ </a-button>
|
|
|
+ </template>
|
|
|
+ </BasicTable>
|
|
|
+ <Add @register="addRegister" />
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+<script lang="ts">
|
|
|
+ import { defineComponent, ref, unref } from 'vue';
|
|
|
+ import Add from './add.vue'
|
|
|
+import { useModal } from '/@/components/Modal';
|
|
|
+ import {
|
|
|
+ BasicTable,
|
|
|
+ useTable,
|
|
|
+ TableAction,
|
|
|
+ BasicColumn,
|
|
|
+ ActionItem,
|
|
|
+ EditRecordRow,
|
|
|
+ TableActionType
|
|
|
+ } from '/@/components/Table';
|
|
|
+
|
|
|
+
|
|
|
+ const columns: BasicColumn[] = [
|
|
|
+ {
|
|
|
+ title: 'id',
|
|
|
+ dataIndex: 'id',
|
|
|
+ editComponentProps: {
|
|
|
+ prefix: '$',
|
|
|
+ },
|
|
|
+ width: 200,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ title: '账户名',
|
|
|
+ dataIndex: 'name',
|
|
|
+ editRow: true,
|
|
|
+ width: 200,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ title: '密码',
|
|
|
+ dataIndex: 'password',
|
|
|
+ editRow: true,
|
|
|
+ width: 200,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ title: '详情',
|
|
|
+ dataIndex: 'detail',
|
|
|
+ editRow: true,
|
|
|
+ width: 200,
|
|
|
+ },
|
|
|
+ ];
|
|
|
+
|
|
|
+ export default defineComponent({
|
|
|
+ components: { BasicTable, TableAction, Add },
|
|
|
+ setup() {
|
|
|
+ const tableRef = ref<Nullable<TableActionType>>(null);
|
|
|
+ const currentEditKeyRef = ref('');
|
|
|
+ const [registerTable] = useTable({
|
|
|
+ title: "基础示例",
|
|
|
+ titleHelpMessage: "温馨提醒",
|
|
|
+ rowSelection: { type: 'checkbox' },
|
|
|
+ columns: columns,
|
|
|
+ clickToRowSelect: false, // 点击行不勾选
|
|
|
+ dataSource: [{id:1,name:'vben',detail:'super admin', password: '123456'},{id:2,name:'test',detail:'test admin', password: '123456'}],
|
|
|
+ showIndexColumn: false,
|
|
|
+ actionColumn: {
|
|
|
+ width: 160,
|
|
|
+ title: 'Action',
|
|
|
+ dataIndex: 'action',
|
|
|
+ slots: { customRender: 'action' },
|
|
|
+ },
|
|
|
+ });
|
|
|
+ const [addRegister, { openModal: openAdd }] = useModal();
|
|
|
+ const rowClick = (e: any) => {
|
|
|
+ console.log(e)
|
|
|
+ }
|
|
|
+
|
|
|
+ function getTableAction() { // 获取组件
|
|
|
+ const tableAction = unref(tableRef);
|
|
|
+ if (!tableAction) {
|
|
|
+ throw new Error('tableAction is null');
|
|
|
+ }
|
|
|
+ return tableAction;
|
|
|
+ }
|
|
|
+
|
|
|
+ function getSelectRowList() { // 获取选中行
|
|
|
+ console.log(getTableAction().getSelectRows());
|
|
|
+ }
|
|
|
+ function getSelectRowKeyList() { // 获取选中行的key --- id
|
|
|
+ console.log(getTableAction().getSelectRowKeys());
|
|
|
+ }
|
|
|
+
|
|
|
+ function addColumn() {
|
|
|
+ console.log('添加')
|
|
|
+ openAdd(true, {
|
|
|
+ data: '苏轼',
|
|
|
+ info: '苏州',
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ function handleSubmit() {
|
|
|
+ console.log('handleSubmit')
|
|
|
+ // console.log(data)
|
|
|
+ }
|
|
|
+
|
|
|
+ function handleEdit(record: EditRecordRow) {
|
|
|
+ currentEditKeyRef.value = record.id; // record.key
|
|
|
+ record.onEdit?.(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ function handleCancel(record: EditRecordRow) {
|
|
|
+ currentEditKeyRef.value = '';
|
|
|
+ record.onEdit?.(false, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ function handleDelete(record: Recordable) {
|
|
|
+ console.log('点击了删除', record);
|
|
|
+ const data = getTableAction().getDataSource()
|
|
|
+ console.log(data)
|
|
|
+ getTableAction().setTableData(data.map(item => item.id !== record.id))
|
|
|
+ }
|
|
|
+
|
|
|
+ function createActions(record: EditRecordRow, column: BasicColumn): ActionItem[] {
|
|
|
+ if (!record.editable) {
|
|
|
+ return [
|
|
|
+ {
|
|
|
+ label: '编辑',
|
|
|
+ icon: 'ant-design:edit-outlined',
|
|
|
+ color: 'warning',
|
|
|
+ disabled: currentEditKeyRef.value ? currentEditKeyRef.value !== record.key : false,
|
|
|
+ onClick: handleEdit.bind(null, record),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ label: '删除',
|
|
|
+ color: 'error',
|
|
|
+ icon: 'ic:outline-delete-outline',
|
|
|
+ disabled: currentEditKeyRef.value ? currentEditKeyRef.value !== record.key : false,
|
|
|
+ onClick: handleDelete.bind(null, record),
|
|
|
+ },
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ return [
|
|
|
+ {
|
|
|
+ label: '保存',
|
|
|
+ onClick: handleSave.bind(null, record, column),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ label: '取消',
|
|
|
+ popConfirm: {
|
|
|
+ title: '是否取消编辑',
|
|
|
+ confirm: handleCancel.bind(null, record, column),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ return {
|
|
|
+
|
|
|
+ // ...toRefs(state),
|
|
|
+ tableRef,
|
|
|
+ registerTable,
|
|
|
+ rowClick,
|
|
|
+ addColumn,
|
|
|
+ handleEdit,
|
|
|
+ createActions,
|
|
|
+ getTableAction,
|
|
|
+ getSelectRowList,
|
|
|
+ getSelectRowKeyList,
|
|
|
+ addRegister,
|
|
|
+ handleSubmit
|
|
|
+
|
|
|
+ };
|
|
|
+ },
|
|
|
+ });
|
|
|
+</script>
|