TableMode.vue
9.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<template>
<div>
<BasicTable
class="devide-profiles"
@register="registerTable"
:rowSelection="{ type: 'checkbox' }"
:clickToRowSelect="false"
>
<template #toolbar>
<Authority :value="ProductPermission.CREATE">
<a-button type="primary" @click="handleCreate"> 新增产品 </a-button>
</Authority>
<Authority :value="ProductPermission.IMPORT">
<ImpExcel @success="loadDataSuccess" dateFormat="YYYY-MM-DD">
<a-button @click="handleImport"> 导入产品 </a-button>
</ImpExcel>
</Authority>
<Authority :value="ProductPermission.DELETE">
<Popconfirm
title="您确定要批量删除数据"
ok-text="确定"
cancel-text="取消"
@confirm="handleDeleteOrBatchDelete(null)"
>
<a-button type="primary" color="error" :disabled="hasBatchDelete"> 批量删除 </a-button>
</Popconfirm>
</Authority>
<ModeSwitchButton :value="$props.mode" @change="handleModeChange" />
</template>
<template #img="{ record }">
<TableImg
:size="30"
:showBadge="false"
:simpleShow="true"
:imgList="
typeof record.image !== 'undefined' && record.image !== '' && record.image != null
? [record.image]
: null
"
/>
</template>
<template #action="{ record }">
<TableAction
:actions="[
{
label: '详情',
auth: ProductPermission.DETAIL,
icon: 'ant-design:eye-outlined',
onClick: handleDetailView.bind(null, record),
},
{
label: '编辑',
auth: ProductPermission.UPDATE,
icon: 'clarity:note-edit-line',
onClick: handleEdit.bind(null, record),
ifShow: () => {
return record.name !== 'default' ? true : false;
},
},
]"
:drop-down-actions="[
{
label: '默认',
icon: 'ant-design:profile-outlined',
onClick: handleSetDefault.bind(null, record),
ifShow: () => {
return record.default === false;
},
},
{
label: '导出',
auth: ProductPermission.EXPORT,
icon: 'ant-design:login-outlined',
onClick: handleExport.bind(null, record),
},
{
label: '删除',
auth: ProductPermission.DELETE,
icon: 'ant-design:delete-outlined',
color: 'error',
popConfirm: {
title: '是否确认删除',
confirm: handleDeleteOrBatchDelete.bind(null, record),
},
ifShow: () => {
return record.default === false && record.name !== 'default';
},
},
]"
/>
</template>
</BasicTable>
<DeviceProfileModal @register="registerModal" @success="handleSuccess" />
<DeviceProfileDrawer @register="registerDrawer" />
<ExpExcelModal
ref="expExcelModalRef"
@register="registerExportModal"
@success="defaultHeader"
/>
</div>
</template>
<script lang="ts" setup>
import { ref, nextTick, onUnmounted } from 'vue';
import { BasicTable, TableImg, useTable, TableAction, BasicColumn } from '/@/components/Table';
import { columns, searchFormSchema, defaultObj, ProductPermission } from './device.profile.data';
import { useMessage } from '/@/hooks/web/useMessage';
import {
deviceConfigGetQuery,
deviceConfigDelete,
setDeviceProfileIsDefaultApi,
} from '/@/api/device/deviceConfigApi';
import { useModal } from '/@/components/Modal';
import { useDrawer } from '/@/components/Drawer';
import DeviceProfileModal from '/@/views/device/profiles/DeviceProfileModal.vue';
import { ImpExcel, ExcelData } from '/@/components/Excel';
import { jsonToSheetXlsx, ExpExcelModal, ExportModalResult } from '/@/components/Excel';
import { Authority } from '/@/components/Authority';
import { useBatchDelete } from '/@/hooks/web/useBatchDelete';
import { Popconfirm } from 'ant-design-vue';
import DeviceProfileDrawer from './DeviceProfileDrawer.vue';
import { EnumTableCardMode, ModeSwitchButton } from '/@/components/Widget';
defineProps<{
mode: EnumTableCardMode;
}>();
const emit = defineEmits(['changeMode']);
const exportData: any = ref([]);
const expExcelModalRef: any = ref(null);
const getPathUrl = ref('');
const getPathUrlName = ref('');
const disabled = ref(true);
const onCloseVal = ref(0);
const immediateStatus = ref(false);
const { createMessage } = useMessage();
const [registerModal, { openModal }] = useModal();
const [registerExportModal, { openModal: openModalExcel }] = useModal();
const [registerTable, { setProps, reload, setTableData, getForm }] = useTable({
title: '产品列表',
clickToRowSelect: false,
api: deviceConfigGetQuery,
immediate: immediateStatus.value,
columns,
formConfig: {
labelWidth: 120,
schemas: searchFormSchema,
},
rowKey: 'id',
useSearchForm: true,
showTableSetting: true,
bordered: true,
showIndexColumn: false,
actionColumn: {
width: 200,
title: '操作',
dataIndex: 'action',
slots: { customRender: 'action' },
fixed: 'right',
},
});
const { hasBatchDelete, handleDeleteOrBatchDelete, selectionOptions } = useBatchDelete(
deviceConfigDelete,
handleSuccess,
setProps
);
selectionOptions.rowSelection.getCheckboxProps = (record: Recordable) => {
// Demo:status为1的选择框禁用
if (record.default === true) {
return { disabled: true };
} else if (record.name == 'default') {
return { disabled: true };
} else {
return { disabled: false };
}
};
nextTick(() => {
setProps(selectionOptions);
});
/**
*@param url,name
**/
function getParam(url, name) {
try {
let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
let r = url.split('?')[1].match(reg);
if (r != null) {
return r[2];
}
return ''; //如果此处只写return;则返回的是undefined
} catch (e) {
return ''; //如果此处只写return;则返回的是undefined
}
}
getPathUrl.value = window.location.href;
const name = 'name';
const getName = getParam(getPathUrl.value, name);
getPathUrlName.value = decodeURIComponent(getName);
const setRowClassName = async () => {
if (getPathUrlName.value !== '') {
const { items } = await deviceConfigGetQuery({
page: 1,
pageSize: 10,
name: getPathUrlName.value,
});
nextTick(() => {
setTableData(items);
const { setFieldsValue, resetFields } = getForm();
setFieldsValue({
name: getPathUrlName.value,
});
if (onCloseVal.value == 1) {
resetFields();
}
});
} else {
setTimeout(() => {
reload();
}, 80);
}
};
setRowClassName();
onUnmounted(() => {
getPathUrlName.value = '';
onCloseVal.value = 1;
});
const tableListRef = ref<
{
title: string;
columns?: any[];
dataSource?: any[];
}[]
>([]);
function loadDataSuccess(excelDataList: ExcelData[]) {
tableListRef.value = [];
for (const excelData of excelDataList) {
const {
header,
results,
meta: { sheetName },
} = excelData;
const columns: BasicColumn[] = [];
for (const title of header) {
columns.push({ title, dataIndex: title });
}
tableListRef.value.push({ title: sheetName, dataSource: results, columns });
}
}
//新增
function handleCreate() {
openModal(true, {
isUpdate: false,
});
}
//编辑
function handleEdit(record: Recordable) {
openModal(true, {
record,
isUpdate: true,
});
}
const [registerDrawer, { openDrawer }] = useDrawer();
//详情
function handleDetailView(record: Recordable) {
openDrawer(true, { record });
}
function defaultHeader({ filename, bookType }: ExportModalResult) {
// 默认Object.keys(data[0])作为header
const data = exportData.value;
jsonToSheetXlsx({
data,
filename,
write2excelOpts: {
bookType,
},
});
}
//导出
const handleExport = (record: Recordable) => {
exportData.value = [];
exportData.value.push({
createTime: record.createTime,
description: record.description,
name: record.name,
});
nextTick(() => {
openModalExcel();
expExcelModalRef.value?.clearFieldFunc();
});
};
//导入
function handleImport() {}
function handleSuccess() {
reload();
}
const handleSetDefault = async (record: Recordable) => {
let id = record.tbProfileId;
const data = await setDeviceProfileIsDefaultApi(id, 'default', defaultObj);
if (!data) return createMessage.error('设置该产品为默认失败');
createMessage.success('设置该产品为默认成功');
reload();
disabled.value = true;
};
const handleModeChange = (value: EnumTableCardMode) => {
emit('changeMode', value);
};
</script>
<style lang="css">
.devide-profiles .rowcolor {
color: red;
}
.devide-profiles .rowcolor2 {
background: #a2c3e6;
}
</style>