elemeentui el-table封装

elemeentui el-table封装

复制代码
<template>
  <div style="height: 100%;">
    <el-table ref="sneTable"  
    element-loading-text="加载中" 
    element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(45,47,79, 0.8)" 
      :border="border" 
      :stripe="stripe" 
      :height="height"
      :max-height="maxHeight" 
      :size="size" 
      :row-key="rowKey" 
      :data="dataSource" 
      style="width: 99%;font-size: 14px "
      :default-expand-all="isExpandAll" 
      :tree-props="treeProps" 
      :span-method="onSpanMethod"
      @current-change="currentRowChange" 
      @selection-change="selectionChange" 
      @row-click="rowClick"
      @sort-change="sortChange">
      <div slot="empty" class="relative">
        <empty />
      </div>
      <el-table-column v-if="selector" reserve-selection fixed="left" type="selection" header-align="center"
        align="center" width="50" :selectable="checkSelectTable" />
      <el-table-column v-if="index" align="center" fixed="left" type="index" label="序号" width="50"></el-table-column>
      <template v-for="(column, i) in columns">
        <el-table-column v-if="isShow(column)" :key="i" :prop="column.prop" :label="column.label" :width="column.width"
          :min-width="column.minWidth" :sortable="column.sortable || false" :align="column.align || 'left'"
          :fixed="column.fixed" :show-overflow-tooltip="showTooltip">
          <template #default="{ row }">
            <span v-if="!column.slotName">
              {{
                row[column.prop] || row[column.prop] === 0
                ? row[column.prop]
                : "--"
              }}
            </span>
            <slot v-else :name="column.slotName" :data="row" />
          </template>
        </el-table-column>
      </template>
    </el-table>
  </div>
</template>
<script setup>
const props = defineProps({
  // 加载
  loading: { type: Boolean, default: false },
  // 是否带边框
  border: { type: Boolean, default: false },
  // 是否带斑马纹
  stripe: { type: Boolean, default: false },
  // 是否有序号
  index: { type: Boolean, default: true },
  // 表格高度 不传默认是计算后的高度
  height: { type: [Number, String], default: null },
  // 表格最大高度
  maxHeight: { type: Number, default: null },
  // 表格大小
  size: { type: String, default: "small" },
  // 唯一标识
  rowKey: { type: String, default: null },
  // 表数据
  dataSource: { type: Array, default: () => [] },
  // 是否多选
  selector: { type: Boolean, default: false },

  // 表头
  columns: { type: [String, Object, Array], required: true },
  // 是否展开
  isExpandAll: { type: Boolean, default: false },
  // 渲染嵌套数据的配置选项
  treeProps: {
    type: Object,
    default: () => { }
  },
  small: { type: Boolean, default: false },
  showTooltip: {
    type: Boolean,
    default: true
  }
})
const emit = defineEmits()
function sortChange({ column, prop, order }) {
  emit("sortChange", { column, prop, order });
}
function onSpanMethod({ rowIndex, columnIndex }) {
  let obj = { rowspan: 1, colspan: 1 };
  emit("onSpanMethod", { rowIndex, columnIndex }, val => {
    obj = val;
  });
  return obj;
}
function isShow(c) {
  return c.show === undefined ? true : c.show;
}
// 当前行变化
function currentRowChange(row) {
  if (row) emit("currentRowChange", row);
}

// 多选
function selectionChange(values) {
  emit("selection-change", values);
}

// 设置多选框(数据增加selectionIsSelect字段,判断当前是否可勾选)
function checkSelectTable(row) {
  return row.selectionIsSelect !== undefined ? row.selectionIsSelect : true;
}
// 点击某一行
function rowClick(row) {
  if (row) emit("row-click", row);
}
</script>

父使用

复制代码
  <sne-table ref="sRef" :loading="loading" :selector="true" size="mini" row-key="id" height="calc( 100% - 140px )"
         :data-source="noticeList" :columns="columns" @selection-change="handleSelectionChange">
          <template #noticeType="{data}">
            <dict-tag :options="sys_notice_type" :value="data.noticeType" />
          </template>
          <template #status="{ data }">
            <dict-tag :options="sys_notice_status" :value="data.status" />
         </template>
         <template #createTime="{ data }">
            <span>{{ parseTime(data.createTime, '{y}-{m}-{d}') }}</span>
         </template>
         <template #operate="{ data }">
            <el-button  link type="primary" :icon="EditPen" @click="handleUpdate(data)">修改</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(data)" v-hasPermi="['system:notice:remove']">删除</el-button>
         </template>
      </sne-table>

elemeentuiPlus el-table封装 vue3

复制代码
<template>
  <div style="height: 100%;">
    <!-- element-loading-text="加载中" 
    element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(45,47,79, 0.8)"  -->
    <el-table ref="sneTable"  
      :data="dataSource" 
     
      :row-key="rowKey"
      v-loading="loading"
      :default-expand-all="isExpandAll" 
      :tree-props="treeProps" 
      :border="border" 
      :stripe="stripe" 
      :height="height"
      :max-height="maxHeight" 
      size="default" 
      @row-click="rowClick"
      @selection-change="selectionChange" 
      @sort-change="sortChange"
      @current-change="currentRowChange" 
      :span-method="onSpanMethod"
      style="width: 99%;font-size: 14px ">
      <el-table-column v-if="selector" reserve-selection fixed="left" type="selection" header-align="center"
        align="center" width="50" :selectable="checkSelectTable" />
        <el-table-column v-if="index" align="center" fixed="left" type="index" label="序号" width="50"></el-table-column>
        <template  v-for="(column, i) in columns">
        <el-table-column  v-if="isShow(column)" :prop="column.prop" :label="column.label" :width="column.width"
          :min-width="column.minWidth" :sortable="column.sortable || false" :align="column.align || 'left'"
          :fixed="column.fixed" :show-overflow-tooltip="showTooltip">
          <template #default="{ row }">
            <span v-if="!column.slotName">
            {{ row[column.prop] ||  row[column.prop] === 0? row[column.prop] :'' }}</span>
            <slot :name="column.slotName" :data="row" />
          </template>
        </el-table-column>
      </template>
    </el-table>
  </div>
</template>
<script setup>
const props = defineProps({
  // 是否带边框
  border: { type: Boolean, default: false },
  // 是否带斑马纹
  stripe: { type: Boolean, default: false },
  // 是否有序号
  index: { type: Boolean, default: true },
  // 表格高度 不传默认是计算后的高度
  height: { type: [Number, String], default: null },
  // 表格最大高度
  maxHeight: { type: Number, default: null },
  // 表格大小
  size: { type: String, default: "large" },
    // 表数据
    dataSource: {type: Array,default: () => []},
     // 加载
    loading: {type: Boolean, default: false},
      // 表头
    columns: { type: [String, Object, Array], required: true },
    // 是否显示tooltip
    showTooltip: {type: Boolean,default: true},
      // 是否多选
  selector: { type: Boolean, default: false },
  // 唯一标识
  rowKey: { type: String, default: null },
    // 是否展开
    isExpandAll: { type: Boolean, default: false },
  // 渲染嵌套数据的配置选项
  treeProps: { type: Object, default: () => { }},
})
const emit = defineEmits()

function onSpanMethod({ rowIndex, columnIndex }) {
  // let obj = { rowspan: 1, colspan: 1 };
  // emit("onSpanMethod", { rowIndex, columnIndex }, val => {obj = val;});
  // return obj;
}
function isShow(c) {
  return c.show === undefined ? true : c.show;
}
// 当前行变化
function currentRowChange(row) {
  // if (row) emit("currentRowChange", row);
}
function sortChange({ column, prop, order }) {
  // emit("sortChange", { column, prop, order });
}
// 多选
function selectionChange(values) {
  emit("selection-change", values);
}

// 设置多选框(数据增加selectionIsSelect字段,判断当前是否可勾选)
function checkSelectTable(row) {
  return row.selectionIsSelect !== undefined ? row.selectionIsSelect : true;
}
// // 点击某一行
function rowClick(row) {
  // if (row) emit("row-click", row);
}
</script>
相关推荐
MXN_小南学前端4 小时前
watch详解:与computed 对比以及 Vue2 / Vue3 区别
前端·javascript·vue.js
2601_949814695 小时前
Docker部署Spring Boot + Vue项目
vue.js·spring boot·docker
Mr Xu_5 小时前
从后端数据到前端图表:深入解析 reduce 与 flatMap 的数据整形实战
前端·javascript
喜欢吃鱿鱼7 小时前
DES加解密(附带解决转义问题)-VUE
开发语言·前端·javascript
Lkstar7 小时前
逐步搞懂 Vue 的 patchChildren,把 Diff 算法拆给你看
vue.js
Jenlybein7 小时前
速学 VS Code 插件开发入门,客制化你的开发体验
前端·javascript·visual studio code
ZC跨境爬虫8 小时前
UI前端美化技能提升日志day7:(原生苹方字体全局适配+合规页脚完整像素级落地)
前端·javascript·ui·html·交互
好运的阿财8 小时前
OpenClaw工具拆解之tts+web_search
前端·javascript·python·ai·ai编程·openclaw·openclaw工具
whinc8 小时前
Node.js技术周刊 2026年第17周
前端·javascript
nbsaas-boot8 小时前
100万门店级分货系统架构设计
前端·javascript·vue.js