Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: button permissions in the menu to support editing. #484

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion mock/menu/index.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ export default [
title: '分析页',
permissionList: [
{
id: 1,
label: '新增',
value: 'add'
},
{
id: 2,
label: '编辑',
value: 'edit'
}
Expand All @@ -66,14 +68,17 @@ export default [
title: '工作台',
permissionList: [
{
id: 1,
label: '新增',
value: 'add'
},
{
id: 2,
label: '编辑',
value: 'edit'
},
{
id: 3,
label: '删除',
value: 'delete'
}
Expand Down Expand Up @@ -229,18 +234,22 @@ export default [
title: '综合示例-弹窗',
permissionList: [
{
id: 1,
label: '新增',
value: 'add'
},
{
id: 2,
label: '编辑',
value: 'edit'
},
{
id: 3,
label: '删除',
value: 'delete'
},
{
id: 4,
label: '查看',
value: 'view'
}
Expand All @@ -260,18 +269,22 @@ export default [
title: '综合示例-页面',
permissionList: [
{
id: 1,
label: '新增',
value: 'edit'
value: 'add'
},
{
id: 2,
label: '编辑',
value: 'edit'
},
{
id: 3,
label: '删除',
value: 'delete'
},
{
id: 4,
label: '查看',
value: 'view'
}
Expand Down
91 changes: 82 additions & 9 deletions src/views/Authorization/Menu/components/Write.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { PropType, reactive, watch, ref, unref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { useI18n } from '@/hooks/web/useI18n'
import { getMenuListApi } from '@/api/menu'
import { ElTag } from 'element-plus'
import { ElButton, ElInput, ElPopconfirm, ElTable, ElTableColumn, ElTag } from 'element-plus'
import AddButtonPermission from './AddButtonPermission.vue'
import { BaseButton } from '@/components/Button'
import { cloneDeep } from 'lodash-es'
Expand All @@ -29,7 +29,23 @@ const handleClose = async (tag: any) => {
})
}

const handleEdit = async (row: any) => {
// 深拷贝当前行数据到编辑行
permissionEditingRow.value = { ...row }
}

const handleSave = async () => {
const formData = await getFormData()
const index = formData?.permissionList?.findIndex((x) => x.id === permissionEditingRow.value.id)
if (index !== -1) {
formData.permissionList[index] = { ...permissionEditingRow.value }
permissionEditingRow.value = null // 重置编辑状态
}
}

const showDrawer = ref(false)
// 存储正在编辑的行的数据
const permissionEditingRow = ref<any>(null)

const formSchema = reactive<FormSchema[]>([
{
Expand Down Expand Up @@ -196,16 +212,73 @@ const formSchema = reactive<FormSchema[]>([
slots: {
default: (data: any) => (
<>
{data?.permissionList?.map((v) => {
return (
<ElTag class="mr-1" key={v.value} closable onClose={() => handleClose(v)}>
{v.label}
</ElTag>
)
})}
<BaseButton type="primary" size="small" onClick={() => (showDrawer.value = true)}>
<BaseButton
class="m-t-5px"
type="primary"
size="small"
onClick={() => (showDrawer.value = true)}
>
添加权限
</BaseButton>
<ElTable data={data?.permissionList}>
<ElTableColumn type="index" prop="id" />
<ElTableColumn
prop="value"
label="Value"
v-slots={{
default: ({ row }: any) =>
permissionEditingRow.value && permissionEditingRow.value.id === row.id ? (
<ElInput v-model={permissionEditingRow.value.value} size="small" />
) : (
<span>{row.value}</span>
)
}}
/>
<ElTableColumn
prop="label"
label="Label"
v-slots={{
default: ({ row }: any) =>
permissionEditingRow.value && permissionEditingRow.value.id === row.id ? (
<ElInput v-model={permissionEditingRow.value.label} size="small" />
) : (
<ElTag class="mr-1" key={row.value}>
{row.label}
</ElTag>
)
}}
/>
<ElTableColumn
label="Operations"
width="180"
v-slots={{
default: ({ row }: any) =>
permissionEditingRow.value && permissionEditingRow.value.id === row.id ? (
<ElButton size="small" type="primary" onClick={handleSave}>
确定
</ElButton>
) : (
<>
<ElButton size="small" type="primary" onClick={() => handleEdit(row)}>
编辑
</ElButton>
<ElPopconfirm
title="Are you sure to delete this?"
onConfirm={() => handleClose(row)}
>
{{
reference: () => (
<ElButton size="small" type="danger">
删除
</ElButton>
)
}}
</ElPopconfirm>
</>
)
}}
/>
</ElTable>
</>
)
}
Expand Down
Loading