| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787 |
- const e = `<template>
- <div class="list-layout" ref="layout">
- <div class="tools">
- <div class="title">
- {{ title }}
- </div>
- <div class="btns">
- <a-space :size="spaceSize">
- <span class="item-btn" v-for="btn in renderButtons" :key="btn.key">
- <a-button
- class="btn"
- v-action="btn.action || btn.key"
- v-show="btn.show !== false"
- :disabled="!!btn.disabled"
- :type="btn.type || btn.key === 'add' ? 'primary' : 'default'"
- @click="btnCallback(btn)"
- >
- <template #icon>
- <component v-if="renderIcon(btn)" :is="renderIcon(btn)" />
- </template>
- {{ $l(btn.name) }}
- </a-button>
- </span>
- <slot name="otherBtns" :selections="selectedRowKeys" />
- </a-space>
- </div>
- </div>
- <div class="list-layout-main">
- <div
- class="side s-side-tree"
- ref="side"
- :style="{ width: sideWidth + 'px' }"
- v-if="$slots.tree"
- >
- <slot name="tree" />
- </div>
- <div class="side-bar s-theme-bg" v-if="$slots.tree" v-sidedrag />
- <div class="layout-list-content" v-if="!$slots.empty">
- <div class="list-filter-wrap" ref="filters" v-if="!!$slots.filter || !!$slots.filterMore">
- <div class="filter-content" v-if="$slots.filter">
- <div class="filter">
- <slot name="filter" />
- </div>
- <a class="filter-more-btn" v-if="$slots.filterMore" @click="toggleFilter">
- {{ !isMoreFilter ? $l('更多') : $l('收起') }}
- <double-right-outlined
- style="font-size: 12px; transform: rotate(90deg)"
- v-if="!isMoreFilter"
- />
- <double-right-outlined style="font-size: 12px; transform: rotate(-90deg)" v-else />
- </a>
- <div class="reset-wrap" v-if="$slots.filter">
- <a-tooltip
- placement="top"
- :overlayStyle="{ maxWidth: '500px' }"
- :mouseEnterDelay="1.5"
- :mouseLeaveDelay="0.5"
- >
- <template #title>
- <span>{{ $l('条件重置') }}</span>
- </template>
- <a-button style="padding-bottom: 2px" @click="filterReset">
- <template #icon>
- <sync-outlined />
- </template>
- </a-button>
- </a-tooltip>
- </div>
- </div>
- <div class="filter-more-content" v-if="$slots.filterMore" v-show="isMoreFilter">
- <slot name="filterMore" />
- </div>
- <div class="desc-wrap" v-if="$slots.desc">
- <slot name="desc" />
- </div>
- </div>
- <div class="table-content">
- <div class="tabs-wrap" v-if="$slots.tabs" ref="tabs">
- <slot name="tabs" />
- </div>
- <div class="table-container">
- <a-table
- ref="table"
- size="middle"
- :scroll="{ y: tableScrollPx }"
- :rowKey="rowKey"
- :tableLoading="tableLoading"
- :columns="renderColumns"
- :dataSource="tableData"
- v-model:expandedRowKeys="expandedRowKeys"
- :rowSelection="
- showSelection || isSelection
- ? {
- selectedRowKeys: selectedRowKeys,
- onChange: onSelectChange,
- getcheckboxProps: getCheckboxProps
- }
- : null
- "
- :pagination="showPagination ? pagination : false"
- @expand="handExpand"
- @change="handTableChange"
- >
- <!-- 自定义单元格:template 渲染同名插槽、isClick 渲染可点击、action 渲染操作列 -->
- <template #bodyCell="scope">
- <template v-if="scope.column">
- <!-- cell-template -->
- <slot
- v-if="isTemplateColumn(scope.column.dataIndex)"
- :name="scope.column.dataIndex"
- :text="scope.text"
- :record="scope.record"
- :index="scope.index"
- :item="getColumn(scope.column.dataIndex)"
- />
- <!-- cell-click -->
- <a
- v-else-if="isClickColumn(scope.column.dataIndex)"
- @click="tableColCallback(scope.column.dataIndex, scope.record)"
- >{{ getCellText(scope.text) }}</a
- >
- <!-- cell-action -->
- <slot
- v-else-if="scope.column.dataIndex === 'action'"
- name="action"
- :record="scope.record"
- />
- </template>
- </template>
- </a-table>
- </div>
- </div>
- </div>
- <div class="layout-list-content" v-else>
- <slot name="empty" />
- </div>
- </div>
- <div class="other">
- <slot name="other" />
- </div>
- <s-import-modal
- v-if="isShowImportModal"
- ref="importModalRef"
- :upload-url="importItem.importUploadUrl"
- v-model:value="importVisible"
- :columns="importItem.importColumns"
- :apis="importItem.importApis"
- @reload="reload"
- />
- </div>
- </template>
- <script setup>
- import { computed, getCurrentInstance, h, nextTick, onMounted, reactive, ref, watch } from 'vue'
- import { useRoute } from 'vue-router'
- import { createSideDrag } from '../../utils/side-drag'
- defineOptions({ name: 'SListTableTreeLayout' })
- const props = defineProps({
- // 页面标题
- title: {
- type: String,
- required: true
- },
- // 工具栏按钮配置(添加 / 批量删除 / 导入 / 导出)
- buttons: {
- type: Array,
- default: () => [
- {
- name: window.$l('添加'),
- key: 'add',
- show: true,
- type: 'primary',
- icon: 'plus-circle',
- api: null
- },
- {
- name: window.$l('批量删除'),
- key: 'remove',
- action: 'remove',
- show: true,
- icon: 'delete',
- api: null
- },
- {
- name: window.$l('导入'),
- key: 'import',
- show: true,
- icon: 'upload',
- importColumns: [], // 预览的表头信息
- importUploadUrl: null, // 上传url
- importApis: {
- templateDownload: null,
- importPreview: null,
- importData: null,
- exportExceptionData: null
- }
- },
- {
- name: window.$l('导出'),
- key: 'export',
- show: true,
- icon: 'download',
- api: null
- }
- ]
- },
- // 列表checkbox列
- isSelection: {
- type: Boolean,
- default: false
- },
- // 工具栏按钮是否显示图标
- showBtnIcon: {
- type: Boolean,
- default: true
- },
- // 是否显示分页
- showPagination: {
- type: Boolean,
- default: true
- },
- // 表格列配置
- columns: {
- type: Array,
- default: () => []
- },
- // 行主键字段名
- rowKey: {
- type: [Function, String],
- default: 'id'
- },
- // 表体 loading(外部控制)
- tableLoading: {
- type: Boolean,
- default: false
- },
- // 取数函数,返回 { list, total }
- loadData: {
- type: Function,
- required: true
- },
- // 透传 a-table,控制某行是否可勾选
- getCheckboxProps: {
- type: Function,
- default: (record = {}) => {
- return { props: record }
- }
- }
- })
- const emit = defineEmits(['reset', 'btnCallback', 'tableColCallback', 'handExpand'])
- const route = useRoute()
- const instance = getCurrentInstance()
- // ---------- 模板 ref ----------
- const layout = ref(null)
- const filters = ref(null)
- const tabs = ref(null)
- const table = ref(null)
- const spaceSize = ref(8)
- // ---------- 状态 ----------
- const importVisible = ref(false)
- // 表体滚动高度:仅作为 scroll.y 同步值(初值兜底),布局高度完全由 CSS flex 决定
- const tableScrollPx = ref(300)
- const isMoreFilter = ref(false)
- const spinning = ref(false)
- const sideWidth = ref(232)
- const selectedRowKeys = ref([])
- const renderColumns = ref([])
- const renderButtons = ref([])
- const showSelection = ref(false)
- const list = ref([])
- const tableData = ref([])
- const expandedRowKeys = ref([])
- // 服务端分页:showPagination 为真时才挂到 a-table 上(默认关,兼容原来「一次性全量」的用法)
- const pagination = reactive({
- current: 1,
- pageSize: 20,
- total: 0,
- showSizeChanger: true,
- showQuickJumper: true,
- pageSizeOptions: ['10', '20', '50', '100'],
- showTotal: (total) => \`\${window.$l('共')}\${total}\${window.$l('条')}\`
- })
- const iconMap = {
- add: 'PlusCircleOutlined',
- edit: 'FormOutlined',
- delete: 'DeleteOutlined',
- remove: 'DeleteOutlined',
- import: 'UploadOutlined',
- export: 'DownloadOutlined',
- assign: 'ClusterOutlined',
- execute: 'PlayCircleOutlined',
- pause: 'PauseCircleOutlined',
- resume: 'PoweroffOutlined',
- approve: 'HighlightOutlined',
- setting: 'SettingOutlined',
- TopOfType: 'VerticalAlignTopOutlined',
- TOP: 'VerticalAlignTopOutlined',
- synchronize: 'SyncOutlined'
- }
- // ---------- computed ----------
- const isShowImportModal = computed(() => renderButtons.value.map((i) => i.key).includes('import'))
- const importItem = computed(() => renderButtons.value.find((i) => i.key === 'import') || {})
- // ---------- watch ----------
- watch(
- () => props.buttons,
- (v) => {
- if (v && v.length) initRenderButtons(v)
- },
- { immediate: true, deep: true }
- )
- // ---------- 方法 ----------
- function isFunction(fn, name = '') {
- if (fn && typeof fn === 'function') {
- return true
- }
- const is = import.meta.env.NODE_ENV === 'development'
- if (is) {
- console.error(\`\${name}按钮未绑定接口Api方法,请绑定接口或从按钮回调事件"btnCallback"中自行处理\`)
- }
- return false
- }
- function renderIcon(v) {
- if (typeof v.icon === 'string') {
- return v.icon
- }
- if (typeof v.icon === 'boolean' && v.icon) {
- return iconMap[v.key] || ''
- }
- return ''
- }
- function initRenderButtons(list_) {
- // 后续可在这里对当前按钮集合编写逻辑
- renderButtons.value = list_.map((i) => {
- if (i.key === 'remove') showSelection.value = true
- return i
- })
- }
- function initTable() {
- nextTick(() => {
- // 高度一律 flex 撑满:这里只测主区域自身高度,再派生表格 scroll.y
- measureLayout()
- renderColumns.value = props.columns.map((i) => {
- if (
- (i.template || i.isClick || (i.dataIndex === 'action' && !i.customRender)) &&
- !i.scopedSlots
- ) {
- i.scopedSlots = { customRender: i.dataIndex }
- }
- return i
- })
- reload()
- })
- const timer = setTimeout(() => {
- const th = document.querySelector('.ant-table-cell.ant-table-cell-scrollbar')
- const thead = document.querySelector('.ant-table-thead tr')
- if (th && thead) thead.removeChild(th)
- clearTimeout(timer)
- }, 100)
- }
- function initComponent() {
- initTable()
- }
- function toggleFilter() {
- isMoreFilter.value = !isMoreFilter.value
- measureLayout()
- }
- // 按名字调用组件自身的方法(保持与 Options API 里 this[api] 一致的语义)
- function dispatchApi(api, payload) {
- const fn = actionMap[api]
- return fn(payload)
- }
- // 查询条件重置
- function filterReset() {
- emit('reset')
- }
- // 重新请求数据:resetPage=true 回到第一页(对齐 SListLayout 的 reload(true) 语义)
- // showPagination 为真走服务端分页(pageNo/pageSize 传后端并回写 total);
- // 为假时保持旧行为 pageSize:0(一次性取全量)。
- function reload(resetPage = false) {
- if (resetPage) pagination.current = 1
- const query = props.showPagination
- ? { pageNo: pagination.current, pageSize: pagination.pageSize }
- : { pageNo: 1, pageSize: 0 }
- return props.loadData(query).then((res) => {
- const rows = (res && (res.list || res.records)) || []
- tableData.value = rows.length ? rows : list.value
- pagination.total = Number((res && (res.total ?? res.totalCount)) ?? rows.length)
- return res
- })
- }
- function handTableChange(page) {
- pagination.current = page.current
- pagination.pageSize = page.pageSize
- reload()
- }
- // 树列表 - 新增/编辑/详情 时 不刷新
- function setFilterParams() {
- console.log(instance?.parent)
- }
- // 按钮回调
- function btnCallback(item) {
- emit('btnCallback', item.key, selectedRowKeys.value)
- const fn = actionMap[item.key]
- if (typeof fn === 'function') {
- fn(item)
- } else {
- handAction(item)
- }
- }
- // 其他未定义按钮方法生成
- function handAction(item) {
- if (item.selection !== false && !selectedRowKeys.value.length) {
- window.$message.warn(\`\${window.$l('请选择要')}\${window.$l(item.name)}\${window.$l('的数据')}\`)
- return
- }
- if (isFunction(item.api, item.name)) {
- if (item.confirm) {
- window.$confirm({
- title: \`\${item.name}\`,
- content: () =>
- h(
- 'div',
- { style: 'color:var(--s-color-error)' },
- \`\${window.$l('确认要')}{\${window.$l(item.name)}}\${window.$l('数据吗?')}\`
- ),
- onOk: () => {
- spinning.value = true
- item
- .api(selectedRowKeys.value)
- .then(() => {
- window.$message.success(\`\${window.$l(item.name)}\${window.$l('成功')}\`)
- emit('reset')
- })
- .finally(() => {
- spinning.value = false
- })
- }
- })
- return
- }
- spinning.value = true
- item
- .api(selectedRowKeys.value)
- .then(() => {
- window.$message.success(\`\${window.$l(item.name)}\${window.$l('成功')}\`)
- emit('reset')
- })
- .finally(() => {
- spinning.value = false
- })
- }
- }
- // 设置列表路由地址
- function setHistoryPath() {
- sessionStorage.setItem('backPath', route.path)
- }
- // 添加方法(业务自行处理,这里只做占位,不再走 handAction)
- function addAction() {
- emit('btnCallback', 'add')
- }
- // 删除方法
- function removeAction() {
- if (!selectedRowKeys.value.length) {
- window.$notice.warn({
- key: 'only-you',
- message: window.$l('操作失败'),
- description: window.$l('请选择要删除的数据')
- })
- return
- }
- if (isFunction('deletesApi')) {
- window.$confirm({
- title: window.$l('确认删除'),
- content: () =>
- h('div', { style: 'color: var(--s-color-error)' }, window.$l('确认要批量删除数据吗?')),
- onOk: () => {
- spinning.value = true
- dispatchApi('deletesApi', selectedRowKeys.value)
- .then(() => {
- window.$notice.success({
- key: 'only-you',
- message: window.$l('操作成功'),
- description: window.$l('删除成功')
- })
- emit('reset')
- })
- .finally(() => {
- spinning.value = false
- })
- }
- })
- }
- }
- // 导入方法
- function importAction() {
- if (!isShowImportModal.value) {
- emit('btnCallback', 'import')
- return
- }
- importVisible.value = true
- }
- // 导出方法
- function exportAction() {
- if (isFunction('exportApi')) {
- spinning.value = true
- dispatchApi('exportApi').then(() => {
- spinning.value = false
- })
- }
- }
- // 表格可点击列事件回调
- function tableColCallback(field, item) {
- emit('tableColCallback', field, item)
- }
- // 按 dataIndex 取列配置
- function getColumn(dataIndex) {
- return renderColumns.value.find((i) => i.dataIndex === dataIndex)
- }
- // 该列是否使用自定义插槽(列 dataIndex 即插槽名)
- function isTemplateColumn(dataIndex) {
- const col = getColumn(dataIndex)
- return !!(col && col.template)
- }
- // 该列是否为可点击列
- function isClickColumn(dataIndex) {
- const col = getColumn(dataIndex)
- return !!(col && col.isClick)
- }
- // 可点击列显示文本:对象取 name,其它原样
- function getCellText(text) {
- if (text === null || text === undefined) return ''
- if (typeof text === 'object') return text.name === undefined ? '' : text.name
- return text
- }
- function onSelectChange(keys) {
- selectedRowKeys.value = keys
- }
- // ---------- 高度 ----------
- // 布局高度全部交给 CSS flex(根 100% → 主区 flex:1 → 表格链条逐层贯通),
- // 这里只把 flex 撑出来的真实表体高度同步给 scroll.y —— antd 需要 scroll.y 才启用固定表头。
- // 旧的算术法(父高 − 工具条 − 过滤区 − 分页)死穴是首帧分页还没渲染,必然差十几像素;
- // 同步真实 clientHeight 没有这个问题,值偏小也不影响布局(.ant-table-body 已 max-height:none)。
- function measureLayout() {
- nextTick(syncTableHeight)
- }
- function syncTableHeight() {
- const el = table.value && table.value.$el
- const bodyEl = el && el.querySelector('.ant-table-body')
- const h = bodyEl && bodyEl.clientHeight
- if (h) tableScrollPx.value = Math.max(h - 8, 0)
- }
- // 树表格展开
- function handExpand(expanded, record) {
- emit('handExpand', expanded, record)
- }
- // 按钮 key → 处理函数(等价于原 Options API 里 this[item.key] 的语义)
- const actionMap = {
- add: addAction,
- remove: removeAction,
- import: importAction,
- export: exportAction,
- reload,
- filterReset,
- toggleFilter,
- handAction
- }
- // ---------- 拖拽分隔条指令 ----------
- const vSidedrag = createSideDrag(
- () => sideWidth.value,
- (width) => (sideWidth.value = width),
- { min: 232 }
- )
- // ---------- 生命周期 ----------
- onMounted(() => {
- initComponent()
- })
- defineExpose({
- reload,
- spinning,
- selectedRowKeys,
- expandedRowKeys,
- renderColumns,
- renderButtons,
- tableData,
- pagination,
- table,
- importVisible,
- measureLayout,
- toggleFilter,
- filterReset,
- btnCallback
- })
- <\/script>
- <style lang="less" scoped>
- .list-layout {
- height: 100%;
- min-height: 0;
- display: flex;
- flex-direction: column;
- overflow: hidden;
- }
- .list-layout > :deep(.ant-spin-nested-loading),
- .list-layout > :deep(.ant-spin-container) {
- flex: 1 1 auto;
- min-height: 0;
- display: flex;
- flex-direction: column;
- }
- .other {
- flex: 0 0 auto;
- }
- .tools {
- flex: 0 0 60px;
- height: 60px;
- display: flex;
- justify-content: space-between;
- align-items: center;
- box-sizing: border-box;
- .title {
- font-size: 18px;
- font-weight: 500;
- }
- }
- .list-layout-main {
- flex: 1 1 auto;
- min-height: 0;
- display: flex;
- justify-content: flex-start;
- align-items: stretch;
- border-radius: 6px;
- background-color: var(--s-bg-color-container);
- overflow: hidden;
- }
- .side {
- flex: 0 0 auto;
- min-width: 232px;
- padding: 10px;
- box-sizing: border-box;
- overflow: hidden;
- }
- .side-bar {
- flex: 0 0 10px;
- width: 10px;
- align-self: stretch;
- cursor: pointer;
- user-select: none;
- }
- .layout-list-content {
- flex: 1 1 auto;
- min-width: 0;
- min-height: 0;
- overflow: hidden;
- display: flex;
- flex-direction: column;
- justify-content: flex-start;
- align-items: flex-start;
- }
- .list-filter-wrap {
- width: 100%;
- padding: 10px 16px;
- box-sizing: border-box;
- border-bottom: 1px solid var(--s-bg-color);
- .filter-content {
- height: 32px;
- display: flex;
- justify-content: flex-start;
- align-items: center;
- }
- .filter {
- // flex: 1;
- }
- }
- .filter-more-content {
- // border-bottom: 1px solid var(--s-border-color-light);
- margin-top: 16px;
- // padding-top: 16px;
- // box-sizing: border-box;
- }
- .desc-wrap {
- width: 100%;
- padding: 16px 0 0;
- box-sizing: border-box;
- }
- .reset-wrap {
- margin-left: 24px;
- flex: 1;
- text-align: right;
- }
- .filter-more-btn {
- min-width: 60px;
- text-align: center;
- }
- .table-content {
- width: 100%;
- flex: 1;
- display: flex;
- flex-direction: column;
- min-height: 0;
- .tabs-wrap {
- padding: 0 20px;
- box-sizing: border-box;
- flex: 0 0 auto;
- }
- :deep(.ant-spin-nested-loading .ant-spin-blur) {
- opacity: 1;
- }
- :deep(.ant-table-pagination) {
- margin-top: 0;
- padding: 16px 16px 16px 0;
- border-top: 1px solid var(--s-border-color-split);
- // 不留底部外边距,否则分页与容器底边恒差一个 margin
- margin-bottom: 0;
- flex: 0 0 auto;
- }
- // ---- 撑满链路 ----
- :deep(.table-wrapper),
- :deep(.ant-table-wrapper),
- :deep(.ant-spin-nested-loading),
- :deep(.ant-spin-container),
- :deep(.ant-table),
- :deep(.ant-table-container) {
- flex: 1 1 auto;
- min-height: 0;
- display: flex;
- flex-direction: column;
- }
- :deep(.ant-table-header) {
- flex: 0 0 auto;
- }
- :deep(.ant-table-body) {
- flex: 1 1 auto;
- min-height: 0;
- max-height: none !important;
- }
- }
- .table-container {
- flex: 1 1 auto;
- min-height: 0;
- display: flex;
- flex-direction: column;
- box-sizing: border-box;
- }
- </style>
- `;
- export {
- e as default
- };
|