Skip to content

Commit 262f421

Browse files
feat: Add example-dialog demo
1 parent 41533c1 commit 262f421

File tree

16 files changed

+311
-24
lines changed

16 files changed

+311
-24
lines changed

mock/table/index.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,46 @@ export default [
9292
}
9393
}
9494
}
95+
},
96+
// 详情接口
97+
{
98+
url: '/example/detail',
99+
method: 'get',
100+
response: ({ query }) => {
101+
const { id } = query
102+
for (const example of List) {
103+
if (example.id === id) {
104+
return {
105+
code: result_code,
106+
data: example
107+
}
108+
}
109+
}
110+
}
111+
},
112+
// 删除接口
113+
{
114+
url: '/example/delete',
115+
method: 'post',
116+
response: ({ body }) => {
117+
const ids = body.ids
118+
if (!ids) {
119+
return {
120+
code: '500',
121+
message: '请选择需要删除的数据'
122+
}
123+
} else {
124+
let i = List.length
125+
while (i--) {
126+
if (ids.indexOf(List[i].id) !== -1) {
127+
List.splice(i, 1)
128+
}
129+
}
130+
return {
131+
code: result_code,
132+
data: 'success'
133+
}
134+
}
135+
}
95136
}
96137
] as MockMethod[]

public/favicon.ico

0 Bytes
Binary file not shown.

public/logo.png

-224 KB
Loading

src/api/table/index.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@ export const getTableListApi = ({ params }: AxiosConfig) => {
1010
}>({ url: '/example/list', method: 'get', params })
1111
}
1212

13-
export const saveTableApi = ({ data }: AxiosConfig) => {
13+
export const saveTableApi = ({ data }: AxiosConfig<Recordable, TableData>) => {
1414
return request({ url: '/example/save', method: 'post', data })
1515
}
16+
17+
export const getTableDetApi = ({
18+
params
19+
}: AxiosConfig<
20+
{
21+
id: string
22+
},
23+
Recordable
24+
>) => {
25+
return request<TableData>({ url: '/example/detail', method: 'get', params })
26+
}
27+
28+
export const delTableListApi = ({
29+
data
30+
}: AxiosConfig<
31+
Recordable,
32+
{
33+
id: string[] | number[]
34+
}
35+
>) => {
36+
return request({ url: '/example/delete', method: 'post', data })
37+
}

src/assets/imgs/avatar.png

-4 KB
Binary file not shown.

src/assets/imgs/logo.png

-224 KB
Loading

src/components/Descriptions/src/Descriptions.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ const toggleClick = () => {
100100
</template>
101101

102102
<template #default>
103-
<slot :name="item.field">{{ data[item.field] }}</slot>
103+
<slot :name="item.field" :row="data">{{ data[item.field] }}</slot>
104104
</template>
105105
</ElDescriptionsItem>
106106
</ElDescriptions>

src/components/Table/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Table from './src/Table.vue'
33
export interface TableExpose {
44
setProps: (props: Recordable) => void
55
setColumn: (columnProps: TableSetPropsType[]) => void
6+
selections: Recordable[]
67
}
78

89
export { Table }

src/components/Table/src/Table.vue

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,16 @@ export default defineComponent({
8989
}
9090
}
9191
92+
const selections = ref<Recordable[]>([])
93+
94+
const selectionChange = (selection: Recordable[]) => {
95+
selections.value = selection
96+
}
97+
9298
expose({
9399
setProps,
94-
setColumn
100+
setColumn,
101+
selections
95102
})
96103
97104
const pagination = computed(() => {
@@ -226,7 +233,7 @@ export default defineComponent({
226233
align={v.align || align}
227234
headerAlign={v.headerAlign || headerAlign}
228235
label={v.label}
229-
width="100px"
236+
width="65px"
230237
></ElTableColumn>
231238
)
232239
} else {
@@ -264,6 +271,7 @@ export default defineComponent({
264271
// @ts-ignore
265272
ref={elTableRef}
266273
data={unref(getProps).data}
274+
onSelection-change={selectionChange}
267275
{...getBindValue}
268276
>
269277
{{

src/hooks/web/useTable.ts

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { Table, TableExpose } from '@/components/Table'
2-
import { ElTable } from 'element-plus'
2+
import { ElTable, ElMessageBox, ElMessage } from 'element-plus'
33
import { ref, reactive, watch, computed, unref, nextTick } from 'vue'
44
import { AxiosPromise } from 'axios'
55
import { get, assign } from 'lodash-es'
66
import type { TableProps } from '@/components/Table/src/types'
7+
import { useI18n } from '@/hooks/web/useI18n'
8+
9+
const { t } = useI18n()
710

811
interface UseTableConfig<T, L> {
912
getListApi: (option: L) => AxiosPromise<T>
13+
delListApi?: (option: AxiosConfig) => AxiosPromise<unknown>
1014
// 返回数据格式配置
1115
response: {
1216
list: string
@@ -22,7 +26,6 @@ interface TableObject<K, L> {
2226
tableList: K[]
2327
parmasObj: L
2428
loading: boolean
25-
dialogVisible: boolean
2629
currentRow: Nullable<K>
2730
}
2831

@@ -42,8 +45,6 @@ export const useTable = <T, K, L extends AxiosConfig = AxiosConfig>(
4245
parmasObj: {} as L,
4346
// 加载中
4447
loading: true,
45-
// 弹窗
46-
dialogVisible: false,
4748
// 当前行的数据
4849
currentRow: null
4950
})
@@ -95,11 +96,31 @@ export const useTable = <T, K, L extends AxiosConfig = AxiosConfig>(
9596
return table
9697
}
9798

99+
const delData = async (paramsObj: AxiosConfig, ids: string[] | number[]) => {
100+
const res = await (config?.delListApi && config?.delListApi(paramsObj))
101+
if (res) {
102+
ElMessage.success(t('common.delSuccess'))
103+
104+
// 计算出临界点
105+
const currentPage =
106+
tableObject.total % tableObject.pageSize === ids.length || tableObject.pageSize === 1
107+
? tableObject.currentPage > 1
108+
? tableObject.currentPage - 1
109+
: tableObject.currentPage
110+
: tableObject.currentPage
111+
112+
tableObject.currentPage = currentPage
113+
methods.getList()
114+
}
115+
}
116+
98117
const methods: {
99118
setProps: (props: Recordable) => void
100119
getList: () => Promise<void>
101120
setColumn: (columnProps: TableSetPropsType[]) => void
102121
setSearchParmas: (data: Recordable) => void
122+
getSelections: () => Promise<K[]>
123+
delList: (ids: string[] | number[], multiple: boolean, message?: boolean) => Promise<void>
103124
} = {
104125
getList: async () => {
105126
tableObject.loading = true
@@ -122,6 +143,10 @@ export const useTable = <T, K, L extends AxiosConfig = AxiosConfig>(
122143
const table = await getTable()
123144
table?.setColumn(columnProps)
124145
},
146+
getSelections: async () => {
147+
const table = await getTable()
148+
return (table?.selections || []) as K[]
149+
},
125150
// 与Search组件结合
126151
setSearchParmas: (data: Recordable) => {
127152
tableObject.currentPage = 1
@@ -133,6 +158,39 @@ export const useTable = <T, K, L extends AxiosConfig = AxiosConfig>(
133158
}
134159
})
135160
methods.getList()
161+
},
162+
// 删除数据
163+
delList: async (ids: string[] | number[], multiple: boolean, message = true) => {
164+
const tableRef = await getTable()
165+
if (multiple) {
166+
if (!tableRef?.selections.length) {
167+
ElMessage.warning(t('common.delNoData'))
168+
return
169+
}
170+
} else {
171+
if (!tableObject.currentRow) {
172+
ElMessage.warning(t('common.delNoData'))
173+
return
174+
}
175+
}
176+
const paramsObj: AxiosConfig = {
177+
data: {
178+
ids
179+
}
180+
}
181+
if (message) {
182+
ElMessageBox.confirm(t('common.delMessage'), t('common.delWarning'), {
183+
confirmButtonText: t('common.delOk'),
184+
cancelButtonText: t('common.delCancel'),
185+
type: 'warning'
186+
})
187+
.then(async () => {
188+
await delData(paramsObj, ids)
189+
})
190+
.catch(() => {})
191+
} else {
192+
await delData(paramsObj, ids)
193+
}
136194
}
137195
}
138196

src/locales/en.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ export default {
3333
query: 'Query',
3434
reset: 'Reset',
3535
shrink: 'Put away',
36-
expand: 'Expand'
36+
expand: 'Expand',
37+
delMessage: 'Delete the selected data?',
38+
delWarning: 'Warning',
39+
delOk: 'OK',
40+
delCancel: 'Cancel',
41+
delNoData: 'Please select the data to delete',
42+
delSuccess: 'Deleted successfully'
3743
},
3844
setting: {
3945
projectSetting: 'Project setting',
@@ -374,6 +380,7 @@ export default {
374380
pageviews: 'Pageviews',
375381
important: 'Important',
376382
content: 'Content',
377-
save: 'Save'
383+
save: 'Save',
384+
detail: 'Detail'
378385
}
379386
}

src/locales/zh-CN.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ export default {
3333
query: '查询',
3434
reset: '重置',
3535
shrink: '收起',
36-
expand: '展开'
36+
expand: '展开',
37+
delMessage: '是否删除所选中数据?',
38+
delWarning: '提示',
39+
delOk: '确定',
40+
delCancel: '取消',
41+
delNoData: '请选择需要删除的数据',
42+
delSuccess: '删除成功'
3743
},
3844
setting: {
3945
projectSetting: '项目配置',
@@ -371,6 +377,7 @@ export default {
371377
pageviews: '阅读数',
372378
important: '重要',
373379
content: '内容',
374-
save: '保存'
380+
save: '保存',
381+
detail: '详情'
375382
}
376383
}

src/store/modules/app.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { defineStore } from 'pinia'
22
import { store } from '../index'
3+
import { useCache } from '@/hooks/web/useCache'
34
import { appModules } from '@/config/app'
45
import type { AppState, LayoutType, ThemeTypes } from '@/config/app'
56
import { setCssVar, humpToUnderline } from '@/utils'
67
import { ElMessage } from 'element-plus'
78

9+
const { wsCache } = useCache()
10+
811
export const useAppStore = defineStore({
912
id: 'app',
1013
state: (): AppState => appModules,
@@ -119,6 +122,7 @@ export const useAppStore = defineStore({
119122
return
120123
}
121124
this.layout = layout
125+
wsCache.set('layout', this.layout)
122126
},
123127
setTitle(title: string) {
124128
this.title = title
@@ -132,15 +136,18 @@ export const useAppStore = defineStore({
132136
document.documentElement.classList.add('light')
133137
document.documentElement.classList.remove('dark')
134138
}
139+
wsCache.set('isDark', this.isDark)
135140
},
136141
setCurrentSize(currentSize: ElememtPlusSzie) {
137142
this.currentSize = currentSize
143+
wsCache.set('currentSize', this.currentSize)
138144
},
139145
setMobile(mobile: boolean) {
140146
this.mobile = mobile
141147
},
142148
setTheme(theme: ThemeTypes) {
143149
this.theme = Object.assign(this.theme, theme)
150+
wsCache.set('theme', this.theme)
144151
},
145152
setCssVarTheme() {
146153
for (const key in this.theme) {

src/store/modules/locale.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { defineStore } from 'pinia'
22
import { store } from '../index'
3+
import { useCache } from '@/hooks/web/useCache'
34
import { localeModules, elLocaleMap } from '@/config/locale'
45
import type { LocaleState } from '@/config/locale'
56

7+
const { wsCache } = useCache()
8+
69
export const useLocaleStore = defineStore({
710
id: 'locales',
811
state: (): LocaleState => localeModules,
@@ -22,6 +25,7 @@ export const useLocaleStore = defineStore({
2225
// this.locale = Object.assign(this.locale, localeMap)
2326
this.currentLocale.lang = localeMap?.lang
2427
this.currentLocale.elLocale = elLocaleMap[localeMap?.lang]
28+
wsCache.set('lang', localeMap?.lang)
2529
}
2630
}
2731
})

0 commit comments

Comments
 (0)