| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761 |
- 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' }"
- 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 class="layout-list-content" v-if="!$slots.empty">
- <div class="list-filter-wrap" ref="filters" v-show="$slots.filter">
- <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-spin :spinning="loading">
- <div class="s-card-grid" :style="{ gridTemplateColumns: \`repeat(\${column}, 1fr)\` }">
- <div class="s-card-item" v-for="(item, index) in list" :key="cardKey(item, index)">
- <!-- header:优先用 cardHeader 插槽,其次数据项 title,两者都空则不渲染 -->
- <div class="s-card-header" v-if="$slots.cardHeader || item.title">
- <slot name="cardHeader" :item="item" :index="index">{{ item.title }}</slot>
- </div>
- <!-- 内容区:完全由业务自定义(作用域插槽拿到 item / index) -->
- <div class="s-card-body">
- <slot :item="item" :index="index" />
- </div>
- <!-- footer:默认底部按钮区,传 footer 插槽则完全替换按钮 -->
- <div class="s-card-footer">
- <slot name="cardFooter" :item="item" :index="index">
- <a-button
- type="link"
- size="small"
- @click="handCardAction('edit', item, index)"
- >
- {{ $l('编辑') }}
- </a-button>
- <a-button
- type="link"
- size="small"
- danger
- @click="handCardAction('delete', item, index)"
- >
- {{ $l('删除') }}
- </a-button>
- </slot>
- </div>
- </div>
- <a-empty
- class="s-card-empty"
- v-if="!loading && !list.length"
- :description="$l('暂无数据')"
- />
- </div>
- </a-spin>
- </div>
- <div class="s-card-pagination" v-if="showPagination" ref="pager">
- <a-pagination
- size="small"
- v-model:current="pageNo"
- :page-size="Number(pageSize)"
- :total="total"
- show-size-changer
- :page-size-options="pageSizeOptions"
- :show-total="(t) => \`\${$l('共')}\${t}\${$l('条')}\`"
- @change="handPageChange"
- />
- </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, h, nextTick, onActivated, onMounted, ref, watch } from 'vue'
- import { createSideDrag } from '../../utils/side-drag'
- defineOptions({ name: 'SListCardLayout' })
- const props = defineProps({
- // 每行卡片个数(不传默认每行 5 个)
- column: {
- type: [Number, String],
- default: 5
- },
- // 卡片数据主键字段(用于 v-for key)
- rowKey: {
- type: [Function, String],
- default: 'id'
- },
- // 每页条数可选项
- pageSizeOptions: {
- type: Array,
- default: () => ['10', '20', '40', '80']
- },
- // 页面标题
- 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
- }
- ]
- },
- // 是否显示分页
- showPagination: {
- type: Boolean,
- default: true
- },
- // 取数函数,返回 { list, total }
- loadData: {
- type: Function,
- required: true
- },
- // 每页条数
- pSize: {
- type: [String, Number],
- default: 20
- }
- })
- const emit = defineEmits(['reset', 'btnCallback', 'cardAction'])
- // ---------- 模板 ref ----------
- const layout = ref(null)
- const filters = ref(null)
- const tabs = ref(null)
- const table = ref(null)
- // 卡片分页条(高度要从卡片区里扣掉,否则出现双滚动条)
- const pager = ref(null)
- const spaceSize = ref(8)
- // ---------- 状态 ----------
- const importVisible = ref(false)
- // 卡片区高度已交给 CSS flex,不再需要 JS 测量
- const isMoreFilter = ref(false)
- const spinning = ref(false)
- const pageSize = ref(20)
- const sideWidth = ref(232)
- const showSelection = ref(false)
- const selectedRowKeys = ref([])
- const renderButtons = ref([])
- // ---------- 卡片列表 ----------
- const list = ref([])
- const total = ref(0)
- const pageNo = ref(1)
- const loading = ref(false)
- 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 initComponent() {
- nextTick(() => {
- // 高度一律 flex 撑满:这里只测主区域自身高度,再派生出 card 区滚动高度
- measureLayout()
- pageSize.value = props.pSize
- fetchList()
- })
- }
- function toggleFilter() {
- isMoreFilter.value = !isMoreFilter.value
- measureLayout()
- }
- // 查询条件重置
- function filterReset() {
- emit('reset')
- }
- // 刷新卡片列表:true 回到第一页
- function reload(forceFirst = false) {
- fetchList(forceFirst)
- }
- /**
- * 取卡片数据。
- * 与 STable 的取数约定完全一致:loadData({ pageNo, pageSize }) → { list, total }
- * 兼容后端 PageHelper 的几种外层包裹:顶层 total / pagination.total / totalCount。
- */
- async function fetchList(forceFirst = false) {
- if (typeof props.loadData !== 'function') return
- if (forceFirst) pageNo.value = 1
- loading.value = true
- try {
- const res =
- (await props.loadData({ pageNo: pageNo.value, pageSize: Number(pageSize.value) })) || {}
- const rows = res.list || res.data || []
- list.value = Array.isArray(rows) ? rows : []
- total.value = res.total ?? res.pagination?.total ?? res.totalCount ?? 0
- const current = res.pageNo || res.pageNum || res.pagination?.current
- if (current) pageNo.value = current
- } catch (e) {
- // 接口异常时结束 loading,避免卡片区一直转圈
- console.warn('[SListCardLayout] 卡片数据加载失败:', e)
- list.value = []
- total.value = 0
- } finally {
- loading.value = false
- }
- }
- // 翻页 / 切每页条数(a-pagination 切条数时会连触发 change,这里统一在 change 里判断,
- // 避免同一动作发两次请求)
- function handPageChange(page, size) {
- if (size && Number(size) !== Number(pageSize.value)) {
- pageSize.value = Number(size)
- pageNo.value = 1
- } else {
- pageNo.value = page
- }
- fetchList()
- }
- // 卡片默认底部按钮回调,业务自行处理
- function handCardAction(key, item, index) {
- emit('cardAction', key, item, index)
- }
- // 卡片 v-for key:rowKey 支持字段名或函数,取不到时退回下标
- function cardKey(item, index) {
- if (typeof props.rowKey === 'function') return props.rowKey(item, index)
- const k = item?.[props.rowKey]
- return k === undefined || k === null || k === '' ? index : k
- }
- // 按钮回调
- 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
- }
- // ---------- 高度 ----------
- // 卡片区高度由 CSS flex 决定(根 100% → 主区 flex:1 → 卡片区吃掉剩余并内部滚动),
- // 这里保留空实现,只为兼容既有的 measureLayout 调用点(toggleFilter / keep-alive 切换)。
- function measureLayout() {
- nextTick(syncTableHeight)
- }
- function syncTableHeight() {}
- // 按钮 key → 处理函数(等价于原 Options API 里 this[item.key] 的语义)
- const actionMap = {
- add: addAction,
- delete: deleteAction,
- import: importAction,
- reload,
- filterReset,
- toggleFilter,
- setStateBtn,
- handAction
- }
- // ---------- 拖拽分隔条指令 ----------
- const vSidedrag = createSideDrag(
- () => sideWidth.value,
- (width) => (sideWidth.value = width),
- { min: 232 }
- )
- // ---------- 生命周期 ----------
- onMounted(() => {
- initComponent()
- })
- // keep-alive 缓存页切回来时 DOM 重新可见,下一帧补同步一次
- onActivated(() => {
- nextTick(syncTableHeight)
- })
- defineExpose({
- reload,
- spinning,
- selectedRowKeys,
- renderButtons,
- setStateBtn,
- btnCallback,
- filterReset,
- toggleFilter,
- table,
- importVisible,
- measureLayout,
- // 卡片列表
- list,
- total,
- pageNo,
- fetchList
- })
- <\/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: 8px;
- background-color: var(--s-bg-color-container);
- overflow: hidden;
- }
- .side {
- // 宽度由模板内联的 sideWidth 决定,不参与伸缩,否则拖拽后的宽度会被挤掉
- flex: 0 0 auto;
- min-width: 232px;
- 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;
- 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;
- }
- }
- .table-content {
- width: 100%;
- flex: 1;
- // 卡片网格自身撑满剩余高度,超出部分在容器内滚动,不撑破页面可视区
- display: flex;
- flex-direction: column;
- min-height: 0;
- :deep(.ant-spin-nested-loading .ant-spin-blur) {
- opacity: 1;
- }
- :deep(.ant-table-pagination) {
- border-top: 1px solid var(--s-border-color-base);
- margin-top: 0;
- margin-bottom: 0;
- padding: 16px 0;
- flex: 0 0 auto;
- }
- }
- .table-container {
- flex: 1 1 auto;
- min-height: 0;
- box-sizing: border-box;
- padding: 12px 16px;
- overflow: hidden auto;
- .item-card {
- border: 1px solid var(--s-border-color-split);
- border-radius: 6px;
- background-color: var(--s-bg-color-container);
- }
- }
- // ---------- 卡片列表(列数由 column 控制,行内 style 只负责 gridTemplateColumns)----------
- .s-card-grid {
- display: grid;
- gap: 12px;
- // 卡片按行从顶部开始排,不随容器剩余高度拉伸
- align-content: start;
- }
- .s-card-item {
- display: flex;
- flex-direction: column;
- // 高度与宽度一致:正方形卡片(宽由列宽决定,高自动等于宽)
- aspect-ratio: 1 / 1;
- min-width: 0;
- box-sizing: border-box;
- border: 1px solid var(--s-border-color-split);
- border-radius: 8px;
- background-color: var(--s-bg-color-container);
- overflow: hidden;
- }
- .s-card-header {
- flex: 0 0 auto;
- padding: 10px 12px;
- border-bottom: 1px solid var(--s-bg-color);
- font-size: 15px;
- font-weight: 500;
- color: var(--s-text-color);
- overflow: hidden;
- white-space: nowrap;
- text-overflow: ellipsis;
- }
- .s-card-body {
- flex: 1 1 auto;
- min-height: 0;
- padding: 10px 12px;
- // 卡片是正方形,内容超出时在内容区内部滚动,不撑破卡片
- overflow: hidden;
- }
- .s-card-footer {
- flex: 0 0 auto;
- min-height: 36px;
- padding: 2px 8px;
- display: flex;
- align-items: center;
- justify-content: space-between;
- border-top: 1px solid var(--s-bg-color);
- }
- .s-card-empty {
- // 跨满整行,避免只占第一格
- grid-column: 1 / -1;
- margin: 24px 0;
- }
- .s-card-pagination {
- width: 100%;
- flex: 0 0 auto;
- display: flex;
- justify-content: flex-end;
- padding: 16px 16px 16px 0;
- border-top: 1px solid var(--s-border-color-split);
- box-sizing: border-box;
- }
- .filter-more-content {
- // border-bottom: 1px solid var(--s-border-color-light);
- margin-top: 16px;
- // padding-top: 16px;
- // box-sizing: border-box;
- }
- .desc-wrap {
- 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;
- }
- </style>
- `;
- export {
- e as default
- };
|