mirror of
https://github.com/vbenjs/vue-vben-admin.git
synced 2025-08-27 04:39:12 +08:00
feat(demo): switch
use in table
演示在table中渲染switch列并响应修改switch的动作
This commit is contained in:
@@ -152,6 +152,15 @@ export default [
|
|||||||
return resultPageSuccess(page, pageSize, roleList);
|
return resultPageSuccess(page, pageSize, roleList);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
url: '/basic-api/system/setRoleStatus',
|
||||||
|
timeout: 500,
|
||||||
|
method: 'post',
|
||||||
|
response: ({ query }) => {
|
||||||
|
const { id, status } = query;
|
||||||
|
return resultSuccess({ id, status });
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
url: '/basic-api/system/getAllRoleList',
|
url: '/basic-api/system/getAllRoleList',
|
||||||
timeout: 100,
|
timeout: 100,
|
||||||
|
@@ -15,6 +15,7 @@ import { defHttp } from '/@/utils/http/axios';
|
|||||||
enum Api {
|
enum Api {
|
||||||
AccountList = '/system/getAccountList',
|
AccountList = '/system/getAccountList',
|
||||||
DeptList = '/system/getDeptList',
|
DeptList = '/system/getDeptList',
|
||||||
|
setRoleStatus = '/system/setRoleStatus',
|
||||||
MenuList = '/system/getMenuList',
|
MenuList = '/system/getMenuList',
|
||||||
RolePageList = '/system/getRoleListByPage',
|
RolePageList = '/system/getRoleListByPage',
|
||||||
GetAllRoleList = '/system/getAllRoleList',
|
GetAllRoleList = '/system/getAllRoleList',
|
||||||
@@ -34,3 +35,6 @@ export const getRoleListByPage = (params?: RolePageParams) =>
|
|||||||
|
|
||||||
export const getAllRoleList = (params?: RoleParams) =>
|
export const getAllRoleList = (params?: RoleParams) =>
|
||||||
defHttp.get<RoleListGetResultModel>({ url: Api.GetAllRoleList, params });
|
defHttp.get<RoleListGetResultModel>({ url: Api.GetAllRoleList, params });
|
||||||
|
|
||||||
|
export const setRoleStatus = (id: number, status: string) =>
|
||||||
|
defHttp.post({ url: Api.setRoleStatus, params: { id, status } });
|
||||||
|
@@ -1,7 +1,10 @@
|
|||||||
import { BasicColumn } from '/@/components/Table';
|
import { BasicColumn } from '/@/components/Table';
|
||||||
import { FormSchema } from '/@/components/Table';
|
import { FormSchema } from '/@/components/Table';
|
||||||
import { h } from 'vue';
|
import { h } from 'vue';
|
||||||
import { Tag } from 'ant-design-vue';
|
import { Switch } from 'ant-design-vue';
|
||||||
|
import { setRoleStatus } from '/@/api/demo/system';
|
||||||
|
import { useMessage } from '/@/hooks/web/useMessage';
|
||||||
|
|
||||||
export const columns: BasicColumn[] = [
|
export const columns: BasicColumn[] = [
|
||||||
{
|
{
|
||||||
title: '角色名称',
|
title: '角色名称',
|
||||||
@@ -21,13 +24,33 @@ export const columns: BasicColumn[] = [
|
|||||||
{
|
{
|
||||||
title: '状态',
|
title: '状态',
|
||||||
dataIndex: 'status',
|
dataIndex: 'status',
|
||||||
width: 80,
|
width: 120,
|
||||||
customRender: ({ record }) => {
|
customRender: ({ record }) => {
|
||||||
const status = record.status;
|
if (!Reflect.has(record, 'pendingStatus')) {
|
||||||
const enable = ~~status === 0;
|
record.pendingStatus = false;
|
||||||
const color = enable ? 'green' : 'red';
|
}
|
||||||
const text = enable ? '启用' : '停用';
|
return h(Switch, {
|
||||||
return h(Tag, { color: color }, () => text);
|
checked: record.status === '1',
|
||||||
|
checkedChildren: '已启用',
|
||||||
|
unCheckedChildren: '已禁用',
|
||||||
|
loading: record.pendingStatus,
|
||||||
|
onChange(checked: boolean) {
|
||||||
|
record.pendingStatus = true;
|
||||||
|
const newStatus = checked ? '1' : '0';
|
||||||
|
const { createMessage } = useMessage();
|
||||||
|
setRoleStatus(record.id, newStatus)
|
||||||
|
.then(() => {
|
||||||
|
record.status = newStatus;
|
||||||
|
createMessage.success(`已成功修改角色状态`);
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
createMessage.error('修改角色状态失败');
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
record.pendingStatus = false;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user