Files
guofu-admin/src/components/SelectNavigsation/components/select-data.vue
2024-08-27 07:22:22 +08:00

194 lines
5.0 KiB
Vue

<template>
<ele-modal
:width="750"
:visible="visible"
:maskClosable="false"
:title="title"
:footer="null"
:body-style="{ paddingBottom: '28px' }"
@update:visible="updateVisible"
>
<ele-pro-table
ref="tableRef"
row-key="navigationId"
:columns="columns"
:datasource="datasource"
:parse-data="parseData"
:need-page="false"
:customRow="customRow"
:expand-icon-column-index="1"
:expanded-row-keys="expandedRowKeys"
cache-key="proNavigationTable"
@done="onDone"
@expand="onExpand"
>
<template #toolbar>
<a-space>
<a-button type="dashed" class="ele-btn-icon" @click="expandAll">
展开
</a-button>
<a-button type="dashed" class="ele-btn-icon" @click="foldAll">
折叠
</a-button>
<a-divider type="vertical" />
<a-radio-group v-model:value="position" @change="reload">
<a-radio-button :value="1">顶部</a-radio-button>
<a-radio-button :value="2">底部</a-radio-button>
</a-radio-group>
<a-divider type="vertical" />
<!-- 搜索表单 -->
<a-input-search
allow-clear
v-model:value="searchText"
placeholder="请输入搜索关键词"
@search="reload"
@pressEnter="reload"
/>
</a-space>
</template>
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'action'">
<template v-if="pageId == record.id">
<a-radio v-model:checked="checked" @click="onRadio(record)" />
</template>
<template v-else>
<lebal>
<a-radio @click="onRadio(record)" />
<span class="ele-text-secondary">选择</span>
</lebal>
</template>
</template>
</template>
</ele-pro-table>
</ele-modal>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import {
ColumnItem,
DatasourceFunction
} from 'ele-admin-pro/es/ele-pro-table/types';
import { listNavigation, pageNavigation } from '@/api/cms/navigation';
import { EleProTable } from 'ele-admin-pro';
import { toTreeData } from 'ele-admin-pro/es';
import type { Navigation, NavigationParam } from '@/api/cms/navigation/model';
const props = defineProps<{
// 弹窗是否打开
visible: boolean;
// 标题
title?: string;
// 修改回显的数据
data?: Navigation | null;
}>();
const emit = defineEmits<{
(e: 'done', data: Navigation): void;
(e: 'update:visible', visible: boolean): void;
}>();
/* 更新visible */
const updateVisible = (value: boolean) => {
emit('update:visible', value);
};
// 搜索内容
const pageId = ref<number>(0);
const checked = ref<boolean>(true);
// 表格展开的行
const expandedRowKeys = ref<number[]>([]);
const searchText = ref('');
const position = ref(1);
// 表格实例
const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);
// 表格配置
const columns = ref<ColumnItem[]>([
{
title: 'ID',
dataIndex: 'navigationId',
width: 80
},
{
title: '栏目名称',
dataIndex: 'title',
key: 'title'
}
]);
// 表格数据源
const datasource: DatasourceFunction = ({ where }) => {
where = {};
where.title = searchText.value;
// where.position = position.value;
where.top = 0;
where.bottom = undefined;
if (position.value == 1) {
where.top = 0;
where.bottom = undefined;
}
if (position.value == 2) {
where.top = undefined;
where.bottom = 0;
}
where.isMpWeixin = false;
return listNavigation({ ...where });
};
/* 数据转为树形结构 */
const parseData = (data: Navigation[]) => {
console.log(data);
return toTreeData({
data: data.map((d) => {
return { ...d, key: d.navigationId, value: d.navigationId };
}),
idField: 'navigationId',
parentIdField: 'parentId'
});
};
/* 点击展开图标时触发 */
const onExpand = (expanded: boolean, record: Navigation) => {
if (expanded) {
expandedRowKeys.value = [
...expandedRowKeys.value,
record.navigationId as number
];
} else {
expandedRowKeys.value = expandedRowKeys.value.filter(
(d) => d !== record.navigationId
);
}
};
/* 搜索 */
const reload = (where?: NavigationParam) => {
tableRef?.value?.reload({ page: 1, where });
};
const onRadio = (record: Navigation) => {
pageId.value = Number(record.navigationId);
updateVisible(false);
emit('done', record);
};
/* 自定义行属性 */
const customRow = (record: Navigation) => {
return {
// 行点击事件
// onClick: () => {
// updateVisible(false);
// emit('done', record);
// },
// 行双击事件
onDblclick: () => {
updateVisible(false);
emit('done', record);
}
};
};
</script>
<style lang="less"></style>