PersonTable.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <div>
  3. <BasicTable @register="registerTable" @edit-change="handleEditChange">
  4. <template #bodyCell="{ column, record }">
  5. <template v-if="column.key === 'action'">
  6. <TableAction :actions="createActions(record, column)" />
  7. </template>
  8. </template>
  9. </BasicTable>
  10. <a-button block class="mt-5" type="dashed" @click="handleAdd"> 新增成员 </a-button>
  11. </div>
  12. </template>
  13. <script lang="ts">
  14. import { defineComponent } from 'vue';
  15. import {
  16. BasicTable,
  17. useTable,
  18. TableAction,
  19. BasicColumn,
  20. ActionItem,
  21. EditRecordRow,
  22. } from '/@/components/Table';
  23. const columns: BasicColumn[] = [
  24. {
  25. title: '成员姓名',
  26. dataIndex: 'name',
  27. editRow: true,
  28. },
  29. {
  30. title: '工号',
  31. dataIndex: 'no',
  32. editRow: true,
  33. },
  34. {
  35. title: '所属部门',
  36. dataIndex: 'dept',
  37. editRow: true,
  38. },
  39. ];
  40. const data: any[] = [
  41. {
  42. name: 'John Brown',
  43. no: '00001',
  44. dept: 'New York No. 1 Lake Park',
  45. },
  46. {
  47. name: 'John Brown2',
  48. no: '00002',
  49. dept: 'New York No. 2 Lake Park',
  50. },
  51. {
  52. name: 'John Brown3',
  53. no: '00003',
  54. dept: 'New York No. 3Lake Park',
  55. },
  56. ];
  57. export default defineComponent({
  58. components: { BasicTable, TableAction },
  59. setup() {
  60. const [registerTable, { getDataSource }] = useTable({
  61. columns: columns,
  62. showIndexColumn: false,
  63. dataSource: data,
  64. actionColumn: {
  65. width: 160,
  66. title: '操作',
  67. dataIndex: 'action',
  68. // slots: { customRender: 'action' },
  69. },
  70. pagination: false,
  71. });
  72. function handleEdit(record: EditRecordRow) {
  73. record.onEdit?.(true);
  74. }
  75. function handleCancel(record: EditRecordRow) {
  76. record.onEdit?.(false);
  77. if (record.isNew) {
  78. const data = getDataSource();
  79. const index = data.findIndex((item) => item.key === record.key);
  80. data.splice(index, 1);
  81. }
  82. }
  83. function handleSave(record: EditRecordRow) {
  84. record.onEdit?.(false, true);
  85. }
  86. function handleEditChange(data: Recordable) {
  87. console.log(data);
  88. }
  89. function handleAdd() {
  90. const data = getDataSource();
  91. const addRow: EditRecordRow = {
  92. name: '',
  93. no: '',
  94. dept: '',
  95. editable: true,
  96. isNew: true,
  97. key: `${Date.now()}`,
  98. };
  99. data.push(addRow);
  100. }
  101. function createActions(record: EditRecordRow, column: BasicColumn): ActionItem[] {
  102. if (!record.editable) {
  103. return [
  104. {
  105. label: '编辑',
  106. onClick: handleEdit.bind(null, record),
  107. },
  108. {
  109. label: '删除',
  110. },
  111. ];
  112. }
  113. return [
  114. {
  115. label: '保存',
  116. onClick: handleSave.bind(null, record, column),
  117. },
  118. {
  119. label: '取消',
  120. popConfirm: {
  121. title: '是否取消编辑',
  122. confirm: handleCancel.bind(null, record, column),
  123. },
  124. },
  125. ];
  126. }
  127. return {
  128. registerTable,
  129. handleEdit,
  130. createActions,
  131. handleAdd,
  132. getDataSource,
  133. handleEditChange,
  134. };
  135. },
  136. });
  137. </script>