index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <template>
  2. <div>
  3. <BasicTable ref="tableRef" @register="registerTable" rowKey="id">
  4. <template #toolbar>
  5. <a-button type="primary" @click="addRole"> 添加 </a-button>
  6. <a-button type="danger"> 删除 </a-button>
  7. </template>
  8. <template #action="{ record, column }">
  9. <TableAction :actions="createActions(record, column)" />
  10. </template>
  11. </BasicTable>
  12. <Popup @register="addRegister" :popupData="popupData" @saveData="saveData" />
  13. </div>
  14. </template>
  15. <script lang="ts">
  16. import { defineComponent, reactive, ref, unref } from 'vue';
  17. import Popup from './popup.vue';
  18. import { useModal } from '/@/components/Modal';
  19. // import { getAllMenuList } from '/@/api/sys/menu';
  20. import { columns } from './data';
  21. import { getGroupTree, addGroup, editGroup, deleteGroup } from '/@/api/sys/user';
  22. import {
  23. BasicTable,
  24. useTable,
  25. TableAction,
  26. BasicColumn,
  27. ActionItem,
  28. EditRecordRow,
  29. TableActionType,
  30. } from '/@/components/Table';
  31. interface PopupData {
  32. title: string;
  33. treeData: object[];
  34. }
  35. export default defineComponent({
  36. components: { BasicTable, TableAction, Popup },
  37. setup() {
  38. const tableRef = ref<Nullable<TableActionType>>(null);
  39. const currentEditKeyRef = ref('');
  40. const popupData = reactive<PopupData>({
  41. title: '添加',
  42. treeData: [{}],
  43. });
  44. const [registerTable] = useTable({
  45. title: '角色列表',
  46. titleHelpMessage:
  47. '角色组可以有多个,角色有上下级层级关系,如果子角色有角色组和管理员的权限则可以派生属于自己组别的下级角色组或管理员',
  48. rowSelection: { type: 'checkbox' },
  49. columns: columns,
  50. clickToRowSelect: false, // 点击行不勾选
  51. api: getGroupTree,
  52. afterFetch: afterFetch,
  53. // dataSource: formData,
  54. actionColumn: {
  55. width: 160,
  56. title: '操作',
  57. dataIndex: 'action',
  58. slots: { customRender: 'action' },
  59. fixed: undefined,
  60. },
  61. showIndexColumn: false,
  62. bordered: true,
  63. });
  64. const [addRegister, { openModal: openAdd }] = useModal();
  65. function afterFetch(result) {
  66. console.log(`result`, result);
  67. popupData.treeData = result;
  68. }
  69. function getTableAction() {
  70. // 获取组件
  71. const tableAction = unref(tableRef);
  72. if (!tableAction) {
  73. throw new Error('tableAction is null');
  74. }
  75. return tableAction;
  76. }
  77. function getSelectRowList() {
  78. // 获取选中行
  79. console.log(getTableAction().getSelectRows());
  80. }
  81. function getSelectRowKeyList() {
  82. // 获取选中行的key --- id
  83. console.log(getTableAction().getSelectRowKeys());
  84. }
  85. function addRole() {
  86. console.log('添加');
  87. popupData.title = '添加';
  88. openAdd(true, {});
  89. }
  90. function handleEdit(record: EditRecordRow) {
  91. currentEditKeyRef.value = record.id; // record.key
  92. console.log(record);
  93. popupData.title = '编辑';
  94. const data = getTableAction().getDataSource();
  95. data.map((item) => {
  96. if (item.id === record.id) {
  97. record = item;
  98. }
  99. if (record.pid === item.id) {
  100. record.pname = item.name;
  101. } else {
  102. record.pname = null;
  103. }
  104. });
  105. openAdd(true, record);
  106. }
  107. async function handleDelete(record: Recordable) {
  108. console.log('点击了删除', record.id);
  109. console.log(record);
  110. await deleteGroup({ id: record.id }).then((res) => {
  111. console.log(res);
  112. getTableAction().reload();
  113. });
  114. }
  115. async function saveData(params: any) {
  116. console.log('------------save---------');
  117. const data = params.data;
  118. const closeModel = params.closeModal;
  119. if (!data.id) {
  120. await addGroup(data).then((res) => {
  121. console.log(res);
  122. getTableAction().reload();
  123. closeModel();
  124. });
  125. console.log('----------add---');
  126. } else {
  127. await editGroup(data).then((res) => {
  128. console.log(res);
  129. getTableAction().reload();
  130. closeModel();
  131. });
  132. console.log('----------edit---');
  133. }
  134. }
  135. function createActions(record: EditRecordRow, column: BasicColumn): ActionItem[] {
  136. if (false) {
  137. console.log(column);
  138. }
  139. return [
  140. {
  141. label: '编辑',
  142. icon: 'ant-design:edit-outlined',
  143. color: 'warning',
  144. onClick: handleEdit.bind(null, record),
  145. },
  146. {
  147. label: '删除',
  148. color: 'error',
  149. icon: 'ic:outline-delete-outline',
  150. popConfirm: {
  151. title: '是否确认删除',
  152. confirm: handleDelete.bind(null, record),
  153. },
  154. },
  155. ];
  156. }
  157. return {
  158. popupData,
  159. tableRef,
  160. registerTable,
  161. addRole,
  162. handleEdit,
  163. createActions,
  164. getTableAction,
  165. getSelectRowList,
  166. getSelectRowKeyList,
  167. addRegister,
  168. saveData,
  169. };
  170. },
  171. });
  172. </script>