| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776 |
- const e = `<template>
- <div class="list-layout" ref="layout">
- <a-spin :spinning="spinning">
- <div class="tools">
- <div class="title">
- {{ $l(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' }"
- style="overflow-y: auto"
- v-if="$slots.tree"
- >
- <slot name="tree" />
- </div>
- <div class="side-bar s-theme-bg" v-if="$slots.tree" v-sidedrag></div>
- <div class="layout-list-content" v-if="!$slots.empty && listType === 'table'">
- <div class="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-button
- v-if="isEnableAdvancedSearch"
- class="advanced-search"
- type="link"
- @click="advancedSearch"
- >{{ $l('高级搜索') }}</a-button
- >
- <a-tooltip placement="top" :overlayStyle="{ maxWidth: '500px' }">
- <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="search-bar" v-if="isEnableSearchBar">
- <slot name="search-bar" />
- </div>
- <div class="status-bar" v-if="isEnableStatusBar">
- <slot name="status-bar" />
- </div>
- <div class="table-content">
- <div class="tabs-wrap" v-if="$slots.tabs" ref="tabs">
- <slot name="tabs" />
- </div>
- <div class="table-container">
- <s-table
- ref="table"
- size="middle"
- :scroll="{ y: tableScrollPx, x: '100%' }"
- :pageSize="pageSize"
- :rowKey="rowKey"
- :columns="renderColumns"
- :data="loadData"
- v-model:expanded-row-keys="expandedRowKeys"
- :rowSelection="
- showSelection || isSelection
- ? {
- selectedRowKeys: selectedRowKeys,
- onChange: onSelectChange,
- getCheckboxProps: getCheckboxProps
- }
- : null
- "
- :showPagination="showPagination"
- >
- <!-- template / isClick / action 列共用 bodyCell: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>
- </s-table>
- </div>
- </div>
- </div>
- <div class="layout-list-content" v-if="!$slots.empty && listType === 'custom'">
- <slot name="custom-layout-list-content" />
- </div>
- <div class="layout-list-content" v-if="$slots.empty">
- <slot name="empty" />
- </div>
- </div>
- <div class="other">
- <slot name="other" />
- </div>
- </a-spin>
- <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, h, nextTick, onActivated, onMounted, ref, watch } from 'vue'
- import { useRoute } from 'vue-router'
- import STable from '../Table/index'
- import { createSideDrag } from '../../utils/side-drag'
- defineOptions({ name: 'SNewestDataListLayout' })
- const props = defineProps({
- // 页面标题
- title: {
- type: String,
- required: true
- },
- // 工具栏按钮配置
- buttons: {
- type: Array,
- default: () => [
- {
- name: window.$l('添加'),
- key: 'add',
- show: true,
- type: 'primary',
- icon: true,
- api: null
- },
- {
- name: window.$l('批量删除'),
- key: 'remove',
- action: 'remove',
- show: true,
- icon: true,
- api: null
- },
- {
- name: window.$l('导入'),
- key: 'import',
- show: true,
- icon: true,
- importColumns: [], // 预览的表头信息
- importUploadUrl: null, // 上传url
- importApis: {
- templateDownload: null,
- importPreview: null,
- importData: null,
- exportExceptionData: null
- }
- },
- {
- name: window.$l('导出'),
- key: 'export',
- show: true,
- icon: true,
- api: null
- }
- ]
- },
- // 列表checkbox列
- isSelection: {
- type: Boolean,
- default: false
- },
- // 是否显示分页
- showPagination: {
- type: Boolean,
- default: true
- },
- // 表格列配置
- columns: {
- type: Array,
- default: () => []
- },
- // 行主键字段名
- rowKey: {
- type: [Function, String],
- default: 'id'
- },
- // 取数函数,返回 { list, total }
- loadData: {
- type: Function,
- required: true
- },
- // 每页条数
- pSize: {
- type: [String, Number],
- default: 20
- },
- // 列表主体形态:table 走表格,custom 走 custom-layout-list-content 插槽
- listType: {
- type: String,
- default: 'table'
- },
- // 高级搜索入口
- isEnableAdvancedSearch: {
- type: Boolean,
- default: false
- },
- // 状态条插槽开关
- isEnableStatusBar: {
- type: Boolean,
- default: false
- },
- // 搜索条插槽开关
- isEnableSearchBar: {
- type: Boolean,
- default: false
- },
- // 行勾选控制(antd rowSelection.getCheckboxProps):返回 { disabled, ... }
- getCheckboxProps: {
- type: Function,
- default: () => ({})
- }
- })
- const emit = defineEmits(['reset', 'btnCallback', 'tableColCallback', 'advancedSearch'])
- const route = useRoute()
- // ---------- 模板 ref ----------
- const layout = ref(null)
- const side = ref(null)
- const filters = ref(null)
- const tabs = ref(null)
- const table = ref(null)
- const importModalRef = 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 pageSize = ref(20)
- const sideWidth = ref(256)
- const showSelection = ref(false)
- const selectedRowKeys = ref([])
- const expandedRowKeys = ref([])
- const renderColumns = ref([])
- const renderButtons = ref([])
- 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 }
- )
- watch(
- () => props.columns,
- () => initComponent()
- )
- // ---------- 方法 ----------
- 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(() => {
- measureLayout()
- pageSize.value = props.pSize
- 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
- })
- })
- 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)
- }, 100)
- }
- function initComponent() {
- nextTick(() => {
- initTable()
- })
- }
- function toggleFilter() {
- isMoreFilter.value = !isMoreFilter.value
- measureLayout()
- }
- // 查询条件重置
- function filterReset() {
- emit('reset')
- }
- function reload() {
- table.value && table.value.refresh(true)
- }
- // 树列表 - 新增/编辑/详情 时 不刷新
- function setFilterParams() {}
- // 设置列表路由地址
- function sethsitoryPath() {
- sessionStorage.setItem('backPath', route.path)
- }
- // 按钮回调
- 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, item.key)) {
- 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 setStateBtn(action, opt) {
- if (typeof action === 'string') {
- setStateBtnItem(action, opt)
- } else {
- action.forEach((i) => {
- setStateBtnItem(i, opt)
- })
- }
- }
- function setStateBtnItem(action, opt) {
- const item = renderButtons.value.find((i) => i.key === action)
- if (!item) {
- console.error(\`按钮(\${action})未定义, 请确认该变更状态按钮是否设置正确\`)
- return
- }
- Object.keys(opt).forEach((i) => {
- item[i] = opt[i]
- })
- }
- // 添加方法(业务自行处理,这里只做占位,不再走 handAction)
- function addAction() {}
- // 删除方法
- function deleteAction(item) {
- handAction(item)
- }
- // 导入方法
- function importAction() {
- if (!isShowImportModal.value) {
- emit('btnCallback', 'import')
- return
- }
- importVisible.value = true
- }
- // 导出方法
- function exportAction(item) {
- if (isFunction(item.api, item.name, item.key)) {
- spinning.value = true
- item
- .api(selectedRowKeys.value)
- .then(() => {
- spinning.value = false
- })
- .catch(() => {
- 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 advancedSearch() {
- emit('advancedSearch')
- }
- // 按钮 key → 处理函数(等价于原 Options API 里 this[item.key] 的语义)
- const actionMap = {
- add: addAction,
- delete: deleteAction,
- import: importAction,
- export: exportAction,
- reload,
- filterReset,
- toggleFilter,
- setStateBtn,
- handAction
- }
- // ---------- 拖拽分隔条指令 ----------
- const vSidedrag = createSideDrag(
- () => sideWidth.value,
- (width) => (sideWidth.value = width),
- { min: 256 }
- )
- // ---------- 生命周期 ----------
- onMounted(() => {
- initComponent()
- })
- // keep-alive 缓存页切回来时 DOM 重新可见,下一帧补同步一次 scroll.y
- onActivated(() => {
- nextTick(syncTableHeight)
- })
- defineExpose({
- reload,
- spinning,
- selectedRowKeys,
- expandedRowKeys,
- renderColumns,
- renderButtons,
- setStateBtn,
- btnCallback,
- filterReset,
- toggleFilter,
- table,
- importVisible,
- measureLayout,
- advancedSearch
- })
- <\/script>
- <style lang="less" scoped>
- // 高度实现:纯 CSS flex,零 JS 测量。
- // 根节点撑满父容器 → 工具条定高 → 主区吃掉剩余高度 → 表格链条逐层贯通到表体。
- // 前提:父容器有确定高度(基座 BasicLayout 的 router-view 已带 height);
- // 父级高度不确定时退化为「按内容撑开」,不会像旧版那样测量成 0 直接塌掉。
- .list-layout {
- height: 100%;
- min-height: 0;
- display: flex;
- flex-direction: column;
- overflow: hidden;
- }
- // 根节点外再包一层 a-spin 时(如 SNewestDataListLayout),把剩余高度透传下去,
- // 否则 .list-layout-main 拿不到高度,整页退化成内容高度
- .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;
- // border-bottom: 1px solid var(--s-bg-color);
- // padding: 0 16px;
- 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;
- // stretch 让左右两栏撑满主区高度;flex-start 会让子栏退回内容高度,表格就不吃剩余空间了
- align-items: stretch;
- border-radius: 6px;
- background-color: var(--s-bg-color-container);
- overflow: hidden;
- }
- .side {
- // 宽度由模板内联的 sideWidth 决定,不参与伸缩,否则拖拽后的宽度会被挤掉
- flex: 0 0 auto;
- min-width: 256px;
- padding: 10px;
- box-sizing: border-box;
- overflow: hidden;
- }
- .side-bar {
- // 必须禁止伸缩:主区和左树都带 flex-grow,空间不足时这根 10px 的分隔条会被压成 0 宽,
- // 表现就是「分隔条不见了、左侧树拖不动」
- 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;
- padding: 24px;
- box-sizing: border-box;
- // 该布局内容区是「查询区 + 表格区」纵向排列,必须是 flex 容器,表格区才能吃掉剩余高度
- display: flex;
- flex-direction: column;
- }
- // 表格区吃掉内容区剩余高度;与 SListLayout 同一套撑满链路
- .table-container {
- flex: 1 1 auto;
- min-width: 0;
- min-height: 0;
- display: flex;
- flex-direction: column;
- overflow: hidden;
- // STable 自带的 .table-wrapper 不能漏,漏了整条链断掉、表体撑不开
- :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;
- // antd 把 scroll.y 写成 inline max-height,会卡住 flex 撑出来的高度
- max-height: none !important;
- }
- :deep(.ant-table-pagination.ant-pagination) {
- flex: 0 0 auto;
- margin: 0;
- padding: 16px 0;
- border-top: 1px solid var(--s-border-color-split);
- box-sizing: border-box;
- }
- }
- .filter-wrap {
- flex: 0 0 auto;
- padding: 0 0 24px;
- box-sizing: border-box;
- .filter-content {
- height: 32px;
- display: flex;
- justify-content: flex-start;
- align-items: center;
- }
- }
- .filter-more-content {
- margin-top: 4px;
- }
- .search-bar {
- width: 100%;
- }
- .status-bar {
- width: 100%;
- }
- .desc-wrap {
- padding: 16px 0 0;
- box-sizing: border-box;
- }
- .reset-wrap {
- margin-left: 24px;
- flex: 1;
- text-align: right;
- }
- </style>
- `;
- export {
- e as default
- };
|