Commit 46899aa3cd6b1616c42ac263a28af75be839f6a0

Authored by 无木
1 parent 41854121

feat(demo): `switch` use in table

演示在table中渲染switch列并响应修改switch的动作
mock/demo/system.ts
@@ -153,6 +153,15 @@ export default [ @@ -153,6 +153,15 @@ export default [
153 }, 153 },
154 }, 154 },
155 { 155 {
  156 + url: '/basic-api/system/setRoleStatus',
  157 + timeout: 500,
  158 + method: 'post',
  159 + response: ({ query }) => {
  160 + const { id, status } = query;
  161 + return resultSuccess({ id, status });
  162 + },
  163 + },
  164 + {
156 url: '/basic-api/system/getAllRoleList', 165 url: '/basic-api/system/getAllRoleList',
157 timeout: 100, 166 timeout: 100,
158 method: 'get', 167 method: 'get',
src/api/demo/system.ts
@@ -15,6 +15,7 @@ import { defHttp } from '/@/utils/http/axios'; @@ -15,6 +15,7 @@ import { defHttp } from '/@/utils/http/axios';
15 enum Api { 15 enum Api {
16 AccountList = '/system/getAccountList', 16 AccountList = '/system/getAccountList',
17 DeptList = '/system/getDeptList', 17 DeptList = '/system/getDeptList',
  18 + setRoleStatus = '/system/setRoleStatus',
18 MenuList = '/system/getMenuList', 19 MenuList = '/system/getMenuList',
19 RolePageList = '/system/getRoleListByPage', 20 RolePageList = '/system/getRoleListByPage',
20 GetAllRoleList = '/system/getAllRoleList', 21 GetAllRoleList = '/system/getAllRoleList',
@@ -34,3 +35,6 @@ export const getRoleListByPage = (params?: RolePageParams) => @@ -34,3 +35,6 @@ export const getRoleListByPage = (params?: RolePageParams) =>
34 35
35 export const getAllRoleList = (params?: RoleParams) => 36 export const getAllRoleList = (params?: RoleParams) =>
36 defHttp.get<RoleListGetResultModel>({ url: Api.GetAllRoleList, params }); 37 defHttp.get<RoleListGetResultModel>({ url: Api.GetAllRoleList, params });
  38 +
  39 +export const setRoleStatus = (id: number, status: string) =>
  40 + defHttp.post({ url: Api.setRoleStatus, params: { id, status } });
src/views/demo/system/role/role.data.ts
1 import { BasicColumn } from '/@/components/Table'; 1 import { BasicColumn } from '/@/components/Table';
2 import { FormSchema } from '/@/components/Table'; 2 import { FormSchema } from '/@/components/Table';
3 import { h } from 'vue'; 3 import { h } from 'vue';
4 -import { Tag } from 'ant-design-vue'; 4 +import { Switch } from 'ant-design-vue';
  5 +import { setRoleStatus } from '/@/api/demo/system';
  6 +import { useMessage } from '/@/hooks/web/useMessage';
  7 +
5 export const columns: BasicColumn[] = [ 8 export const columns: BasicColumn[] = [
6 { 9 {
7 title: '角色名称', 10 title: '角色名称',
@@ -21,13 +24,33 @@ export const columns: BasicColumn[] = [ @@ -21,13 +24,33 @@ export const columns: BasicColumn[] = [
21 { 24 {
22 title: '状态', 25 title: '状态',
23 dataIndex: 'status', 26 dataIndex: 'status',
24 - width: 80, 27 + width: 120,
25 customRender: ({ record }) => { 28 customRender: ({ record }) => {
26 - const status = record.status;  
27 - const enable = ~~status === 0;  
28 - const color = enable ? 'green' : 'red';  
29 - const text = enable ? '启用' : '停用';  
30 - return h(Tag, { color: color }, () => text); 29 + if (!Reflect.has(record, 'pendingStatus')) {
  30 + record.pendingStatus = false;
  31 + }
  32 + return h(Switch, {
  33 + checked: record.status === '1',
  34 + checkedChildren: '已启用',
  35 + unCheckedChildren: '已禁用',
  36 + loading: record.pendingStatus,
  37 + onChange(checked: boolean) {
  38 + record.pendingStatus = true;
  39 + const newStatus = checked ? '1' : '0';
  40 + const { createMessage } = useMessage();
  41 + setRoleStatus(record.id, newStatus)
  42 + .then(() => {
  43 + record.status = newStatus;
  44 + createMessage.success(`已成功修改角色状态`);
  45 + })
  46 + .catch(() => {
  47 + createMessage.error('修改角色状态失败');
  48 + })
  49 + .finally(() => {
  50 + record.pendingStatus = false;
  51 + });
  52 + },
  53 + });
31 }, 54 },
32 }, 55 },
33 { 56 {