el-table操作栏优化

使用场景:

例如:经常会遇到el-table中操作项中有多个操作按钮,table-cell容纳不下。


解决方案:

根据宽度调整显示个数,显示不下的都移到扩展符中。
还需找到一种方法不手动设置margin,使扩展符和按钮保持相同的距离。
尚不清楚有何边界情况bug。

完全显示
在这里插入图片描述

<template>
  <div ref="ofRef" class="of">
    <div class="stdPart">
      <slot />
    </div>
    <el-dropdown v-if="showDropdown" trigger="click" ref="dropdownRef">
      <el-button
        link
        type="primary"
        size="small"><el-icon><More /></el-icon></el-button>
      <template #dropdown>
        <el-dropdown-menu>
          <slot />
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script setup>
import { ref, onMounted, nextTick } from 'vue'
const ofRef = ref()
const dropdownRef = ref()
const cellWidth = ref(0) // cell宽
const scrollWidth = ref(0) // 内容宽
const showDropdown = ref(false) // 显隐
onMounted(() => {
  // console.log(ofRef)
  cellWidth.value = ofRef.value.parentNode.offsetWidth - 32 // 减去padding
  scrollWidth.value = ofRef.value.scrollWidth
  if (cellWidth.value < scrollWidth.value) {
    const nodes = ofRef.value.children[0].children
    // const nodes = ofRef.value.children
    let maxCount = 0 // 最多能放下几个
    for (const v of nodes) {
      if (v.offsetLeft + v.offsetWidth <= cellWidth.value) {
        maxCount++
      }
    }
    for (let i = maxCount - 1; i < nodes.length; i++) {
      nodes[i].style.display = 'none'
    }
    showDropdown.value = true
    nextTick(() => {
      const dropdownNodes = dropdownRef.value.contentRef.children
      for (let i = 0; i < maxCount - 1; i++) {
        dropdownNodes[i].style.display = 'none'
      }
    })
  }
})

</script>

<style lang="scss" scoped>
.of{
  display: flex;
  .stdPart{
    display: flex;
    align-items: center;
  }
  .el-dropdown{
    display: flex;
    align-items: center;
    margin-left: 12px;
  }
}
.el-dropdown-menu{
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px;
  :deep(.el-button){
    margin: 4px;
  }
}
</style>


<template> <div> <div> <!--搜索栏--> <el-input v-model="name" placeholder="请输入菜单名称" style="width: 200px" prefix-icon="el-icon-user"></el-input> <el-button class="el-icon-search" style="margin-left: 10px" type="primary" @click="load">查询</el-button> <el-button class="el-icon-refresh" style="margin-left: 10px" type="warning" @click="reset">重置</el-button> </div> <div style="margin-top: 20px;margin-bottom:35px;"> <!-- 新增、批量删除 --> <el-button class="el-icon-plus" style="margin-right: 10px" type="success" @click="save(null)">新增</el-button> <el-button class="el-icon-close" style="margin-left: 10px" type="danger" @click="deleteByids" >批量删除</el-button> </div> <el-table :data="tableData" border stripe :header-cell-style="getRowClass" @selection-change="handleSelectionChange" row-key="id" border default-expand-all> <el-table-column type="selection" width="55"></el-table-column> <el-table-column prop="id" label="id"></el-table-column> <el-table-column prop="name" label="菜单名称"></el-table-column> <el-table-column prop="path" label="菜单路径"></el-table-column> <el-table-column prop="icon" label="菜单图标"> <template v-slot:default="scope"> <i :class="scope.row.icon"></i> </template> </el-table-column> <el-table-column prop="pagePath" label="页面路径"></el-table-column> <el-table-column prop="description" label="菜单描述"></el-table-column> <el-table-column prop="sortNum" label="排序"></el-table-column> <el-table-column label="操作" width="300"> <template v-slot="scope"> <div class="action-buttons"> <!-- 添加容器方便布局 --> <el-button type="success" size="mini" class="compact-btn" @click="save(scope.row.id)" v-if="!scope.row.pid && !scope.row.path">新增子菜单</el-button> <el-button type="prima
最新发布
03-29
<u-modal :ref="`senPi`" :size="`huge`" :is-heading-slot-empty="true"> <template #title> <u-text :text="`报价详情`"></u-text> </template> <template #body> <u-linear-layout direction="vertical" wrap> <u-form :layout="`inline`" key="form_13"> <u-form-item :layout="`center`"> <template #label> <u-text :text="`供应商id`"></u-text> </template> <u-select :clearable="true" :placeholder="`请选择供应商id`" :dataSource="__select_20_handleDataSourceLoad()" :pageSize="50" :textField="`supplier.name`" :valueField="`supplier.name`" :pagination="true" :value.sync="filter2.supplierId" :emptyValueIsNull="true" key="select_20" > <template #option="current"></template> <template #renderFooter></template> </u-select> </u-form-item> <u-form-item :layout="`center`"> <template #label> <u-text :text="`最终供应商`"></u-text> </template> <u-select :clearable="true" :value.sync="filter2.finalSupplier" :placeholder="`请选择最终供应商`" :dataSource="$utils['EnumToList']({ concept: 'TypeAnnotation', typeKind: 'reference', typeNamespace: 'app.enums', typeName: 'Is_Final_Supplier', inferred: false, ruleMap: {}, typeArguments: [] })" :emptyValueIsNull="true" key="select_22" > <template #option="current"></template> <template #renderFooter></template> </u-select> </u-form-item> <u-form-item :layout="`center`" :labelSize="`auto`"> <u-button :color="`primary`" :text="`查 询`" @click="onQueryClick($event)"></u-button> </u-form-item> </u-form> <!-- 树形表格,支持折叠子表 --> <el-table :data="treeData" row-key="id" :tree-props="{ children: 'quotedPrices', hasChildren: 'hasChildren' }" border style="--cw-style-font-size: 10px; font-size: 10px; --table-view-th-padding: 1px; --table-view-td-padding: 1px; --table-cell-padding: 0px" > <!-- 展开 --> <el-table-column type="expand" label="展开" width="50" :expandable="(row) => Array.isArray(row.quotedPrices) && row.quotedPrices.length > 0" :expanded="(row) => row.isExpanded" @expand-change="handleExpandChange" ></el-table-column> <el-table-column prop="materialApplyId" label="物料申请单id" width="150"></el-table-column> <el-table-column prop="supplierId" label="供应商id" width="150"></el-table-column> <el-table-column prop="finalSupplier" label="最终供应商" width="150"></el-table-column> <!-- 子表格:报价详情 --> <el-table-column label="报价详情" width="300"> <template #default="scope"> <el-table v-if="Array.isArray(scope.row.quotedPrices) && scope.row.quotedPrices.length > 0" :data="scope.row.quotedPrices" style="width: 100%; font-size: 10px;" border > <el-table-column prop="times" label="报价次数" width="100"></el-table-column> <el-table-column prop="price" label="报价价格" width="100"></el-table-column> <el-table-column prop="lastPrice" label="是否最终报价" width="100"></el-table-column> </el-table> </template> </el-table-column> </el-table> </u-linear-layout> </template> </u-modal>
03-12
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值