index.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <template>
  2. <div class="p-4">
  3. <BasicTable
  4. ref="tableRef"
  5. @register="registerTable"
  6. rowKey="key"
  7. isTreeTable
  8. >
  9. <template #action="{ record, column }">
  10. <TableAction :actions="createActions(record, column)" />
  11. </template>
  12. <template #form-custom > custom-slot </template>
  13. <template #toolbar>
  14. <a-button type="primary" @click="addMenu" >
  15. {{ '新增菜单' }}
  16. </a-button>
  17. </template>
  18. </BasicTable>
  19. <Model @register="addRegister" @saveData="saveData" />
  20. </div>
  21. </template>
  22. <script lang="ts">
  23. import { defineComponent, ref, unref } from 'vue';
  24. import Model from './model.vue'
  25. import { useModal } from '/@/components/Modal';
  26. import { h } from 'vue';
  27. import { Tag } from 'ant-design-vue';
  28. import { Icon } from '/@/components/Icon';
  29. import { dataSource } from './data'
  30. import {
  31. BasicTable,
  32. useTable,
  33. TableAction,
  34. BasicColumn,
  35. ActionItem,
  36. EditRecordRow,
  37. TableActionType
  38. } from '/@/components/Table';
  39. const columns: BasicColumn[] = [
  40. {
  41. title: '菜单名称',
  42. dataIndex: 'menuName',
  43. width: 150,
  44. align: 'left',
  45. },
  46. {
  47. title: 'key',
  48. dataIndex: 'key',
  49. width: 70,
  50. },
  51. {
  52. title: '图标',
  53. dataIndex: 'icon',
  54. width: 50,
  55. customRender: ({ record }) => {
  56. return h(Icon, { icon: record.icon });
  57. },
  58. },
  59. {
  60. title: '创建时间',
  61. dataIndex: 'create_time',
  62. width: 150,
  63. },
  64. {
  65. title: '详情',
  66. dataIndex: 'detail',
  67. width: 200,
  68. },
  69. {
  70. title: '状态',
  71. dataIndex: 'status',
  72. width: 80,
  73. customRender: ({ record }) => {
  74. const status = record.status;
  75. const enable = ~~status === 0;
  76. const color = enable ? 'green' : 'red';
  77. const text = enable ? '启用' : '停用';
  78. return h(Tag, { color: color }, () => text);
  79. },
  80. },
  81. ];
  82. export default defineComponent({
  83. components: { BasicTable, TableAction, Model, },
  84. setup() {
  85. const tableRef = ref<Nullable<TableActionType>>(null);
  86. const currentEditKeyRef = ref('');
  87. const [registerTable] = useTable({
  88. title: "菜单列表",
  89. titleHelpMessage: "温馨提醒",
  90. columns: columns,
  91. dataSource: dataSource,
  92. bordered: true,
  93. showIndexColumn: false,
  94. actionColumn: {
  95. width: 160,
  96. title: '操作',
  97. dataIndex: 'action',
  98. slots: { customRender: 'action' },
  99. fixed: undefined,
  100. },
  101. });
  102. const [addRegister, { openModal: openAdd }] = useModal();
  103. const rowClick = (e: any) => {
  104. console.log(e)
  105. }
  106. function getTableAction() { // 获取组件
  107. const tableAction = unref(tableRef);
  108. if (!tableAction) {
  109. throw new Error('tableAction is null');
  110. }
  111. return tableAction;
  112. }
  113. function getSelectRowList() { // 获取选中行
  114. console.log(getTableAction().getSelectRows());
  115. }
  116. function getSelectRowKeyList() { // 获取选中行的key --- id
  117. console.log(getTableAction().getSelectRowKeys());
  118. }
  119. function addMenu() {
  120. openAdd(true, {
  121. id: 0,
  122. menuName: '',
  123. detail: '',
  124. });
  125. }
  126. function handleEdit(record: EditRecordRow) {
  127. currentEditKeyRef.value = record.id; // record.key
  128. console.log(record)
  129. // console.log(record.id)
  130. // const data = getTableAction().getDataSource()
  131. // data.map(item => {
  132. // if (item.id === record.id) {
  133. // record = item
  134. // }
  135. // })
  136. openAdd(true, {
  137. // id: record.id,
  138. // name: record.name,
  139. // password: record.password,
  140. // detail: record.detail,
  141. ...record,
  142. isUpdate: true,
  143. });
  144. }
  145. function handleDelete(record: Recordable) {
  146. console.log('点击了删除', record.id);
  147. console.log(record)
  148. const data = getTableAction().getDataSource()
  149. console.log(data)
  150. getTableAction().setTableData(data.filter(item => item.id !== record.id))
  151. }
  152. function saveData(data: any) {
  153. console.log('saveDate--------')
  154. if (!data.key) {
  155. console.log('添加菜单')
  156. console.log(data)
  157. return
  158. }
  159. const mapTree = org => {
  160. const haveChildren = Array.isArray(org.children) && org.children.length > 0;
  161. if (org.key === data.key) {
  162. org.menuName = data.menuName
  163. org.icon = data.icon
  164. org.status = data.status
  165. org.detail = data.detail
  166. return
  167. }
  168. if (haveChildren) {
  169. org.children.map(item => mapTree(item))
  170. }
  171. }
  172. const dataSource = getTableAction().getDataSource()
  173. if (Array.isArray(dataSource)) {
  174. dataSource.map(item => mapTree(item))
  175. }
  176. getTableAction().setTableData(dataSource)
  177. }
  178. function createActions(record: EditRecordRow, column: BasicColumn): ActionItem[] {
  179. column
  180. return [
  181. {
  182. label: '编辑',
  183. icon: 'ant-design:edit-outlined',
  184. color: 'warning',
  185. onClick: handleEdit.bind(null, record),
  186. },
  187. {
  188. label: '删除',
  189. color: 'error',
  190. icon: 'ic:outline-delete-outline',
  191. popConfirm: {
  192. title: '是否确认删除',
  193. confirm: handleDelete.bind(null, record),
  194. },
  195. },
  196. ];
  197. }
  198. return {
  199. tableRef,
  200. registerTable,
  201. rowClick,
  202. addMenu,
  203. handleEdit,
  204. createActions,
  205. getTableAction,
  206. getSelectRowList,
  207. getSelectRowKeyList,
  208. addRegister,
  209. saveData,
  210. };
  211. },
  212. });
  213. </script>