el-table操作列的按钮超过三个时,动态计算,将多余的按钮放入更多el-dropdown-menu中

一下是封装好的操作列组件:OperateBtn

<template>
  <div class="button-group">
    <div
      class="visibleButtons"
      v-for="(button, index) in visibleButtons"
      :key="index"
    >
      <el-button
        link
        type="primary"
        :icon="button.icon"
        @click="button.click"
        v-if="button.isshow && button.text !== '删除'"
      >
        {{ button.text }}
      </el-button>
      <el-popconfirm
        title="确定删除吗?"
        width="200px"
        @confirm="button.click"
        :teleported="false"
        placement="bottom"
        v-if="button.isshow && button.text === '删除'"
      >
        <template #reference>
          <el-button
            link
            type="primary"
            :icon="button.icon"
            @click.stop="button.del"
          >
            {{ button.text }}
          </el-button>
        </template>
      </el-popconfirm>
    </div>

    <el-dropdown v-if="showMore">
      <span class="el-dropdown-link">
        更多
        <el-icon class="el-icon--right">
          <arrow-down />
        </el-icon>
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item
            v-for="(button, index) in hiddenButtons"
            :key="index"
          >
            <span v-if="button.text !== '删除'" @click="button.click">
              {{ button.text }}
            </span>
            <el-popconfirm
              title="确定要删除吗?"
              confirmButtonText="确定"
              cancelButtonText="取消"
              @confirm="button.click"
              v-if="button.text === '删除'"
            >
              <template #reference>
                <el-button link @click.stop="button.del">
                  {{ button.text }}
                </el-button>
              </template>
            </el-popconfirm>
          </el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>
<script lang="ts">
import { defineComponent, computed, ref } from "vue";

interface Item {
  text: string;
  icon: string;
  isshow: boolean;
  click: () => void;
  del?: () => void;			// 这个是可选属性  不给也没事
}

export default defineComponent({
  name: "ButtonGroup",
  props: {
    buttons: {
      type: Array as () => Item[],
      default: () => [],
    },
  },
  setup(props) {
    const isshowButtons = () => {
      return props.buttons?.filter((item: any) => {
        return item.isshow;
      });
    };
    let resultBtn = ref(isshowButtons());
    const visibleButtons = computed(() => {
      if (resultBtn.value.length <= 3) {
        return resultBtn.value;
      } else {
        return resultBtn.value.slice(0, 2);
      }
    });
    const hiddenButtons = computed(() => {
      if (resultBtn.value.length <= 3) {
        return [];
      } else {
        return resultBtn.value.slice(2);
      }
    });
    const showMore = computed(() => {
      return resultBtn.value.length > 3;
    });
    const del = () => {};
    return { visibleButtons, hiddenButtons, showMore, del };
  },
});
</script>

<style lang="scss" scoped>
.button-group {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  align-items: center;
}
</style>

下面是在el-table中的使用:

<el-table>
	<el-table-column align="right">
      <operate-btn
		// :key="scope.row.id"			// 子组件必须绑定一个key,不然切换表格分页的时候,表格操作列不会更新
		:key="Math.random()"		// 2023_0807改成这个,用其他的会发现,即使你觉得id是唯一的,但是想在操作列获取到该行数据的时候,偶尔会出现row没更新的情况,改成这个最保险,每次点击都是最新的
		:buttons="[			// 传给子组件的值,用来渲染按钮,包括:按钮文字、图标、按钮的点击事件、以及按钮的显示与隐藏控制
            {
              text: '编辑',
              icon: 'Edit',
              click: () => handleEdit(row),
              isshow: row.status !== 1 ? true : false,
            },
            {
              text: '订阅详情',
              icon: 'Warning',
              click: () => toAuthDetail(row),
              isshow: row.status === 2 ? true : false,
            },
            {
              text: '重新上架',
              icon: 'RefreshRight',
              click: () => handleRack(row),
              isshow: row.status === 3 ? true : false,
            },
            {
              text: '下架申请',
              icon: 'BottomRight',
              click: () => handleUnShelve(row),
              isshow: row.status === 2 ? true : false,
            },
            {
              text: '上架申请',
              icon: 'TopRight',
              click: () => handleRack(row),
              isshow: row.status === 0 || row.status === 4 ? true : false,
            },
            {
              text: '删除',
              icon: 'Delete',
              click: () => delApi(row.id),
              isshow:
                row.status === 0 || row.status === 3 || row.status === 4
                  ? true
                  : false,
              del: () => del($index),
            },
            // 只有微服务平台和已上架可见
            {
              text: '设置流控规则',
              icon: 'DataAnalysis',
              click: () => openFlowcontrol(row),
              isshow: row.status === 2 && row.source === 1 ? true : false,
            },
            {
              text: '设置超时规则',
              icon: 'Timer',
              click: () => openTimeout(row),
              isshow: row.status === 2 && row.source === 1 ? true : false,
            },
            {
              text: '监控',
              icon: 'Monitor',
              click: () => openMonitor(row),
              isshow: row.status === 2 && row.source === 1 ? true : false,
            },
          ]
		/>
    </el-table-column>
</el-table>
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以在el-table的columns定义一个slot来实现操作显示更多。具体做法如下: 1. 在columns定义一个type为'operation'的column,该column的width可以根据您的需求进行设置。 2. 在该column定义一个slot,用来放置操作按钮。 3. 在该slot,可以定义多个按钮,您可以使用el-button或其他UI组件来实现,同可以为按钮绑定点击事件。 4. 如果您的操作按钮过多,可以使用el-dropdown组件来进行收纳。 以下是一个示例代码片段: ```html <el-table :data="tableData"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column label="操作" width="200" type="operation"> <template slot-scope="scope"> <el-dropdown> <el-button type="primary">更多操作</el-button> <el-dropdown-menu slot="dropdown"> <el-dropdown-item @click="handleEdit(scope.$index, scope.row)">编辑</el-dropdown-item> <el-dropdown-item @click="handleDelete(scope.$index, scope.row)">删除</el-dropdown-item> <el-dropdown-item @click="handleView(scope.$index, scope.row)">查看</el-dropdown-item> <!-- 更多按钮 --> </el-dropdown-menu> </el-dropdown> </template> </el-table-column> </el-table> ``` 在上述示例,我们定义了一个type为'operation'的column,并在该column定义了一个slot。在该slot,我们使用了el-dropdown组件来收纳操作按钮。您可以根据实际需求来定义操作按钮及其事件处理函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值