index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <template>
  2. <div>
  3. <BasicTable
  4. ref="tableRef"
  5. @register="registerTable"
  6. rowKey="id"
  7. :pagination="{ pageSize: 2, defaultPageSize: 2, showSizeChanger: false }"
  8. >
  9. <template #toolbar>
  10. <a-button @click="openModal"> 导出 </a-button>
  11. <a-button type="danger"> 删除 </a-button>
  12. </template>
  13. <template #action="{ record, column }">
  14. <TableAction :actions="createActions(record, column)" />
  15. </template>
  16. </BasicTable>
  17. <ExpExcelModel @register="register" @success="defaultHeader" />
  18. <Popup @register="PopupRegister" :popupData="popupData" />
  19. </div>
  20. </template>
  21. <script lang="ts">
  22. import { defineComponent, reactive, ref, unref } from 'vue';
  23. import Popup from './popup.vue';
  24. import { getUserList, deleteUser } from '/@/api/sys/user';
  25. import { useModal } from '/@/components/Modal';
  26. import { jsonToSheetXlsx, ExpExcelModel, ExportModalResult } from '/@/components/Excel';
  27. import { columns } from './data';
  28. import {
  29. BasicTable,
  30. useTable,
  31. TableAction,
  32. BasicColumn,
  33. ActionItem,
  34. EditRecordRow,
  35. TableActionType,
  36. } from '/@/components/Table';
  37. // interface popupData {
  38. // title: string,
  39. // treeData: object[],
  40. // checkedKeys: string[] | number[],
  41. // selectedKeys: string[] | number[],
  42. // expandedKeys: string[] | number[]
  43. // }
  44. const formData = [
  45. {
  46. id: 1,
  47. username: 'test1',
  48. title: '日志1',
  49. url: 'http://localhost:3100/#/permission/admin',
  50. ip: '0.0.0.0',
  51. browser: 'Mozilla/5.0',
  52. time: '2020-10-20',
  53. },
  54. {
  55. id: 2,
  56. username: 'test2',
  57. title: '日志2',
  58. url: 'http://localhost:3100/#/permission/admin',
  59. ip: '0.0.0.0',
  60. browser: 'Mozilla/5.0',
  61. time: '2020-10-21',
  62. },
  63. ];
  64. export default defineComponent({
  65. components: { BasicTable, TableAction, Popup, ExpExcelModel },
  66. setup() {
  67. const tableRef = ref<Nullable<TableActionType>>(null);
  68. const currentEditKeyRef = ref('');
  69. const popupData = reactive({
  70. title: '详情',
  71. });
  72. const [register, { openModal }] = useModal();
  73. const [registerTable] = useTable({
  74. title: '管理员日志',
  75. titleHelpMessage: '管理员可以查看自己所拥有的权限的管理员日志',
  76. rowSelection: { type: 'checkbox' },
  77. columns: columns,
  78. clickToRowSelect: false, // 点击行不勾选
  79. // api: getUserList,
  80. dataSource: formData,
  81. actionColumn: {
  82. width: 160,
  83. title: '操作',
  84. dataIndex: 'action',
  85. slots: { customRender: 'action' },
  86. fixed: undefined,
  87. },
  88. showIndexColumn: false,
  89. bordered: true,
  90. });
  91. const [PopupRegister, { openModal: openAdd }] = useModal();
  92. function getTableAction() {
  93. // 获取组件
  94. const tableAction = unref(tableRef);
  95. if (!tableAction) {
  96. throw new Error('tableAction is null');
  97. }
  98. return tableAction;
  99. }
  100. function getSelectRowList() {
  101. // 获取选中行
  102. console.log(getTableAction().getSelectRows());
  103. }
  104. function getSelectRowKeyList() {
  105. // 获取选中行的key --- id
  106. console.log(getTableAction().getSelectRowKeys());
  107. }
  108. function handleDetail(record: EditRecordRow) {
  109. currentEditKeyRef.value = record.id; // record.key
  110. const data = getTableAction().getDataSource();
  111. data.map((item) => {
  112. if (item.id === record.id) {
  113. record = item;
  114. }
  115. });
  116. openAdd(true, record);
  117. }
  118. async function handleDelete(record: Recordable) {
  119. console.log('点击了删除', record.id);
  120. console.log(record);
  121. await deleteUser({ id: record.id }).then((res) => {
  122. console.log(res);
  123. getTableAction().reload();
  124. });
  125. // const data = getTableAction().getDataSource()
  126. // console.log(data)
  127. // getTableAction().setTableData(data.filter(item => item.id !== record.id))
  128. }
  129. // 导出
  130. function defaultHeader({ filename, bookType }: ExportModalResult) {
  131. // 默认Object.keys(data[0])作为header
  132. jsonToSheetXlsx({
  133. data: formData,
  134. filename,
  135. write2excelOpts: {
  136. bookType,
  137. },
  138. });
  139. }
  140. function createActions(record: EditRecordRow, column: BasicColumn): ActionItem[] {
  141. if (false) {
  142. console.log(column);
  143. }
  144. return [
  145. {
  146. label: '详情',
  147. icon: 'akar-icons:eye-open',
  148. color: 'success',
  149. onClick: handleDetail.bind(null, record),
  150. },
  151. {
  152. label: '删除',
  153. color: 'error',
  154. icon: 'ic:outline-delete-outline',
  155. popConfirm: {
  156. title: '是否确认删除',
  157. confirm: handleDelete.bind(null, record),
  158. },
  159. },
  160. ];
  161. }
  162. return {
  163. defaultHeader,
  164. popupData,
  165. tableRef,
  166. registerTable,
  167. handleDetail,
  168. createActions,
  169. getTableAction,
  170. getSelectRowList,
  171. getSelectRowKeyList,
  172. PopupRegister,
  173. getUserList,
  174. register,
  175. openModal,
  176. };
  177. },
  178. });
  179. </script>