feat: 导出按钮支持仅导出勾选行模式
This commit is contained in:
@@ -106,7 +106,7 @@ export default defineComponent({
|
||||
}
|
||||
|
||||
// 导出
|
||||
async function toExport(columns: ClTable.Column[]) {
|
||||
async function toExport(columns: ClTable.Column[], mode: 'all' | 'selected' = 'all') {
|
||||
// 加载
|
||||
loading.value = true;
|
||||
|
||||
@@ -117,7 +117,38 @@ export default defineComponent({
|
||||
const header = await getHeader(columns, fields);
|
||||
|
||||
// 数据
|
||||
let data = await getData();
|
||||
let data: any[];
|
||||
|
||||
if (mode === 'selected') {
|
||||
// 导出勾选行 — 直接从 crud.selection 取数据
|
||||
data = Crud.value?.selection;
|
||||
|
||||
if (!data || data.length === 0) {
|
||||
loading.value = false;
|
||||
return ElMessage.warning('请先勾选要导出的行');
|
||||
}
|
||||
|
||||
// 应用 formatter 和 dict
|
||||
data = data.map((e, i) => {
|
||||
const row = { ...e };
|
||||
for (const k in row) {
|
||||
const col = props.columns?.find(c => c.prop == k);
|
||||
|
||||
if (col) {
|
||||
if (col.formatter) {
|
||||
row[k] = col.formatter(row, col, row[k], i);
|
||||
}
|
||||
|
||||
if (col.dict) {
|
||||
row[k] = deepFind(row[k], col.dict as any)?.label || row[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
return row;
|
||||
});
|
||||
} else {
|
||||
data = await getData();
|
||||
}
|
||||
|
||||
if (!data) {
|
||||
loading.value = false;
|
||||
@@ -165,9 +196,21 @@ export default defineComponent({
|
||||
labelPosition: 'top'
|
||||
},
|
||||
form: {
|
||||
mode: 'all',
|
||||
checked: columns.map(e => e.prop)
|
||||
},
|
||||
items: [
|
||||
{
|
||||
label: t('导出范围'),
|
||||
prop: 'mode',
|
||||
component: {
|
||||
name: 'el-radio-group',
|
||||
options: [
|
||||
{ label: '全部数据', value: 'all' },
|
||||
{ label: '仅导出勾选', value: 'selected' }
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
label: t('选择列'),
|
||||
prop: 'checked',
|
||||
@@ -184,11 +227,14 @@ export default defineComponent({
|
||||
],
|
||||
on: {
|
||||
submit(data, { close, done }) {
|
||||
if (isEmpty(data.checked)) {
|
||||
if (data.mode === 'selected' && (!Crud.value?.selection || Crud.value.selection.length === 0)) {
|
||||
ElMessage.warning('请先勾选要导出的行');
|
||||
done();
|
||||
} else if (isEmpty(data.checked)) {
|
||||
ElMessage.warning(t('请先选择要导出的列'));
|
||||
done();
|
||||
} else {
|
||||
toExport(columns.filter(e => data.checked.includes(e.prop)));
|
||||
toExport(columns.filter(e => data.checked.includes(e.prop)), data.mode);
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user