当前位置: 首页 > news >正文

做网站自学模板网站建设优惠

做网站自学,模板网站建设优惠,东莞机械网络推广,网站建设认知与理解基于 elements3 包装的 可展开 table 组件1.手风琴效果&#xff0c;一次只能展开一个2.动态加载 展开内容 &#xff08;根据接口&#xff09;效果&#xff1a;<template><el-tableref"tableRef":data"tableData"v-loading"loading"v-if…

基于 elements3 包装的 可展开 table 组件

1.手风琴效果,一次只能展开一个

2.动态加载 展开内容 (根据接口)

效果:

<template><el-tableref="tableRef":data="tableData"v-loading="loading"v-if="columns.length > 0"style="width: 100%"@expand-change="handleExpand"class="custom-expend-table"header-row-class-name="custom-table-header"row-class-name="custom-table-row"@row-click="handleClick"><!-- 折叠列 --><el-table-column type="expand" v-if="hasExpand" width="20"><template #default="{ row }"><div class="expand-content-wrapper"><el-tablev-if="childDataMap[row[rowKey]]?.length":data="childDataMap[row[rowKey]]"size="small"header-row-class-name="child-table-header"row-class-name="child-table-row"><el-table-columnv-for="col in childColumns":key="col.column":label="col.showName":prop="col.column":min-width="col.width || col.column === 'month' ? 50 : 80"align="center"><template #default="{ row }">{{ row[col.column] || '-' }}</template></el-table-column></el-table><div v-else class="text-gray-500 text-center py-4 empty-box">{{ expandLoadingMap[row[rowKey]] ? '加载中...' : '暂无数据' }}</div></div></template></el-table-column><!-- 主表列 --><el-table-columnv-for="col in columns":key="col.column":prop="col.column":label="col.showName":min-width="col.width || col.column === 'year' ? 50 : 80"align="center"><template #default="{ row }">{{ row[col.column] || '-' }}</template></el-table-column><template #empty><EmptyBox desc="无数据" v-if="height" :height="'auto'" /><EmptyBox desc="无数据" v-else /></template></el-table>
</template><script setup lang="ts">
import { ref , PropType,computed } from 'vue'
import { ElMessage } from 'element-plus'
import {sortByDate} from "@/utils/formate/sort_formate";
import { isEqual  } from 'lodash-es';const props = defineProps({height: {type: [String, Boolean],default: '180'},tableData: {type: Array as PropType<any[]>,required: true,},columns: {type: Array as PropType<any[]>, // 使用定义好的 Column 类型required: true,}, // 主表字段rowKey: {type: String,default: 'id',},fetchChildData: {type: Function || Function as PropType<(row: any) => Promise<any>> ,required: false,default: undefined},loading: {type: Boolean,default: false,},isThrottle: {type: Boolean,default: false,}
})const hasExpand = computed(() => typeof props.fetchChildData === 'function')// 保存每行子表数据
const childDataMap = ref<Record<string, any[]>>({})
const childColumns = ref<any[]>([])
const expandLoadingMap = ref<Record<string, boolean>>({})// 展开行触发:异步加载子表
const handleExpand = async (row: any, expanded: any[]) => {const rowKey: any = row[props.rowKey]// 当前展开的 rowKey(row 点击后立即触发)const isExpanding: any = expanded.some((t: any) => rowKey == t[props.rowKey])// 收起时清空记录if (!isExpanding) {if (currentExpandedRowKey.value === rowKey) {currentExpandedRowKey.value = null}return}// 若当前点击的不是同一行,先收起上一个if (currentExpandedRowKey.value && currentExpandedRowKey.value !== rowKey) {const prevRow = props.tableData?.find((item: any) => item[props.rowKey] === currentExpandedRowKey.value)if (prevRow && tableRef.value) {tableRef.value.toggleRowExpansion(prevRow, false)}}// 设置当前展开行为当前行currentExpandedRowKey.value = rowKey// 判断是否需要加载数据if (props.isThrottle && childDataMap.value[rowKey]) returnexpandLoadingMap.value[rowKey] = truetry {if (!props.fetchChildData) returnconst { head, data } = await props.fetchChildData(row)sortByDate(data, 'month', 'desc')childDataMap.value[rowKey] = data || []childColumns.value = head || []} catch (err) {ElMessage.error('子表加载失败')childDataMap.value[rowKey] = []} finally {expandLoadingMap.value[rowKey] = false}
}const tableRef = ref()
const currentExpandedRowKey = ref<string | null>(null)
const currentExpandedRow = ref<any>(null)
const handleClick = (row: any) => {if (!hasExpand.value) returnconst rowKey = row[props.rowKey]// 从 tableData 中找到真正要展开的那一行(必要)const matchedRow: any = props.tableData?.find((item: any) => item[props.rowKey] === rowKey)if (!matchedRow) {console.warn('找不到匹配的行数据:', rowKey)return}// 👉 判断是否是“当前已展开的那一行”const isSameRow = (currentExpandedRowKey.value === rowKey &&isEqual(currentExpandedRow.value, matchedRow))// 如果点击的是当前已展开行 → 收起if ( isSameRow ) {tableRef.value.toggleRowExpansion(matchedRow, false)currentExpandedRowKey.value = nullcurrentExpandedRow.value = nullreturn}// 收起之前已展开的if (currentExpandedRowKey.value !== null) {const prevRow = props.tableData.find((item: any) => item[props.rowKey] === currentExpandedRowKey.value)if (prevRow) {tableRef.value.toggleRowExpansion(prevRow, false)}}// 展开新行tableRef.value.toggleRowExpansion(matchedRow, true)currentExpandedRowKey.value = rowKeycurrentExpandedRow.value = matchedRow
}defineExpose({handleClick,
});</script>

使用:

    <CustomExpandTableref="expandTable":columns="tableData.head":tableData="tableData.data":fetchChildData="getSubTable"rowKey="stattime"></CustomExpandTable>

http://www.dtcms.com/a/535157.html

相关文章:

  • 模具东莞网站建设英文seo优化包年费用
  • 宁夏住房和城乡建设局网站wordpress模板UI
  • 中国网建北京网站优化步
  • 做任务可以给钱的网站从化公司网站建设
  • 息烽县住房和城乡建设局网站沈阳建设网站公司
  • 深圳商城网站制作英文网站seo
  • 在哪里创建网站平台知名企业网站人才招聘情况
  • 曲阜建设局网站触摸屏网站如何做
  • 网站正在建设页面Wordpress写文章刷不开
  • 网站不公开简历做家教网站开发不满意
  • 便捷网站建设哪家便宜ppt设计器在哪里
  • 网站框架是什么wordpress快递查询
  • 高端网站制作平台找工作哪个网站好智联招聘
  • 上海建设网站是国家级吗大同工程建设信息网
  • 网站页面类型做物流的用什么网站配货
  • wordpress formseo快速排名上首页
  • 浙江广发建设有限公司网站昆明seo培训
  • 深汕特别合作区小漠镇seo推广的特点
  • 网站开发的软件有哪些岳阳网站开发商城
  • 响应式网站是怎么做的有什么好用的搜索引擎
  • 大连seo建站网站如何在google提交收录
  • 做游戏类型的网站的好处网站小样用什么做
  • 电子会员卡系统哪个好seo 排名
  • 宣讲网站建设昆明开发app公司
  • 潮州营销型网站建设推广wordpress 更换模板
  • 有机农产品网站开发方案免费搭建网站哪个好
  • 响水做网站的公司wordpress菜单下拉特效
  • 乐陵读音济南seo公司
  • 定制网站建设价格做网站技术员
  • 购物类网站都有哪些模块wordpress数学公式的代码